-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
118 lines (95 loc) · 2.57 KB
/
cli.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
#!/usr/bin/env node
const program = require("commander");
const execSync = require("child_process").execSync;
const chalk = require("chalk");
const inquirer = require("inquirer");
const symbols = require("log-symbols");
const ora = require("ora");
const library = require("./constant");
const PKG = require("./package.json");
program.version(PKG.version);
program
.command("ls")
.description("List all the popular react libraries")
.action(onLibrary);
program.parse(process.argv);
if (process.argv.length === 2) {
program.outputHelp();
}
function onLibrary() {
inquirer
.prompt([
{
name: "libraryIndex",
message: `请选择库类型索引\n${showLibraryType()}`
}
])
.then(answers => {
const { libraryIndex } = answers;
const libraryKeys = Object.keys(library);
if (isNaN(libraryIndex)) {
return exit("输入的索引类型错误");
}
if (libraryIndex < 1 || libraryIndex > libraryKeys.length) {
return exit("输入的索引超出范围");
}
const libraryKey = libraryKeys[libraryIndex - 1];
const librarys = library[libraryKey];
inquirer
.prompt([
{
name: "libraryList",
message: `请选择库索引,多个以空格分隔(例如:1 2)\n${showLibrary(
librarys
)}`
},
{
name: "dev",
message: `请选择安装以上库到开发还是生产环境(1: 生产,2:开发)`
}
])
.then(answers => {
const { libraryList, dev } = answers;
installList = "";
const spinner = ora("正在下载库...");
spinner.start();
libraryList.split(" ").forEach(function(index) {
installList += `${librarys[index]} `;
});
onUse(installList, dev);
spinner.succeed();
});
});
}
function showLibraryType() {
let info = [];
Object.keys(library).forEach(function(item) {
info.push(item);
});
return msg(info);
}
function showLibrary(librarys) {
let info = [];
Object.values(librarys).forEach(function(item) {
info.push(item);
});
return msg(info);
}
function onUse(name, dev) {
const params = dev ? "-S" : "-D";
execSync(`npm i ${params} ${name}`, { cwd: process.cwd() });
}
function exit(err) {
printErr(err);
process.exit(1);
}
function printErr(err) {
console.log(symbols.error, chalk.red(err));
}
function msg(infos) {
let msg = "";
infos.forEach(function(info, index) {
msg += `${index + 1}. ${info}\n`;
});
return msg;
}