-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
148 lines (138 loc) · 3.69 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
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
/**
* @file main file
*/
const program = require('commander');
const packageJson = require('./package.json');
const moment = require('moment');
const inquirer = require('inquirer');
const getPackageDetails = require('./lib/getPackageDetails');
const walkDependencies = require('./lib/walkDependencies');
const showImpact = require('./lib/showImpact');
const showDetails = require('./lib/showDetails');
const showQuickStats = require('./lib/showQuickStats');
const colors = require('colors/safe');
const install = require('./lib/install');
const exec = require('./lib/exec');
const getInstallCommand = require(
'./lib/getInstallCommand'
);
/**
* @param {string} nameVersion
* @return {object} name and version loose
*/
function parseName(nameVersion) {
// TODO: check urls
let nameVersionStr = String(nameVersion).trim();
let scope = false;
if (nameVersionStr[0] === `@`) {
scope = true;
nameVersionStr = nameVersionStr.slice(1);
}
let [name, versionLoose] = nameVersionStr.split(`@`);
if (!versionLoose) {
versionLoose = `latest`;
}
if (scope) {
name = `@${name}`;
}
return { name, versionLoose };
}
/**
* @param {string} command npm or yarn
* @param {string[]} args
* @return {string[]} prompt choices
*/
function getChoices(command, args) {
return [
`Install (${colors.bold(`${command} ${args.join(' ')}`)})`,
`Impact`,
`Details`,
`Skip`
];
}
/**
* @param {string} name
* @param {string} versionLoose
* @param {Object} packages
*/
function promptNextAction(name, versionLoose, packages) {
return getInstallCommand().then(({ command, args }) => {
const choices = getChoices(command, args);
return inquirer.prompt({
type: `list`,
name: `next`,
message: `What is next?`,
choices
}).then(({ next }) => {
switch (choices.indexOf(next)) {
case 0:
exec(command, args);
return Promise.reject();
case 1:
return showImpact(name, versionLoose, packages);
case 2:
return showDetails(packages);
default:
process.exit(0);
}
});
}).then(() => {
return promptNextAction(name, versionLoose, packages);
}, (e) => {
if (e) {
throw e;
}
});
}
/**
* install action
* @param {string} nameVersion package considering to install
*/
function installPackage(nameVersion) {
const { name, versionLoose } = parseName(nameVersion);
getPackageDetails(name, versionLoose)
.then((packageStats) => {
process.stdout.cursorTo(0);
process.stdout.clearLine(1);
process.stdout.write(`${
colors.bold(
`${packageStats.name}@${packageStats.version}`
)
} (updated ${
moment(packageStats.modified).fromNow()
})\n`);
return walkDependencies(
{ [name]: versionLoose }
);
})
.then((packages) => {
showQuickStats(packages);
return promptNextAction(
name, versionLoose, packages
);
})
.catch((e) => {
console.error(e);
process.exit(1);
});
}
program.version(packageJson.version);
program.description(packageJson.description);
program.usage('npm-consider install <pkg>');
if (!process.argv.slice(2).length) {
program.outputHelp();
}
program.command(`install [pkg]`)
.alias(`i`)
.action((pkg, options) => {
if (pkg) {
installPackage(pkg);
} else {
install(options);
}
})
.option(`-S, --save`, `Save to dependencies`)
.option(`-D, --save-dev`, `Save to devDependencies`)
.option(`--production`, `Will not install modules listed in devDependencies`)
.option(`--test`, `Exit with code 1 if package limits like maxPackagesNumber or maxSizeBites exceeded`);
program.parse(process.argv);