-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstaller.js
38 lines (34 loc) · 1.06 KB
/
installer.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
let exec;
let cwd;
const execute = command => {
if (!exec) {
exec = require('child_process').exec;
cwd = process.cwd();
}
return new Promise(resolve => {
exec(command, {cwd}, (error, stdout, stderr) => resolve({stdout, stderr}));
});
};
const installNpm = async (logger, name) => {
logger.debug(`installing '${name}'`);
const {stdout, stderr} = await execute(`npm i ${name}`);
logger.debug(`installation of '${name}' finished: \n${stdout}${stderr ? stderr+'\n\n' : ''}`);
if (!stdout.includes(`${name}@`)) {
throw new Error(`${name} module not available, run 'npm i ${name}' to install it`)
}
};
let cache = {};
const asyncRequire = async (logger, name, noInstallation) => {
let item = cache[name];
if (!item) {
try {
cache[name] = item = require(name);
} catch (e) {
if (noInstallation) throw e;
await installNpm(logger, name);
return await asyncRequire(logger, name, true);
}
}
return item;
};
module.exports = {asyncRequire};