lzx-test-cli 1.0.1 → 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 -15
- 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,20 +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
|
-
|
|
233
|
-
const templatePath = path.resolve("", templateChosen)
|
|
234
|
-
console.log("🚀 ~ :233 ~ templatePath:", templatePath)
|
|
233
|
+
|
|
235
234
|
if(downloadType === 'remote') {
|
|
236
235
|
const repoMap = {
|
|
237
236
|
'template-main': main_proj_remote_url,
|
|
@@ -247,14 +246,21 @@ program
|
|
|
247
246
|
await downloadFileToFolder(repoUrl, targetDir, fileName, headers);
|
|
248
247
|
}
|
|
249
248
|
} else{
|
|
250
|
-
|
|
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)
|
|
251
257
|
}
|
|
252
258
|
console.log(chalk.green("\n同步模版成功,开始解压并安装依赖..."));
|
|
253
259
|
await unCompressFileAndInstall(targetDir);
|
|
254
|
-
|
|
255
|
-
console.log(
|
|
256
|
-
|
|
257
|
-
);
|
|
260
|
+
syncTemplate.succeed(chalk.green(chalk.blue.underline.bold(projectName) + ' 项目创建成功!'))
|
|
261
|
+
// console.log(
|
|
262
|
+
|
|
263
|
+
// );
|
|
258
264
|
// 退出
|
|
259
265
|
process.stdin.pause();
|
|
260
266
|
process.exit(0); // 完成后正常退出
|
|
@@ -262,6 +268,8 @@ program
|
|
|
262
268
|
if (e && e.name === 'ExitPromptError') {
|
|
263
269
|
console.log('\n 已取消创建!');
|
|
264
270
|
process.exit(0);
|
|
271
|
+
}else{
|
|
272
|
+
console.log('\n 项目创建失败: ' + e.message);
|
|
265
273
|
}
|
|
266
274
|
process.exit(1); // 遇错退出
|
|
267
275
|
}
|
package/bin/test.js
ADDED