-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
207 lines (189 loc) · 5.5 KB
/
util.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
const fs = require('fs');
const url = require('url');
const path = require('path');
const zlib = require('zlib');
const util = require('util');
const http = require('http');
const https = require('https');
const exec = require('child_process').exec;
const spawn = require('child_process').spawn;
const readChunk = require('read-chunk');
const fileType = require('file-type');
const tar = require('tar-stream');
//process.env.NMPM_NPM_CLI = require.resolve('npm/bin/npm-cli');
const NPM = process.env.NMPM_NPM_CLI || 'npm';
const fsStat = util.promisify(fs.stat);
const fsUnlink = util.promisify(fs.unlink);
const readUrlChunk = (value) => {
return new Promise((resolve, reject) => {
const parsedUrl = url.parse(value);
if (parsedUrl.protocol) {
const request = ('https:' == parsedUrl.protocol ? https : http).get(value, res => {
res.once('data', chunk => {
res.destroy();
resolve(chunk);
});
});
request.on('error', (err) => reject(err));
} else {
reject(new Error('Not URL'))
}
});
};
const readTarballPkg = (opts) => {
return new Promise((resolve, reject) => {
let data = '';
const extract = tar.extract();
extract.on('entry', (header, stream, cb) => {
stream.on('data', (chunk) => {
if (header.name.endsWith('/package.json')) {
data += chunk;
}
});
stream.on('end', () => cb());
stream.resume();
});
extract.on('finish', () => {
let json = {};
try {
json = JSON.parse(data);
} catch (e) {}
resolve(json);
});
if ('tarball_url' == opts['type']) {
const parsedUrl = url.parse(opts['value']);
if (parsedUrl.protocol) {
const request = ('https:' == parsedUrl.protocol ? https : http).get(opts['value'], res => {
res.pipe(zlib.createGunzip()).pipe(extract);
});
request.on('error', (err) => reject(err));
} else {
reject(new Error('Not URL'))
}
} else {
fs.createReadStream(opts['value']).pipe(zlib.createGunzip()).pipe(extract);
}
});
};
const npmSpawn = util.promisify((args, callback) => {
const install = spawn(NPM, args, { stdio: 'inherit' });
install.on('close', function(code) {
callback(null, code);
});
});
const npmExec = util.promisify((cmd, callback) => {
exec(NPM + ' ' + cmd, (err, stdout, stderr) => {
if (err) {
return callback(err);
}
callback(null, { stdout, stderr });
});
});
const optsToString = (opts) => {
const arr = [];
for (let key in opts) {
if (undefined != opts[key]) {
arr.push('--' + key + '=' + opts[key]);
} else {
arr.push('--' + key);
}
}
return arr.join(' ');
};
const isTarball = (value) => {
const type = fileType.fromFile(value);
return type && ['tar', 'gz'].includes(type['ext']);
};
const isTarballFile = async (value) => {
return isTarball(await readChunk(value, 0, 4100));
};
const isTarballUrl = async (value) => {
return isTarball(await readUrlChunk(value));
};
const resolveName = async (value) => {
if (isUrl(value, ['http', 'https'])) {
try {
if (await isTarballUrl(value)) {
return {
type: 'tarball_url',
value: value
};
}
} catch (e) {}
} else {
try {
const resolvedPath = path.resolve(value);
const stat = await fsStat(resolvedPath);
if (stat.isDirectory()) {
return {
type: 'folder',
value: resolvedPath
};
} else if (stat.isFile() && await isTarballFile(resolvedPath)) {
return {
type: 'tarball_file',
value: resolvedPath
};
}
} catch (e) {}
}
return {
type: 'name',
value: value
};
};
const resolvePath = async (value, opts) => {
if (opts['global']) {
const cmd = 'root -g --no-update-notifier';
const { stdout } = await npmExec(cmd);
return path.join(stdout, value);
}
return path.join(opts['prefix'], 'node_modules', value);
};
const isNewerVersion = (oldVer, newVer) => {
const oldParts = oldVer.split('.');
const newParts = newVer.split('.');
for (let i = 0; i < newParts.length; i++) {
const a = ~~newParts[i]; // parse int
const b = ~~oldParts[i]; // parse int
if (a > b) {
return true;
}
if (a < b) {
return false;
}
}
return false;
};
const isUrl = (value, protocols) => {
try {
const url = new url.URL(value);
if (protocols) {
if (url.protocol) {
return protocols
.map(x => x + ':')
.includes(url.protocol)
;
}
} else {
return true;
}
} catch (err) {}
return false;
};
module.exports = {
fsStat,
fsUnlink,
readUrlChunk,
readTarballPkg,
npmSpawn,
npmExec,
optsToString,
isTarball,
isTarballFile,
isTarballUrl,
resolveName,
resolvePath,
isNewerVersion,
isUrl,
};