lzx-test-cli 1.0.2 → 1.0.4
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 +41 -31
- package/package.json +2 -3
- package/bin/test.js +0 -8
package/bin/index.js
CHANGED
|
@@ -10,12 +10,10 @@ 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
|
-
|
|
14
|
-
import {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
chalk.green('开始创建项目!')
|
|
18
|
-
);
|
|
13
|
+
|
|
14
|
+
import { createRequire } from 'node:module';
|
|
15
|
+
const require = createRequire(import.meta.url);
|
|
16
|
+
const pkg = require('../package.json');
|
|
19
17
|
|
|
20
18
|
/**
|
|
21
19
|
*
|
|
@@ -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
|
-
* @description:
|
|
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) => {
|
|
@@ -156,6 +156,7 @@ const main_proj_remote_url = 'http://10.14.121.116:8081/repository/npm-hosted/ar
|
|
|
156
156
|
const sub_proj_remote_url = 'http://10.14.121.116:8081/repository/npm-hosted/artery5-manager-platform/-/artery5-manager-platform-1.0.2.tgz';
|
|
157
157
|
|
|
158
158
|
const program = new Command();
|
|
159
|
+
|
|
159
160
|
program
|
|
160
161
|
.command('create')
|
|
161
162
|
.alias('init')
|
|
@@ -164,6 +165,9 @@ program
|
|
|
164
165
|
// .option('-f, --force', '强制覆盖,如果文件存在则强制覆盖')
|
|
165
166
|
// .option('-l, --local-path <path>', '从本地下载模版创建')
|
|
166
167
|
.action(async () => {
|
|
168
|
+
console.log(
|
|
169
|
+
chalk.green('开始创建项目!')
|
|
170
|
+
);
|
|
167
171
|
try {
|
|
168
172
|
let { projectName } = await inquirer.prompt([
|
|
169
173
|
{
|
|
@@ -194,7 +198,7 @@ program
|
|
|
194
198
|
{ name: "本地", value: "local" },
|
|
195
199
|
{ name: "远程", value: "remote" }
|
|
196
200
|
],
|
|
197
|
-
default: '
|
|
201
|
+
default: 'remote',
|
|
198
202
|
},
|
|
199
203
|
]);
|
|
200
204
|
let { templateChosen } = await inquirer.prompt([
|
|
@@ -220,7 +224,9 @@ program
|
|
|
220
224
|
message: '是否强制覆盖',
|
|
221
225
|
},
|
|
222
226
|
]);
|
|
223
|
-
const syncTemplate = ora(
|
|
227
|
+
const syncTemplate = ora({
|
|
228
|
+
text: chalk.yellow('项目创建中...'),
|
|
229
|
+
})
|
|
224
230
|
syncTemplate.start()
|
|
225
231
|
const cwd = process.cwd();
|
|
226
232
|
const targetDir = path.resolve(cwd, projectName);
|
|
@@ -241,26 +247,21 @@ program
|
|
|
241
247
|
const headers = {
|
|
242
248
|
'User-Agent': 'node.js',
|
|
243
249
|
'Accept': 'application/octet-stream',
|
|
250
|
+
// 'Authorization': `token ${YOUR_TOKEN}`
|
|
244
251
|
}
|
|
245
252
|
const fileName = getFileNameFromUrl(repoUrl);
|
|
246
253
|
await downloadFileToFolder(repoUrl, targetDir, fileName, headers);
|
|
247
254
|
}
|
|
248
255
|
} else{
|
|
249
|
-
// const templatePath = path.resolve("templates", templateChosen); //不能这样使用,这是当前cli工具执行时的工作目录
|
|
250
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
256
|
+
// const templatePath = path.resolve("templates", templateChosen); //不能这样使用,这是当前cli工具执行时的工作目录
|
|
257
|
+
const __filename = fileURLToPath(import.meta.url); //由于使用esm, 需要使用__dirname读取模块相关的文件(不受执行位置影响)
|
|
251
258
|
const __dirname = path.dirname(__filename);
|
|
252
|
-
console.log("🚀 ~ :252 ~ __dirname:", __dirname)
|
|
253
259
|
const templatesDir = path.join(__dirname, '../', templateChosen);
|
|
254
|
-
console.log("🚀 ~ :254 ~ templatesDir:", templatesDir)
|
|
255
|
-
|
|
256
260
|
await fs.copySync(templatesDir, targetDir)
|
|
257
261
|
}
|
|
258
262
|
console.log(chalk.green("\n同步模版成功,开始解压并安装依赖..."));
|
|
259
263
|
await unCompressFileAndInstall(targetDir);
|
|
260
264
|
syncTemplate.succeed(chalk.green(chalk.blue.underline.bold(projectName) + ' 项目创建成功!'))
|
|
261
|
-
// console.log(
|
|
262
|
-
|
|
263
|
-
// );
|
|
264
265
|
// 退出
|
|
265
266
|
process.stdin.pause();
|
|
266
267
|
process.exit(0); // 完成后正常退出
|
|
@@ -275,10 +276,19 @@ program
|
|
|
275
276
|
}
|
|
276
277
|
})
|
|
277
278
|
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
program
|
|
282
|
+
.name(pkg.name)
|
|
283
|
+
.description(pkg.description || 'CLI 工具')
|
|
284
|
+
.version(pkg.version);
|
|
285
|
+
|
|
278
286
|
program.on('--help', function () {
|
|
279
287
|
console.log('\nExamples:')
|
|
280
288
|
console.log(`artery-cli create`)
|
|
281
289
|
})
|
|
282
|
-
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
program.parse(process.argv);
|
|
283
293
|
|
|
284
294
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lzx-test-cli",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"bin": {
|
|
5
5
|
"lzx-test-cli": "bin/index.js"
|
|
6
6
|
},
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"keywords": [],
|
|
14
14
|
"author": "",
|
|
15
15
|
"license": "ISC",
|
|
16
|
-
"description": "",
|
|
16
|
+
"description": "示例cli工具",
|
|
17
17
|
"devDependencies": {
|
|
18
18
|
"@types/node": "^25.0.6",
|
|
19
19
|
"ts-node": "^10.9.2",
|
|
@@ -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",
|