@itbox/cli 0.0.3 → 0.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/dist/index.js +34092 -0
- package/package.json +12 -6
- package/bin/index.js +0 -337
package/package.json
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@itbox/cli",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "前端项目模板生成工具,支持Vue、Vue+TypeScript、Nuxt、Nest等模板",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"module": "dist/index.js",
|
|
7
8
|
"bin": {
|
|
8
|
-
"itbox": "
|
|
9
|
-
"create-itbox": "
|
|
9
|
+
"itbox": "dist/index.js",
|
|
10
|
+
"create-itbox": "dist/index.js"
|
|
10
11
|
},
|
|
11
12
|
"scripts": {
|
|
12
|
-
"
|
|
13
|
+
"build": "tsc && rolldown -c",
|
|
14
|
+
"publish": "npm publish --access public"
|
|
13
15
|
},
|
|
14
16
|
"keywords": [
|
|
15
17
|
"cli",
|
|
@@ -23,7 +25,7 @@
|
|
|
23
25
|
"node": ">=14.0.0"
|
|
24
26
|
},
|
|
25
27
|
"files": [
|
|
26
|
-
"
|
|
28
|
+
"dist/",
|
|
27
29
|
"package.json"
|
|
28
30
|
],
|
|
29
31
|
"publishConfig": {
|
|
@@ -34,5 +36,9 @@
|
|
|
34
36
|
"fs-extra": "^11.3.3",
|
|
35
37
|
"inquirer": "^13.1.0",
|
|
36
38
|
"ora": "^9.0.0"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@types/fs-extra": "^11.0.4",
|
|
42
|
+
"rolldown": "1.0.0-beta.60"
|
|
37
43
|
}
|
|
38
44
|
}
|
package/bin/index.js
DELETED
|
@@ -1,337 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import { Command } from 'commander';
|
|
3
|
-
import inquirer from 'inquirer';
|
|
4
|
-
import fs from 'fs-extra';
|
|
5
|
-
import path from 'path';
|
|
6
|
-
import ora from 'ora';
|
|
7
|
-
import { execSync } from 'child_process';
|
|
8
|
-
import os from 'os';
|
|
9
|
-
import { fileURLToPath } from 'url';
|
|
10
|
-
|
|
11
|
-
const { prompt } = inquirer;
|
|
12
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
13
|
-
const __dirname = path.dirname(__filename);
|
|
14
|
-
|
|
15
|
-
// 配置文件相关
|
|
16
|
-
const CONFIG_DIR = path.join(os.homedir(), '.fe-cli');
|
|
17
|
-
const CONFIG_FILE = path.join(CONFIG_DIR, 'config.json');
|
|
18
|
-
|
|
19
|
-
// 读取配置
|
|
20
|
-
function readConfig() {
|
|
21
|
-
if (fs.existsSync(CONFIG_FILE)) {
|
|
22
|
-
try {
|
|
23
|
-
return fs.readJSONSync(CONFIG_FILE);
|
|
24
|
-
} catch (error) {
|
|
25
|
-
console.error('❌ 读取配置文件失败:', error.message);
|
|
26
|
-
return {};
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
return {};
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// 写入配置
|
|
33
|
-
function writeConfig(config) {
|
|
34
|
-
try {
|
|
35
|
-
// 确保配置目录存在
|
|
36
|
-
fs.ensureDirSync(CONFIG_DIR);
|
|
37
|
-
fs.writeJSONSync(CONFIG_FILE, config, { spaces: 2 });
|
|
38
|
-
return true;
|
|
39
|
-
} catch (error) {
|
|
40
|
-
console.error('❌ 写入配置文件失败:', error.message);
|
|
41
|
-
return false;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// 获取配置项
|
|
46
|
-
function getConfig(key) {
|
|
47
|
-
const config = readConfig();
|
|
48
|
-
return config[key];
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// 设置配置项
|
|
52
|
-
function setConfig(key, value) {
|
|
53
|
-
const config = readConfig();
|
|
54
|
-
config[key] = value;
|
|
55
|
-
return writeConfig(config);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// 创建命令行程序实例
|
|
59
|
-
const program = new Command();
|
|
60
|
-
|
|
61
|
-
// 检查git是否安装
|
|
62
|
-
function checkGit() {
|
|
63
|
-
try {
|
|
64
|
-
execSync('git --version', { stdio: 'ignore' });
|
|
65
|
-
return true;
|
|
66
|
-
} catch (error) {
|
|
67
|
-
return false;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// 检查包管理器是否安装
|
|
72
|
-
function checkPackageManager(pm) {
|
|
73
|
-
try {
|
|
74
|
-
execSync(`${pm} --version`, { stdio: 'ignore' });
|
|
75
|
-
return true;
|
|
76
|
-
} catch (error) {
|
|
77
|
-
return false;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// 获取远程仓库分支
|
|
82
|
-
function getRemoteBranches(repoUrl) {
|
|
83
|
-
try {
|
|
84
|
-
const output = execSync(`git ls-remote --heads ${repoUrl}`, { stdio: 'pipe' });
|
|
85
|
-
const branches = output.toString().split('\n').filter(line => line.trim()).map(line => {
|
|
86
|
-
const [, ref] = line.split('\t');
|
|
87
|
-
return ref.replace('refs/heads/', '');
|
|
88
|
-
});
|
|
89
|
-
return branches;
|
|
90
|
-
} catch (error) {
|
|
91
|
-
if (error.message.includes('403')) {
|
|
92
|
-
console.error('❌ 仓库访问受限 (403)');
|
|
93
|
-
console.error('请检查:');
|
|
94
|
-
console.error('1. 是否已登录Gitee');
|
|
95
|
-
console.error('2. 是否有权限访问该仓库');
|
|
96
|
-
console.error('3. 仓库是否处于公开状态');
|
|
97
|
-
} else {
|
|
98
|
-
console.error('❌ 获取远程分支失败:', error.message);
|
|
99
|
-
}
|
|
100
|
-
process.exit(1);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
let TEMPLATE_REPO = getConfig('templateRepo');
|
|
107
|
-
|
|
108
|
-
// 检查模板仓库配置
|
|
109
|
-
function checkTemplateRepoConfig() {
|
|
110
|
-
if (!TEMPLATE_REPO) {
|
|
111
|
-
console.error('❌ 模板仓库地址未配置');
|
|
112
|
-
console.error('\n请使用以下命令配置模板仓库地址:');
|
|
113
|
-
console.error(' itbox config set templateRepo <仓库地址>');
|
|
114
|
-
console.error('\n示例:');
|
|
115
|
-
console.error(' itbox config set templateRepo https://gitee.com/username/template.git');
|
|
116
|
-
console.error('\n配置完成后,重新运行命令即可。');
|
|
117
|
-
process.exit(1);
|
|
118
|
-
}
|
|
119
|
-
return TEMPLATE_REPO;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// 从package.json获取项目信息
|
|
123
|
-
const packagePath = path.resolve(__dirname, '../package.json');
|
|
124
|
-
const packageInfo = fs.readJSONSync(packagePath);
|
|
125
|
-
|
|
126
|
-
// 命令行配置
|
|
127
|
-
program
|
|
128
|
-
.name(packageInfo.name.split('/')[1]) // 使用包名的第二部分作为命令名(如@itbox/cli -> itbox)
|
|
129
|
-
.description(packageInfo.description)
|
|
130
|
-
.version(packageInfo.version);
|
|
131
|
-
|
|
132
|
-
// 列出所有可用模板
|
|
133
|
-
program
|
|
134
|
-
.command('list')
|
|
135
|
-
.description('列出所有可用的项目模板')
|
|
136
|
-
.action(() => {
|
|
137
|
-
// 检查git是否安装
|
|
138
|
-
if (!checkGit()) {
|
|
139
|
-
console.error('❌ 请先安装Git');
|
|
140
|
-
process.exit(1);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// 检查模板仓库配置
|
|
144
|
-
const templateRepo = checkTemplateRepoConfig();
|
|
145
|
-
|
|
146
|
-
// 获取远程仓库分支(模板)
|
|
147
|
-
const spinner = ora('正在获取可用模板列表...').start();
|
|
148
|
-
const branches = getRemoteBranches(templateRepo);
|
|
149
|
-
spinner.succeed('✅ 模板列表获取完成');
|
|
150
|
-
|
|
151
|
-
// 显示模板列表
|
|
152
|
-
if (branches.length === 0) {
|
|
153
|
-
console.log('❌ 当前仓库没有可用的模板');
|
|
154
|
-
} else {
|
|
155
|
-
console.log('\n🚀 可用模板列表:');
|
|
156
|
-
console.log('================');
|
|
157
|
-
branches.forEach((branch, index) => {
|
|
158
|
-
console.log(`${index + 1}. ${branch}`);
|
|
159
|
-
});
|
|
160
|
-
console.log('================');
|
|
161
|
-
console.log(`\n共 ${branches.length} 个可用模板`);
|
|
162
|
-
console.log('\n使用命令创建项目: itbox create <项目名>');
|
|
163
|
-
console.log('或使用: pnpm create itbox <项目名>');
|
|
164
|
-
}
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
program
|
|
168
|
-
.command('create <projectName>')
|
|
169
|
-
.description('创建新的项目')
|
|
170
|
-
.action((projectName) => {
|
|
171
|
-
// 检查git是否安装
|
|
172
|
-
if (!checkGit()) {
|
|
173
|
-
console.error('❌ 请先安装Git');
|
|
174
|
-
process.exit(1);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// 检查项目名是否已存在
|
|
178
|
-
if (fs.existsSync(projectName)) {
|
|
179
|
-
console.error('❌ 项目已存在');
|
|
180
|
-
process.exit(1);
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
// 检查模板仓库配置
|
|
184
|
-
const templateRepo = checkTemplateRepoConfig();
|
|
185
|
-
|
|
186
|
-
// 获取远程仓库分支
|
|
187
|
-
const spinner = ora('正在获取模板列表...').start();
|
|
188
|
-
const branches = getRemoteBranches(templateRepo);
|
|
189
|
-
spinner.succeed('✅ 模板列表获取完成');
|
|
190
|
-
|
|
191
|
-
// 如果没有分支
|
|
192
|
-
if (branches.length === 0) {
|
|
193
|
-
console.error('❌ 仓库没有可用的分支');
|
|
194
|
-
process.exit(1);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
// 选择模板(分支)
|
|
198
|
-
prompt([
|
|
199
|
-
{
|
|
200
|
-
type: 'checkbox',
|
|
201
|
-
name: 'branch',
|
|
202
|
-
message: '请选择项目模板 (空格选择,回车确认):',
|
|
203
|
-
choices: branches.map(branch => ({
|
|
204
|
-
name: branch,
|
|
205
|
-
value: branch
|
|
206
|
-
})),
|
|
207
|
-
validate: function(answer) {
|
|
208
|
-
if (answer.length !== 1) {
|
|
209
|
-
return '请选择一个模板';
|
|
210
|
-
}
|
|
211
|
-
return true;
|
|
212
|
-
}
|
|
213
|
-
},
|
|
214
|
-
{ type: 'checkbox',
|
|
215
|
-
name: 'packageManager',
|
|
216
|
-
message: '请选择包管理器 (空格选择,回车确认):',
|
|
217
|
-
choices: ['npm', 'pnpm'],
|
|
218
|
-
validate: function(answer) {
|
|
219
|
-
if (answer.length !== 1) {
|
|
220
|
-
return '请选择一个包管理器';
|
|
221
|
-
}
|
|
222
|
-
return true;
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
]).then(answers => {
|
|
226
|
-
// 检查包管理器是否安装
|
|
227
|
-
if (!checkPackageManager(answers.packageManager[0])) {
|
|
228
|
-
console.error(`❌ 请先安装 ${answers.packageManager[0]}`);
|
|
229
|
-
process.exit(1);
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
// 获取选中的分支(checkbox返回数组,取第一个元素)
|
|
233
|
-
const selectedBranch = answers.branch[0];
|
|
234
|
-
const branchSpinner = ora(`正在下载模板 (${selectedBranch})...`).start();
|
|
235
|
-
|
|
236
|
-
// 使用git命令直接克隆模板
|
|
237
|
-
try {
|
|
238
|
-
execSync(`git clone -b ${selectedBranch} ${TEMPLATE_REPO} ${projectName}`, {
|
|
239
|
-
stdio: 'inherit'
|
|
240
|
-
});
|
|
241
|
-
branchSpinner.succeed('✅ 模板下载完成');
|
|
242
|
-
|
|
243
|
-
// 安装依赖
|
|
244
|
-
const installSpinner = ora('正在安装依赖...').start();
|
|
245
|
-
try {
|
|
246
|
-
execSync(`${answers.packageManager[0]} install`, {
|
|
247
|
-
cwd: path.resolve(process.cwd(), projectName),
|
|
248
|
-
stdio: 'inherit'
|
|
249
|
-
});
|
|
250
|
-
installSpinner.succeed('✅ 依赖安装完成');
|
|
251
|
-
|
|
252
|
-
console.log('\n🎉 项目创建成功!');
|
|
253
|
-
console.log(`📁 项目目录: ${projectName}`);
|
|
254
|
-
console.log(`📦 包管理器: ${answers.packageManager[0]}`);
|
|
255
|
-
console.log(`\n接下来可以执行:`);
|
|
256
|
-
console.log(` cd ${projectName}`);
|
|
257
|
-
console.log(` ${answers.packageManager[0]} run dev\n`);
|
|
258
|
-
} catch (error) {
|
|
259
|
-
installSpinner.fail('❌ 依赖安装失败');
|
|
260
|
-
console.error('\n请尝试手动安装依赖:');
|
|
261
|
-
console.error(` cd ${projectName}`);
|
|
262
|
-
console.error(` ${answers.packageManager[0]} install\n`);
|
|
263
|
-
process.exit(1);
|
|
264
|
-
}
|
|
265
|
-
} catch (err) {
|
|
266
|
-
branchSpinner.fail('❌ 模板下载失败');
|
|
267
|
-
console.error('\n错误信息:', err.message);
|
|
268
|
-
console.error('\n请检查以下几点:');
|
|
269
|
-
console.error('1. 网络连接是否正常');
|
|
270
|
-
console.error('2. Git是否已正确安装');
|
|
271
|
-
console.error('3. 是否有权限访问该仓库');
|
|
272
|
-
process.exit(1);
|
|
273
|
-
}
|
|
274
|
-
});
|
|
275
|
-
});
|
|
276
|
-
|
|
277
|
-
// 配置命令
|
|
278
|
-
const configCommand = program.command('config');
|
|
279
|
-
|
|
280
|
-
// 设置配置
|
|
281
|
-
configCommand
|
|
282
|
-
.command('set <key> <value>')
|
|
283
|
-
.description('设置配置项')
|
|
284
|
-
.action((key, value) => {
|
|
285
|
-
if (setConfig(key, value)) {
|
|
286
|
-
console.log('✅ 配置设置成功');
|
|
287
|
-
}
|
|
288
|
-
});
|
|
289
|
-
|
|
290
|
-
// 获取配置
|
|
291
|
-
configCommand
|
|
292
|
-
.command('get <key>')
|
|
293
|
-
.description('获取配置项')
|
|
294
|
-
.action((key) => {
|
|
295
|
-
const value = getConfig(key);
|
|
296
|
-
if (value !== undefined) {
|
|
297
|
-
console.log(value);
|
|
298
|
-
} else {
|
|
299
|
-
console.log(`❌ 配置项 ${key} 不存在`);
|
|
300
|
-
}
|
|
301
|
-
});
|
|
302
|
-
|
|
303
|
-
// 删除配置
|
|
304
|
-
configCommand
|
|
305
|
-
.command('delete <key>')
|
|
306
|
-
.description('删除配置项')
|
|
307
|
-
.action((key) => {
|
|
308
|
-
const config = readConfig();
|
|
309
|
-
if (config[key] !== undefined) {
|
|
310
|
-
delete config[key];
|
|
311
|
-
if (writeConfig(config)) {
|
|
312
|
-
console.log('✅ 配置删除成功');
|
|
313
|
-
}
|
|
314
|
-
} else {
|
|
315
|
-
console.log(`❌ 配置项 ${key} 不存在`);
|
|
316
|
-
}
|
|
317
|
-
});
|
|
318
|
-
|
|
319
|
-
// 配置命令帮助
|
|
320
|
-
configCommand
|
|
321
|
-
.command('help')
|
|
322
|
-
.description('显示配置命令帮助')
|
|
323
|
-
.action(() => {
|
|
324
|
-
console.log('配置命令用法:');
|
|
325
|
-
console.log(' itbox config set <key> <value> 设置配置项');
|
|
326
|
-
console.log(' itbox config get <key> 获取配置项');
|
|
327
|
-
console.log(' itbox config delete <key> 删除配置项');
|
|
328
|
-
console.log(' itbox config help 显示帮助信息');
|
|
329
|
-
});
|
|
330
|
-
|
|
331
|
-
// 解析命令行参数
|
|
332
|
-
program.parse(process.argv);
|
|
333
|
-
|
|
334
|
-
// 如果没有传入命令,显示帮助信息
|
|
335
|
-
if (!process.argv.slice(2).length) {
|
|
336
|
-
program.outputHelp();
|
|
337
|
-
}
|