-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.js
101 lines (91 loc) · 2.83 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
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
98
99
100
101
const Promise = require('bluebird');
const exec = require('child_process').exec;
module.exports = {
install: function(packages, opts){
if(packages.length == 0 || !packages || !packages.length){return Promise.reject("No packages found");}
if(typeof packages == "string") packages = [packages];
if(!opts) opts = {};
var cmdString = "npm install " + packages.join(" ") + " "
+ (opts.global ? " -g":"")
+ (opts.save ? " --save":" --no-save")
+ (opts.saveDev? " --save-dev":"")
+ (opts.legacyBundling? " --legacy-bundling":"")
+ (opts.noOptional? " --no-optional":"")
+ (opts.ignoreScripts? " --ignore-scripts":"");
return new Promise(function(resolve, reject){
var cmd = exec(cmdString, {cwd: opts.cwd?opts.cwd:"/", maxBuffer: opts.maxBuffer?opts.maxBuffer:200 * 1024},(error, stdout, stderr) => {
if (error) {
reject(error);
} else {
resolve(true);
}
});
if(opts.output) {
var consoleOutput = function(msg) {
console.log('npm: ' + msg);
};
cmd.stdout.on('data', consoleOutput);
cmd.stderr.on('data', consoleOutput);
}
});
},
uninstall: function(packages, opts){
if(packages.length == 0 || !packages || !packages.length){return Promise.reject(new Error("No packages found"));}
if(typeof packages == "string") packages = [packages];
if(!opts) opts = {};
var cmdString = "npm uninstall " + packages.join(" ") + " "
+ (opts.global ? " -g":"")
+ (opts.save ? " --save":" --no-save")
+ (opts.saveDev? " --saveDev":"");
return new Promise(function(resolve, reject){
var cmd = exec(cmdString, {cwd: opts.cwd?opts.cwd:"/"},(error, stdout, stderr) => {
if (error) {
reject(error);
} else {
resolve(true);
}
});
if(opts.output) {
var consoleOutput = function(msg) {
console.log('npm: ' + msg);
};
cmd.stdout.on('data', consoleOutput);
cmd.stderr.on('data', consoleOutput);
}
});
},
list:function(path){
var global = false;
if(!path) global = true;
var cmdString = "npm ls --depth=0 " + (global?"-g ":" ");
return new Promise(function(resolve, reject){
exec(cmdString, {cwd: path?path:"/"},(error, stdout, stderr) => {
if(stderr !== ""){
if (stderr.indexOf("missing")== -1 && stderr.indexOf("required") == -1) {
return reject(error);
}
}
var packages = [];
packages = stdout.split('\n');
packages = packages.filter(function(item){
if(item.match(/^├──.+/g) != null){
return true
}
if(item.match(/^└──.+/g) != null){
return true
}
return undefined;
});
packages = packages.map(function(item){
if(item.match(/^├──.+/g) != null){
return item.replace(/^├──\s/g, "");
}
if(item.match(/^└──.+/g) != null){
return item.replace(/^└──\s/g, "");
}
})
resolve(packages);
});
});
}
}