-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathhelpers.js
73 lines (64 loc) · 1.58 KB
/
helpers.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const os = require('os');
const path = require('path');
const exec = require('child_process').exec;
const _root = path.resolve(__dirname, '..');
/**
* Plaform independant path to an executable cmd
* @param {string} path
*/
platformPath = (path) => {
return /^win/.test(os.platform()) ? `${path}.cmd` : path;
};
/**
*
* @param {string[]} args
*/
rootDir = (...args) => {
return path.join.apply(path, [_root].concat(...args));
};
/**
*
* @param {string} cmd
*/
binPath = (cmd) => {
return platformPath(`/node_modules/.bin/${cmd}`);
};
/**
* Promisified child_process.exec
*
* @param cmd
* @param opts See child_process.exec node docs
* @returns {Promise<number>}
*/
execp = (cmd, opts) => {
opts = Object.assign(opts || {}, {
stdout: process.stdout,
stderr: process.stderr
});
return new Promise((resolve, reject) => {
const child = exec(cmd, opts,
(err, stdout, stderr) => err ? reject(err.code) : resolve(0));
if (opts.stdout) {
child.stdout.pipe(opts.stdout);
}
if (opts.stderr) {
child.stderr.pipe(opts.stderr);
}
});
};
/**
* Install dependencies using yarn, if present, or npm otherwise.
* @param opts See child_process.exec node docs
* @returns {Promise<number>}
*/
installDependencies = (opts) => {
return execp('yarn -v') // first try to install deps using yarn
.then(exitCode => exitCode === 0 ? execp('yarn install', opts) : execp('npm install', opts));
};
var exports = module.exports = {
root: rootDir,
execp: execp,
binPath: binPath,
platformPath: platformPath,
installDependencies: installDependencies
};