lzx-test-cli 1.0.2 → 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 +22 -25
- package/package.json +1 -2
- package/bin/test.js +0 -8
package/bin/index.js
CHANGED
|
@@ -10,8 +10,6 @@ import ora from 'ora';
|
|
|
10
10
|
import compressing from 'compressing'; //支持- tar - gzip - tgz - zip
|
|
11
11
|
import { spawn } from 'child_process';
|
|
12
12
|
import fetch from 'node-fetch';
|
|
13
|
-
import cliProgress from 'cli-progress';
|
|
14
|
-
import { log } from 'console';
|
|
15
13
|
// import download from 'download-git-repo';
|
|
16
14
|
console.log(
|
|
17
15
|
chalk.green('开始创建项目!')
|
|
@@ -59,7 +57,7 @@ function runCommand(cwd) {
|
|
|
59
57
|
cwd: safeCwd,
|
|
60
58
|
stdio: 'inherit'
|
|
61
59
|
});
|
|
62
|
-
console.log(chalk.green("\n
|
|
60
|
+
console.log(chalk.green("\n开始安装依赖,请耐心等待..."));
|
|
63
61
|
child.on('error', reject);
|
|
64
62
|
child.on('close', code => {
|
|
65
63
|
if (code !== 0) {
|
|
@@ -72,41 +70,43 @@ function runCommand(cwd) {
|
|
|
72
70
|
});
|
|
73
71
|
}
|
|
74
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
|
+
|
|
75
83
|
/**
|
|
76
84
|
* @param {string} url 下载链接
|
|
77
85
|
* @param {string} targetDir 目标文件夹
|
|
78
86
|
* @param {string} fileName 保存的文件名
|
|
79
87
|
* @description: 下载文件并显示进度条
|
|
80
88
|
*/
|
|
81
|
-
export async function downloadFileToFolder(url, targetDir, fileName) {
|
|
89
|
+
export async function downloadFileToFolder(url, targetDir, fileName, headers = {}) {
|
|
82
90
|
// 目标文件夹确保存在
|
|
83
91
|
await fs.promises.mkdir(targetDir, { recursive: true });
|
|
84
92
|
|
|
85
93
|
const dest = path.join(targetDir, fileName);
|
|
86
94
|
|
|
87
95
|
const res = await fetch(url, {
|
|
88
|
-
headers
|
|
89
|
-
'User-Agent': 'node.js',
|
|
90
|
-
'Accept': 'application/octet-stream'
|
|
91
|
-
// 'Authorization': `token ${YOUR_TOKEN}`
|
|
92
|
-
},
|
|
96
|
+
headers,
|
|
93
97
|
redirect: 'follow', // 跟随重定向
|
|
94
98
|
});
|
|
95
99
|
if (!res.ok) throw new Error(`下载失败: ${res.status} ${res.statusText}`);
|
|
96
100
|
|
|
97
101
|
// 获取 content-length 用于进度条(如果服务器提供)
|
|
98
102
|
const total = +res.headers.get('content-length') || 0;
|
|
99
|
-
const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
|
|
100
|
-
if (total > 0) progressBar.start(total, 0);
|
|
101
|
-
|
|
102
103
|
const fileStream = fs.createWriteStream(dest);
|
|
103
|
-
|
|
104
|
-
|
|
105
104
|
let downloaded = 0;
|
|
105
|
+
|
|
106
106
|
// 监听 data 更新进度
|
|
107
107
|
res.body.on('data', chunk => {
|
|
108
108
|
downloaded += chunk.length;
|
|
109
|
-
if (total > 0)
|
|
109
|
+
if (total > 0) showPercent(downloaded, total);
|
|
110
110
|
});
|
|
111
111
|
|
|
112
112
|
await new Promise((resolve, reject) => {
|
|
@@ -194,7 +194,7 @@ program
|
|
|
194
194
|
{ name: "本地", value: "local" },
|
|
195
195
|
{ name: "远程", value: "remote" }
|
|
196
196
|
],
|
|
197
|
-
default: '
|
|
197
|
+
default: 'remote',
|
|
198
198
|
},
|
|
199
199
|
]);
|
|
200
200
|
let { templateChosen } = await inquirer.prompt([
|
|
@@ -220,7 +220,9 @@ program
|
|
|
220
220
|
message: '是否强制覆盖',
|
|
221
221
|
},
|
|
222
222
|
]);
|
|
223
|
-
const syncTemplate = ora(
|
|
223
|
+
const syncTemplate = ora({
|
|
224
|
+
text: chalk.yellow('项目创建中...'),
|
|
225
|
+
})
|
|
224
226
|
syncTemplate.start()
|
|
225
227
|
const cwd = process.cwd();
|
|
226
228
|
const targetDir = path.resolve(cwd, projectName);
|
|
@@ -241,26 +243,21 @@ program
|
|
|
241
243
|
const headers = {
|
|
242
244
|
'User-Agent': 'node.js',
|
|
243
245
|
'Accept': 'application/octet-stream',
|
|
246
|
+
// 'Authorization': `token ${YOUR_TOKEN}`
|
|
244
247
|
}
|
|
245
248
|
const fileName = getFileNameFromUrl(repoUrl);
|
|
246
249
|
await downloadFileToFolder(repoUrl, targetDir, fileName, headers);
|
|
247
250
|
}
|
|
248
251
|
} else{
|
|
249
|
-
// const templatePath = path.resolve("templates", templateChosen); //不能这样使用,这是当前cli工具执行时的工作目录
|
|
250
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
252
|
+
// const templatePath = path.resolve("templates", templateChosen); //不能这样使用,这是当前cli工具执行时的工作目录
|
|
253
|
+
const __filename = fileURLToPath(import.meta.url); //由于使用esm, 需要使用__dirname读取模块相关的文件(不受执行位置影响)
|
|
251
254
|
const __dirname = path.dirname(__filename);
|
|
252
|
-
console.log("🚀 ~ :252 ~ __dirname:", __dirname)
|
|
253
255
|
const templatesDir = path.join(__dirname, '../', templateChosen);
|
|
254
|
-
console.log("🚀 ~ :254 ~ templatesDir:", templatesDir)
|
|
255
|
-
|
|
256
256
|
await fs.copySync(templatesDir, targetDir)
|
|
257
257
|
}
|
|
258
258
|
console.log(chalk.green("\n同步模版成功,开始解压并安装依赖..."));
|
|
259
259
|
await unCompressFileAndInstall(targetDir);
|
|
260
260
|
syncTemplate.succeed(chalk.green(chalk.blue.underline.bold(projectName) + ' 项目创建成功!'))
|
|
261
|
-
// console.log(
|
|
262
|
-
|
|
263
|
-
// );
|
|
264
261
|
// 退出
|
|
265
262
|
process.stdin.pause();
|
|
266
263
|
process.exit(0); // 完成后正常退出
|
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",
|