-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
71 lines (61 loc) · 2.16 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
'use strict';
const path = require('path');
const helpers = require('./lib/helpers');
const readPkgUp = require('read-pkg-up');
const platform = process.platform.toLowerCase();
const supportedPlatforms = require('./package').os;
const defaultOpts = {
clear: false,
// OSX options
dependencyColor: 'blue',
devDependencyColor: 'yellow',
// Windows options
dependencyHidden: false,
devDependencyHidden: false
};
module.exports = (projectPath, opts) => {
let modulesPath;
const findRootPackage = () =>
readPkgUp({cwd: projectPath}).then(pkg => {
if (!pkg.pkg) {
return Promise.reject(new Error(`couldn't find package.json in ${projectPath}`));
}
projectPath = path.dirname(pkg.path);
modulesPath = path.join(projectPath, 'node_modules');
return pkg.pkg;
});
return (function main() {
if (supportedPlatforms.indexOf(platform) === -1) {
return Promise.reject(new Error(`${platform} unsupported, supported platforms: ${supportedPlatforms}`));
}
const platformLib = require(`./lib/${platform}`);
if (typeof projectPath !== 'string') {
return Promise.reject(new TypeError('projectPath path should be a string'));
}
opts = Object.assign({}, defaultOpts, opts || {});
if (typeof platformLib.handleOptions === 'function') {
opts = platformLib.handleOptions(opts);
}
if (typeof platformLib.performPreAction !== 'function') {
platformLib.performPreAction = () => Promise.resolve();
}
return findRootPackage()
.then(rootPackage =>
platformLib.performPreAction(projectPath, opts)
.then(() =>
Promise.all([
helpers.getDeps(modulesPath, rootPackage.dependencies),
helpers.getDeps(modulesPath, rootPackage.devDependencies)
]).then(deps =>
Promise.all([
platformLib.performAction('dependencies', deps[0], opts),
platformLib.performAction('devDependencies', deps[1], opts)
])
).then(affectedDeps => ({
dependencies: affectedDeps[0],
devDependencies: affectedDeps[1]
}))
)
);
})();
};