-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (53 loc) · 1.48 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
'use strict';
const { spawnSync, spawn } = require('child_process');
const os = require('os');
const commandNpm = os.platform() === 'win32' ? 'npm.cmd' : 'npm';
/**
* Get npm command
* @param {string} name Dependency name
* @param {string} string Npm Registry(optional)
* @return {Array<string>} Npm command
*/
const getCommand = (name, registry) => {
if (registry) {
return ['show', name, '--json', '--registry', registry];
} else {
return ['show', name, '--json'];
}
};
/**
* Returns all details synchronously
* @param {string} name Dependency name
* @param {string} string Npm Registry(optional)
* @return {Object} All details of an npm dependency
*/
const seeSync = (name, registry) => {
try {
const result = spawnSync(commandNpm, getCommand(name, registry), {
cwd: process.cwd(),
env: process.env,
stdio: 'pipe',
encoding: 'utf-8'
});
return JSON.parse(result.stdout);
} catch (error) {
return error;
}
};
/**
* Returns all details asynchronously
* @param {string} name Dependency name
* @param {string} registry Npm Registry(optional)
* @return {Promise} Promise with all details of an npm dependency
*/
const see = (name, registry) =>
new Promise((resolve, reject) => {
const child = spawn(commandNpm, getCommand(name, registry));
child.stdout.on('data', data => {
resolve(JSON.parse(data));
});
child.stderr.on('data', err => {
reject(err);
});
});
module.exports = { see, seeSync };