lzx-test-cli 1.0.1 → 1.0.3
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 +33 -28
- package/package.json +1 -2
package/bin/index.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
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
|
-
import cliProgress from 'cli-progress';
|
|
13
13
|
// import download from 'download-git-repo';
|
|
14
14
|
console.log(
|
|
15
15
|
chalk.green('开始创建项目!')
|
|
@@ -57,7 +57,7 @@ function runCommand(cwd) {
|
|
|
57
57
|
cwd: safeCwd,
|
|
58
58
|
stdio: 'inherit'
|
|
59
59
|
});
|
|
60
|
-
console.log(chalk.green("\n
|
|
60
|
+
console.log(chalk.green("\n开始安装依赖,请耐心等待..."));
|
|
61
61
|
child.on('error', reject);
|
|
62
62
|
child.on('close', code => {
|
|
63
63
|
if (code !== 0) {
|
|
@@ -70,41 +70,43 @@ function runCommand(cwd) {
|
|
|
70
70
|
});
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
+
function showPercent(downloaded, total) {
|
|
74
|
+
const percent = total > 0
|
|
75
|
+
? Math.floor((downloaded / total) * 100)
|
|
76
|
+
: 0;
|
|
77
|
+
|
|
78
|
+
// \r 回到当前行起始位置,不换行
|
|
79
|
+
process.stdout.write(`\r下载进度: ${percent}%`);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
|
|
73
83
|
/**
|
|
74
84
|
* @param {string} url 下载链接
|
|
75
85
|
* @param {string} targetDir 目标文件夹
|
|
76
86
|
* @param {string} fileName 保存的文件名
|
|
77
87
|
* @description: 下载文件并显示进度条
|
|
78
88
|
*/
|
|
79
|
-
export async function downloadFileToFolder(url, targetDir, fileName) {
|
|
89
|
+
export async function downloadFileToFolder(url, targetDir, fileName, headers = {}) {
|
|
80
90
|
// 目标文件夹确保存在
|
|
81
91
|
await fs.promises.mkdir(targetDir, { recursive: true });
|
|
82
92
|
|
|
83
93
|
const dest = path.join(targetDir, fileName);
|
|
84
94
|
|
|
85
95
|
const res = await fetch(url, {
|
|
86
|
-
headers
|
|
87
|
-
'User-Agent': 'node.js',
|
|
88
|
-
'Accept': 'application/octet-stream'
|
|
89
|
-
// 'Authorization': `token ${YOUR_TOKEN}`
|
|
90
|
-
},
|
|
96
|
+
headers,
|
|
91
97
|
redirect: 'follow', // 跟随重定向
|
|
92
98
|
});
|
|
93
99
|
if (!res.ok) throw new Error(`下载失败: ${res.status} ${res.statusText}`);
|
|
94
100
|
|
|
95
101
|
// 获取 content-length 用于进度条(如果服务器提供)
|
|
96
102
|
const total = +res.headers.get('content-length') || 0;
|
|
97
|
-
const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
|
|
98
|
-
if (total > 0) progressBar.start(total, 0);
|
|
99
|
-
|
|
100
103
|
const fileStream = fs.createWriteStream(dest);
|
|
101
|
-
|
|
102
|
-
|
|
103
104
|
let downloaded = 0;
|
|
105
|
+
|
|
104
106
|
// 监听 data 更新进度
|
|
105
107
|
res.body.on('data', chunk => {
|
|
106
108
|
downloaded += chunk.length;
|
|
107
|
-
if (total > 0)
|
|
109
|
+
if (total > 0) showPercent(downloaded, total);
|
|
108
110
|
});
|
|
109
111
|
|
|
110
112
|
await new Promise((resolve, reject) => {
|
|
@@ -218,20 +220,19 @@ program
|
|
|
218
220
|
message: '是否强制覆盖',
|
|
219
221
|
},
|
|
220
222
|
]);
|
|
221
|
-
|
|
222
|
-
|
|
223
|
+
const syncTemplate = ora({
|
|
224
|
+
text: chalk.yellow('项目创建中...'),
|
|
225
|
+
})
|
|
226
|
+
syncTemplate.start()
|
|
223
227
|
const cwd = process.cwd();
|
|
224
|
-
const targetDir =
|
|
228
|
+
const targetDir = path.resolve(cwd, projectName);
|
|
225
229
|
if (fs.existsSync(targetDir)) {
|
|
226
230
|
if (force) {
|
|
227
231
|
fs.removeSync(targetDir); //删除项目下已有的
|
|
228
232
|
}
|
|
229
233
|
}
|
|
230
234
|
fs.mkdirsSync(targetDir);
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
const templatePath = path.resolve("", templateChosen)
|
|
234
|
-
console.log("🚀 ~ :233 ~ templatePath:", templatePath)
|
|
235
|
+
|
|
235
236
|
if(downloadType === 'remote') {
|
|
236
237
|
const repoMap = {
|
|
237
238
|
'template-main': main_proj_remote_url,
|
|
@@ -242,19 +243,21 @@ program
|
|
|
242
243
|
const headers = {
|
|
243
244
|
'User-Agent': 'node.js',
|
|
244
245
|
'Accept': 'application/octet-stream',
|
|
246
|
+
// 'Authorization': `token ${YOUR_TOKEN}`
|
|
245
247
|
}
|
|
246
248
|
const fileName = getFileNameFromUrl(repoUrl);
|
|
247
249
|
await downloadFileToFolder(repoUrl, targetDir, fileName, headers);
|
|
248
250
|
}
|
|
249
251
|
} else{
|
|
250
|
-
|
|
252
|
+
// const templatePath = path.resolve("templates", templateChosen); //不能这样使用,这是当前cli工具执行时的工作目录
|
|
253
|
+
const __filename = fileURLToPath(import.meta.url); //由于使用esm, 需要使用__dirname读取模块相关的文件(不受执行位置影响)
|
|
254
|
+
const __dirname = path.dirname(__filename);
|
|
255
|
+
const templatesDir = path.join(__dirname, '../', templateChosen);
|
|
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
|
-
chalk.green(chalk.blue.underline.bold(projectName) + ' 项目创建成功!')
|
|
257
|
-
);
|
|
260
|
+
syncTemplate.succeed(chalk.green(chalk.blue.underline.bold(projectName) + ' 项目创建成功!'))
|
|
258
261
|
// 退出
|
|
259
262
|
process.stdin.pause();
|
|
260
263
|
process.exit(0); // 完成后正常退出
|
|
@@ -262,6 +265,8 @@ program
|
|
|
262
265
|
if (e && e.name === 'ExitPromptError') {
|
|
263
266
|
console.log('\n 已取消创建!');
|
|
264
267
|
process.exit(0);
|
|
268
|
+
}else{
|
|
269
|
+
console.log('\n 项目创建失败: ' + e.message);
|
|
265
270
|
}
|
|
266
271
|
process.exit(1); // 遇错退出
|
|
267
272
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lzx-test-cli",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"bin": {
|
|
5
5
|
"lzx-test-cli": "bin/index.js"
|
|
6
6
|
},
|
|
@@ -22,7 +22,6 @@
|
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"@types/fs-extra": "^11.0.4",
|
|
24
24
|
"chalk": "^5.6.2",
|
|
25
|
-
"cli-progress": "^3.12.0",
|
|
26
25
|
"commander": "^14.0.2",
|
|
27
26
|
"compressing": "^2.0.0",
|
|
28
27
|
"download-git-repo": "^3.0.2",
|