-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
139 lines (134 loc) · 3.92 KB
/
index.js
File metadata and controls
139 lines (134 loc) · 3.92 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
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
#!/usr/bin/env node
import inquirer from "inquirer";
import downloadGit from "download-git-repo";
import fs from "fs";
import path from "path";
import chalk from "chalk";
import ora from "ora";
import { Command } from "commander";
let __dirname = import.meta.dirname;
const config = {
vue: {
downloadpath: "github:vuejs/vue",
sourceName: "vue-source",
},
react: {
downloadpath: "github:facebook/react",
sourceName: "react-source",
},
angular: {
downloadpath: "githup:angular/angular",
sourceName: "angular-source",
},
ts: "ts.config.json",
webpack: "webpack.config.js",
};
let programe = new Command();
programe.version("1.0.0");
programe
.command("config [config-type]")
.description("获取指定类型配置文件: webpack 、ts")
.action((arg) => {
if (["ts.config.json", "webpack.config.js"].includes(config[arg])) {
let spinner = ora(chalk.greenBright("下载中,请稍后...")).start();
fs.createReadStream(`${__dirname}/lib/${config[arg]}`, {
highWaterMark: 5,
}).pipe(
fs.createWriteStream(path.resolve(process.cwd(), `./${config[arg]}`))
);
console.log(chalk.green(`${config[arg]}下载完成^_^`));
spinner.stop();
} else {
inquirer
.prompt([
{
type: "list",
message: "请选择需要下载的配置文件:",
name: "config",
choices: ["ts.config.json", "webpack.config.js"],
},
{
type: "confirm",
message: "是否下载?",
name: "yesOrno",
},
])
.then((answers) => {
if (answers.yesOrno) {
let spinner = ora(chalk.greenBright("下载中,请稍后...")).start();
fs.createReadStream(`${__dirname}/lib/${answers.config}`, {
highWaterMark: 5,
}).pipe(
fs.createWriteStream(
path.resolve(process.cwd(), `./${answers.config}`)
)
);
console.log(chalk.green(`${answers.config}下载完成^_^`));
spinner.stop();
} else {
console.log(chalk.green("拜拜啦..."));
}
});
}
});
programe
.command("source [source-type]")
.description("下载指定前端框架最新源码: vue、react、angular")
.action((source) => {
if (["vue", "react", "angular"].includes(source)) {
downloadFrontEnd({ frontEndFramework: source });
} else {
inquirer
.prompt([
{
type: "input",
message: "请输入文件夹名:",
name: "dirName",
},
{
type: "list",
message: "请选择需要下载的前端框架?",
name: "frontEndFramework",
choices: ["vue", "react", "angular"],
},
{
type: "confirm",
message: "是否下载?",
name: "yesOrno",
},
])
.then((answers) => {
if (answers.yesOrno) {
downloadFrontEnd(answers);
} else {
console.log(chalk.green("拜拜啦..."));
}
})
.catch((error) => {
console.log(chalk.redBright("退出了噢..."));
});
}
});
programe.parse(process.argv);
function downloadFrontEnd(answers) {
let sourceName =
answers.dirName || config[answers.frontEndFramework].sourceName;
if (fs.existsSync(`./${sourceName}`)) {
fs.rmSync(`./${sourceName}`, { recursive: true });
}
let cwd = process.cwd();
let spinner = ora(chalk.greenBright("下载中,请稍后...")).start();
downloadGit(
config[answers.frontEndFramework].downloadpath,
path.resolve(cwd, sourceName),
{},
(error) => {
if (error) {
console.log(chalk.red("下载失败o_0"));
} else {
console.log(chalk.green(`${answers.frontEndFramework}下载完成^_^`));
}
spinner.stop();
}
);
}