-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
187 lines (180 loc) · 9.41 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env node
const { program } = require('commander');
const chalk = require('chalk');
const download = require('download-git-repo');
const path = require('path');
const fs = require('fs');
const inquirer = require('inquirer');
const ora = require('ora');
const jsonfile = require('jsonfile');
const rimraf = require('rimraf');
const cwd = process.cwd();
const spinner = ora('');
program
.version('1.2.7')
.description('project boilerplate')
.option('-c, --create [projectName]', 'create new project')
.parse(process.argv);
if (process.argv.length <= 2) {
console.log(chalk.red('指令不能为空'));
return;
}
const projectName = program.create;
if (projectName != null) {
if (projectName && typeof projectName === 'string') {
const targetDir = path.resolve(cwd, projectName);
if (fs.existsSync(targetDir)) {
const stat = fs.statSync(targetDir);
if (!stat.isDirectory()) {
console.log(chalk.red(`${targetDir}已经存在,且不是文件夹`));
return;
}
const childFiles = fs.readdirSync(targetDir);
if (Array.isArray(childFiles) && childFiles.length > 0) {
console.log(chalk.red(`${targetDir}已经存在, 且不为空`));
return;
}
} else {
fs.mkdirSync(targetDir)
}
const pjGitDic = {
'react-ui-lib': 'gitee.com:Thyiad/pt-ui-lib#master',
'ts-lib': 'gitee.com:Thyiad/pt-ts-lib#master',
'vue3-vite-ele-spa': 'gitee.com:Thyiad/pt-vue3-vite-ele#master',
'vue3-vite-vant-spa': 'gitee.com:Thyiad/pt-vue3-vite-vant#master',
'react-vite-antd-spa': 'gitee.com:Thyiad/pt-vite#master',
'react-vite-zarm-spa': 'gitee.com:Thyiad/pt-vite#mobile',
'react-antd-ssr': 'gitee.com:Thyiad/react-ssr#master',
'react-antd-spa': 'gitee.com:Thyiad/react-ssr#master',
'react-zarm-ssr': 'gitee.com:Thyiad/react-ssr#mobile-init',
'react-zarm-spa': 'gitee.com:Thyiad/react-ssr#mobile-init',
'react-cra-spa': 'gitee.com:Thyiad/react-project#master',
'react-antd-pro-v4': 'gitee.com:Thyiad/pt-antd-pro-v4#master',
'react-antd-pro-v2': 'gitee.com:Thyiad/pt-antd-pro-v2#master',
'vue2-material': 'gitee.com:Thyiad/pt-vue-material#master',
'vue2-element': 'gitee.com:Thyiad/pt-vue-element#master',
'gin-mongo': 'gitee.com:Thyiad/pt-gin-mongo#master',
'koa-mongo': 'gitee.com:Thyiad/pt-koa-mongo#master',
'net-core-webapi': 'gitee.com:Thyiad/pt-net-core-webapi#master',
}
// 选择模板
inquirer
.prompt([
{
name: 'pt', // pt: project template
message: '请选择你要的模板:',
type: 'list',
choices: Object.keys(pjGitDic).map(item=>({name: item}))
}
])
.then(answers => {
const tagetGit = pjGitDic[answers.pt]
// clone 代码
spinner.start(`开始下载模板项目`)
download(tagetGit, projectName, { clone: true }, (err) => {
if (err) {
spinner.fail('下载模板项目失败,请确定已配置好gitee的ssh');
console.log(err);
} else {
spinner.succeed('下载模板项目完成');
spinner.start('开始处理代码');
if(['react-antd-ssr', 'react-zarm-ssr'].includes(answers.pt)){
// 修改package.json
const pkgJsonPath = path.resolve(targetDir, 'package.json');
const pkg = jsonfile.readFileSync(pkgJsonPath);
pkg.scripts["dev"] = "node webpack/dev.js ssr";
pkg.scripts["build"] = "node webpack/build.js ssr";
[
"dev:spa",
"dev:ssr",
"build:spa",
"build:ssr"
].forEach(item=>{
delete pkg.scripts[item];
});
jsonfile.writeFileSync(pkgJsonPath, pkg, { spaces: " " });
} else if (['react-antd-spa', 'react-zarm-spa'].includes(answers.pt)) {
// 删除server代码
rimraf.sync(path.resolve(targetDir, 'src/server'))
// 挪移client、global.d.ts到src平级
fs.renameSync(path.resolve(targetDir, 'src/client'), path.resolve(targetDir, 'client'));
fs.renameSync(path.resolve(targetDir, 'src/global.d.ts'), path.resolve(targetDir, 'global.d.ts'));
// 删除src目录,重命名client为src,挪移global.d.ts到新的src
fs.rmdirSync(path.resolve(targetDir, 'src'));
fs.renameSync(path.resolve(targetDir, 'client'), path.resolve(targetDir, 'src'));
fs.renameSync(path.resolve(targetDir, 'global.d.ts'), path.resolve(targetDir, 'src/global.d.ts'));
// 修改package.json
const pkgJsonPath = path.resolve(targetDir, 'package.json');
const pkg = jsonfile.readFileSync(pkgJsonPath);
pkg.scripts["dev"] = "node webpack/dev.js spa";
pkg.scripts["build"] = "node webpack/build.js spa";
// 删除script
[
"dev:spa",
"dev:ssr",
"build:spa",
"build:ssr"
].forEach(item=>{
delete pkg.scripts[item];
});
// 删除dependencies
[
"@loadable/server",
"art-template",
"koa",
"koa-art-template",
"koa-bodyparser",
"koa-static",
"@koa/router"
].forEach(item=>{
delete pkg.dependencies[item];
});
// 删除devDependencies
[
"@types/koa",
"@types/koa-bodyparser",
"@types/koa-static",
"@types/koa__router",
"@types/loadable__server"
].forEach(item=>{
delete pkg.devDependencies[item];
})
jsonfile.writeFileSync(pkgJsonPath, pkg, { spaces: " " });
// 修改tsconfig.json
const tsJsonPath = path.resolve(targetDir, 'tsconfig.json');
const ts = jsonfile.readFileSync(tsJsonPath);
ts.compilerOptions.paths["@client/*"] = ["*"];
ts.compilerOptions.paths["@/*"] = ["*"];
jsonfile.writeFileSync(tsJsonPath, ts, { spaces: " " });
// 修改create-config.js
const webpackConfPath = path.resolve(targetDir, 'webpack/create-config.js');
const webpackConf = fs.readFileSync(webpackConfPath).toString();
const spaWebpackConf = webpackConf
.replace(`'@client': path.resolve(cwd, 'src/client'),`, `'@client': path.resolve(cwd, 'src'),`)
.replace(`'@': path.resolve(cwd, \`src\${spaClientFolder}\`),`, `'@': path.resolve(cwd, 'src'),`);
fs.writeFileSync(webpackConfPath, spaWebpackConf);
// 修改env-config.js
const envConfPath = path.resolve(targetDir, 'webpack/env-config.js');
const envConf = fs.readFileSync(envConfPath).toString();
const spaEnvConf = envConf.replace(`sysType: 'ssr',`, `sysType: 'spa',`);
fs.writeFileSync(envConfPath, spaEnvConf);
}
// 追加.npmrc文件
const npmrcPath = path.resolve(targetDir, '.npmrc');
if(!fs.existsSync(npmrcPath)){
fs.writeFileSync(npmrcPath, 'registry=https://registry.npmmirror.com/')
}
spinner.succeed('处理代码成功');
}
})
})
.catch(error => {
console.log(chalk.red('inquirer 发生错误'));
console.log(error);
});
} else {
console.log(chalk.red("请输入项目名称"))
}
return;
}
console.log(chalk.red(`无法识别的指令: ${process.argv.slice(2).join(' ')}`));