/
install.js
executable file
·119 lines (91 loc) · 2.85 KB
/
install.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
#!/usr/bin/env node
var fs = require('fs')
, path = require('path')
, program = require('commander')
, installNpm = require('./install-npm.js')
, installYarn = require('./install-yarn.js')
, rootPath = process.cwd()
, envLabel = 'install_peers_skip'
;
program.option('-f, --force-run', 'Force Run').parse(process.argv);
if (program.forceRun) {
installPeerDeps({ forceRun: true });
} else {
installPeerDeps();
}
// --- Subroutines
function installPeerDeps({ forceRun = false } = {}) {
var argv;
// only run on `install`
if (process.env['npm_config_argv']) {
argv = JSON.parse(process.env['npm_config_argv']);
if (!forceRun && argv && !['install', 'i'].includes(argv['cooked'][0])) {
console.log('Only run install-peer-deps after `install` command. Skipping.');
return;
}
}
// check for the "kill switch"
if (process.env[envLabel]) {
console.log('Only run install-peer-deps once. Skipping.');
return;
}
// yo, do not install peers while installing peers
process.env[envLabel] = '1';
getPackageConfig(rootPath, function(config) {
var peerDeps = getPeerDeps(config);
if (!peerDeps || !peerDeps.length) {
console.error('Unable to find peerDependencies in ' + rootPath);
return;
}
// ready to install, switch directories
process.chdir(rootPath);
// TODO: Add more alternatives
if (installYarn) {
installYarn(peerDeps, config, installDone.bind(null, 'yarn', peerDeps.length));
} else if (installNpm) {
installNpm(peerDeps, config, installDone.bind(null, 'npm', peerDeps.length));
}
});
}
function installDone(tool, packagesNum, result) {
// cleanup env
process.env[envLabel] = '';
// Summary
console.log('+ Successfully installed ' + packagesNum + ' peerDependencies via ' + tool + '.\n');
// Verbose
console.log(result);
}
function getPeerDeps(config) {
var peerDeps;
if (typeof config.peerDependencies === 'object' && !Array.isArray(config.peerDependencies)) {
peerDeps = Object.keys(config.peerDependencies).map(function(name) {
return name + '@' + config.peerDependencies[name];
});
}
return peerDeps;
}
function getPackageConfig(packagePath, callback) {
var packageFile = path.join(packagePath, 'package.json')
, config
;
fs.readFile(packageFile, 'utf-8', function(error, content) {
if (error || !content) {
console.error('Unable to read ' + packageFile + ':', error || 'no content');
return;
}
config = parseConfig(content);
if (config.isParseConfigFailed) {
console.error('Unable to parse ' + packageFile + ':', config.error);
return;
}
callback(config);
});
}
function parseConfig(config) {
try {
config = JSON.parse(config);
} catch (error) {
config = {isParseConfigFailed: true, error: error};
}
return config;
}