forked from alibaba/lowcode-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnpm.js
More file actions
41 lines (36 loc) · 1.04 KB
/
npm.js
File metadata and controls
41 lines (36 loc) · 1.04 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
const path = require('path');
const { pathExists } = require('fs-extra');
const spawn = require('cross-spawn-promise');
async function isNPMInstalled(args) {
return pathExists(path.join(args.workDir, 'node_modules'));
}
async function install(args) {
if (await isNPMInstalled(args)) return;
const { workDir, npmClient = 'tnpm' } = args;
try {
await spawn(npmClient, ['i'], { stdio: 'inherit', cwd: workDir });
} catch (e) {
// TODO
}
}
async function isNPMModuleInstalled(args, name) {
const modulePkgJsonPath = path.resolve(args.workDir || '', 'node_modules', name, 'package.json');
return pathExists(modulePkgJsonPath);
}
async function installModule(args, name) {
if (await isNPMModuleInstalled(args, name)) return;
const { workDir, npmClient = 'tnpm' } = args;
try {
await spawn(npmClient, [
'i',
name,
npmClient === 'npm' ? '--registry=https://registry.npmmirror.com' : ''
], { stdio: 'inherit', cwd: workDir });
} catch (e) {
// TODO
}
}
module.exports = {
installModule,
install,
};