-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
37 lines (32 loc) · 1.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
'use strict';
const shell = require('shelljs');
const tempWrite = require('temp-write');
const shebangRegex = require('shebang-regex');
const cmdShim = require('cmd-shim');
const pify = require('pify');
module.exports = (bin, shebang, code) => {
if (!shebangRegex.test(shebang)) {
shebang = '#!/usr/bin/env ' + shebang;
}
// On windows shebangs aren't supported. Use a fake extension to prevent
// the bin from being executed instead of the shim.
const fileName = bin + (process.platform === 'win32' ? '.x-mock-bin' : '');
let oldPath = shell.env.PATH;
return tempWrite(shebang + '\n' + code, fileName)
.then(filepath => {
// Path separator according to platform
const sep = process.platform === 'win32' ? ';' : ':';
shell.chmod('+x', filepath);
shell.env.PATH = filepath.replace(new RegExp(fileName + '$'), '') + sep + oldPath;
if (process.platform === 'win32') {
// On windows shebangs aren't supported, add cmd shim
return pify(cmdShim)(filepath, filepath.replace(new RegExp('\\.x-mock-bin$'), ''));
}
})
.then(() => () => {
if (oldPath) {
shell.env.PATH = oldPath;
oldPath = null;
}
});
};