-
Notifications
You must be signed in to change notification settings - Fork 37
/
execaHook.js
55 lines (42 loc) · 1.07 KB
/
execaHook.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// NOTE this workaround forces execa calls to be always sync
// Discussion: https://github.com/semantic-release/semantic-release/issues/193#issuecomment-462063871
const ritm = require("require-in-the-middle");
const execa = require("execa");
let interceptor;
const uncache = () => delete require.cache[require.resolve("execa")];
const getExecaSyncPromisified = () =>
Object.assign((...args) => {
const result = new Promise((resolve, reject) => {
try {
resolve(execa.sync(...args));
} catch (e) {
reject(e);
}
});
result.stdout = new String("");
result.stderr = new String("");
result.stdout.pipe = () => {};
result.stderr.pipe = () => {};
return result;
}, execa);
const hook = () => {
if (interceptor) {
return;
}
const execaHooked = getExecaSyncPromisified();
interceptor = ritm(["execa"], () => execaHooked);
uncache();
console.log('"execa" hooked');
};
const unhook = () => {
if (interceptor) {
interceptor.unhook();
interceptor = null;
uncache();
console.log('"execa" unhooked');
}
};
module.exports = {
hook,
unhook
};