yk-temp-cli 1.0.0
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/index.js +79 -0
- package/package.json +20 -0
- package/templates.json +6 -0
package/index.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const inquirer = require('inquirer').default ?? require('inquirer');
|
|
4
|
+
const chalk = require('chalk');
|
|
5
|
+
const { execSync } = require('child_process');
|
|
6
|
+
const path = require('path');
|
|
7
|
+
const fs = require('fs');
|
|
8
|
+
|
|
9
|
+
const TEMPLATES_FILE = path.join(__dirname, 'templates.json');
|
|
10
|
+
|
|
11
|
+
function loadTemplates() {
|
|
12
|
+
try {
|
|
13
|
+
const content = fs.readFileSync(TEMPLATES_FILE, 'utf-8');
|
|
14
|
+
const templates = JSON.parse(content);
|
|
15
|
+
if (!Array.isArray(templates) || templates.length === 0) {
|
|
16
|
+
console.error(chalk.red('错误: 模板列表为空,请在 templates.json 中添加模板'));
|
|
17
|
+
process.exit(1);
|
|
18
|
+
}
|
|
19
|
+
return templates;
|
|
20
|
+
} catch (err) {
|
|
21
|
+
if (err.code === 'ENOENT') {
|
|
22
|
+
console.error(chalk.red('错误: 找不到 templates.json 文件'));
|
|
23
|
+
} else {
|
|
24
|
+
console.error(chalk.red('错误: 无法解析 templates.json -', err.message));
|
|
25
|
+
}
|
|
26
|
+
process.exit(1);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function main() {
|
|
31
|
+
console.log(chalk.cyan.bold('\n📦 项目模板创建工具\n'));
|
|
32
|
+
|
|
33
|
+
const templates = loadTemplates();
|
|
34
|
+
|
|
35
|
+
const answers = await inquirer.prompt([
|
|
36
|
+
{
|
|
37
|
+
type: 'input',
|
|
38
|
+
name: 'projectName',
|
|
39
|
+
message: '请输入项目名称:',
|
|
40
|
+
validate: (input) => {
|
|
41
|
+
if (!input.trim()) return '项目名称不能为空';
|
|
42
|
+
if (/[<>:"/\\|?*]/.test(input)) return '项目名称包含非法字符';
|
|
43
|
+
return true;
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
type: 'list',
|
|
48
|
+
name: 'template',
|
|
49
|
+
message: '请选择模板:',
|
|
50
|
+
choices: templates.map((t) => ({
|
|
51
|
+
name: `${t.name} (${t.url})`,
|
|
52
|
+
value: t,
|
|
53
|
+
})),
|
|
54
|
+
},
|
|
55
|
+
]);
|
|
56
|
+
|
|
57
|
+
const { projectName, template } = answers;
|
|
58
|
+
const targetDir = path.resolve(process.cwd(), projectName.trim());
|
|
59
|
+
|
|
60
|
+
if (fs.existsSync(targetDir)) {
|
|
61
|
+
console.error(chalk.red(`\n错误: 目录 "${projectName}" 已存在`));
|
|
62
|
+
process.exit(1);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
console.log(chalk.gray(`\n正在从 ${template.name} 拉取模板...`));
|
|
66
|
+
|
|
67
|
+
try {
|
|
68
|
+
execSync(`git clone "${template.url}" "${targetDir}"`, {
|
|
69
|
+
stdio: 'inherit',
|
|
70
|
+
});
|
|
71
|
+
console.log(chalk.green(`\n✅ 项目 "${projectName}" 创建成功!`));
|
|
72
|
+
console.log(chalk.gray(` 目录: ${targetDir}`));
|
|
73
|
+
} catch (err) {
|
|
74
|
+
console.error(chalk.red('\n拉取失败,请检查网络连接和仓库地址是否正确'));
|
|
75
|
+
process.exit(1);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
main();
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "yk-temp-cli",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "CLI to create project from template repository",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"temp-cli": "./index.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"start": "node index.js",
|
|
11
|
+
"create": "node index.js"
|
|
12
|
+
},
|
|
13
|
+
"keywords": ["cli", "template", "scaffold"],
|
|
14
|
+
"author": "",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"inquirer": "^9.2.12",
|
|
18
|
+
"chalk": "^4.1.2"
|
|
19
|
+
}
|
|
20
|
+
}
|