forked from 418sec/install-package
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
97 lines (81 loc) · 1.77 KB
/
index.js
File metadata and controls
97 lines (81 loc) · 1.77 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
'use strict';
const execFile = require('child_process').execFile;
function isArray(value) {
return Array.isArray(value);
}
function isObject(value) {
return !Array.isArray(value) && typeof value === 'object';
}
function isString(value) {
return typeof value === 'string';
}
function isTrue(value) {
return value === true;
}
function normalizePackages(args) {
let pkgs = [];
if (isArray(args)) {
for (let pkg of args) {
if (isString(pkg)) {
pkgs.push(pkg);
}
}
} else if (isString(args)) {
pkgs.push(args);
}
return pkgs;
}
function normalizeOptions(args) {
let opts = [];
if (isArray(args)) {
for (let opt of args) {
if (isString(opt)) {
opts.push(opt);
}
}
} else if (isObject(args)) {
let keys = Object.keys(args);
for (let key of keys) {
let value = args[key];
if (isTrue(value)) {
opts.push(key);
} else if (isString(value)) {
opts.push(`${key}=${value}`);
}
}
} else if (isString(args)) {
opts.push(args);
}
return opts;
}
module.exports = function (packages, options, execOptions) {
let args = ['npm', 'install'];
let pkgs = normalizePackages(packages);
let opts = normalizeOptions(options);
let execOpts = execOptions || {};
if (pkgs.length === 0) {
throw new Error('Invalid package names');
}
for (let pkg of pkgs) {
args.push(pkg);
}
for (let opt of opts) {
args.push(opt);
}
return new Promise((resolve, reject) => {
execFile(args[0], args.slice(1), execOpts, (error, stdout, stderr) => {
if (error) {
return reject({
error,
stdout,
stderr
});
}
return resolve({
error,
stdout,
stderr
});
});
});
};