lzx-test-cli 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/index.js +23 -13
- package/bin/test.js +8 -0
- package/package.json +1 -1
package/bin/index.js
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import { fileURLToPath } from 'url';
|
|
3
5
|
import { Command } from 'commander';
|
|
4
6
|
import fs from 'fs-extra';
|
|
5
7
|
import inquirer from 'inquirer';
|
|
6
8
|
import chalk from 'chalk';
|
|
7
|
-
|
|
9
|
+
import ora from 'ora';
|
|
8
10
|
import compressing from 'compressing'; //支持- tar - gzip - tgz - zip
|
|
9
|
-
import path from 'path';
|
|
10
11
|
import { spawn } from 'child_process';
|
|
11
12
|
import fetch from 'node-fetch';
|
|
12
13
|
import cliProgress from 'cli-progress';
|
|
14
|
+
import { log } from 'console';
|
|
13
15
|
// import download from 'download-git-repo';
|
|
14
16
|
console.log(
|
|
15
17
|
chalk.green('开始创建项目!')
|
|
@@ -192,7 +194,7 @@ program
|
|
|
192
194
|
{ name: "本地", value: "local" },
|
|
193
195
|
{ name: "远程", value: "remote" }
|
|
194
196
|
],
|
|
195
|
-
default: '
|
|
197
|
+
default: 'local',
|
|
196
198
|
},
|
|
197
199
|
]);
|
|
198
200
|
let { templateChosen } = await inquirer.prompt([
|
|
@@ -218,18 +220,17 @@ program
|
|
|
218
220
|
message: '是否强制覆盖',
|
|
219
221
|
},
|
|
220
222
|
]);
|
|
221
|
-
|
|
222
|
-
|
|
223
|
+
const syncTemplate = ora('项目创建中...')
|
|
224
|
+
syncTemplate.start()
|
|
223
225
|
const cwd = process.cwd();
|
|
224
|
-
const targetDir =
|
|
226
|
+
const targetDir = path.resolve(cwd, projectName);
|
|
225
227
|
if (fs.existsSync(targetDir)) {
|
|
226
228
|
if (force) {
|
|
227
229
|
fs.removeSync(targetDir); //删除项目下已有的
|
|
228
230
|
}
|
|
229
231
|
}
|
|
230
232
|
fs.mkdirsSync(targetDir);
|
|
231
|
-
|
|
232
|
-
const templatePath = path.resolve("", templateChosen)
|
|
233
|
+
|
|
233
234
|
if(downloadType === 'remote') {
|
|
234
235
|
const repoMap = {
|
|
235
236
|
'template-main': main_proj_remote_url,
|
|
@@ -245,14 +246,21 @@ program
|
|
|
245
246
|
await downloadFileToFolder(repoUrl, targetDir, fileName, headers);
|
|
246
247
|
}
|
|
247
248
|
} else{
|
|
248
|
-
|
|
249
|
+
// const templatePath = path.resolve("templates", templateChosen); //不能这样使用,这是当前cli工具执行时的工作目录
|
|
250
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
251
|
+
const __dirname = path.dirname(__filename);
|
|
252
|
+
console.log("🚀 ~ :252 ~ __dirname:", __dirname)
|
|
253
|
+
const templatesDir = path.join(__dirname, '../', templateChosen);
|
|
254
|
+
console.log("🚀 ~ :254 ~ templatesDir:", templatesDir)
|
|
255
|
+
|
|
256
|
+
await fs.copySync(templatesDir, targetDir)
|
|
249
257
|
}
|
|
250
258
|
console.log(chalk.green("\n同步模版成功,开始解压并安装依赖..."));
|
|
251
259
|
await unCompressFileAndInstall(targetDir);
|
|
252
|
-
|
|
253
|
-
console.log(
|
|
254
|
-
|
|
255
|
-
);
|
|
260
|
+
syncTemplate.succeed(chalk.green(chalk.blue.underline.bold(projectName) + ' 项目创建成功!'))
|
|
261
|
+
// console.log(
|
|
262
|
+
|
|
263
|
+
// );
|
|
256
264
|
// 退出
|
|
257
265
|
process.stdin.pause();
|
|
258
266
|
process.exit(0); // 完成后正常退出
|
|
@@ -260,6 +268,8 @@ program
|
|
|
260
268
|
if (e && e.name === 'ExitPromptError') {
|
|
261
269
|
console.log('\n 已取消创建!');
|
|
262
270
|
process.exit(0);
|
|
271
|
+
}else{
|
|
272
|
+
console.log('\n 项目创建失败: ' + e.message);
|
|
263
273
|
}
|
|
264
274
|
process.exit(1); // 遇错退出
|
|
265
275
|
}
|
package/bin/test.js
ADDED