my-cli-vue-template-demo2 0.0.1

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/cli.js ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env node
2
+
3
+ // bin/cli.js
4
+
5
+ const { program } = require('commander');
6
+ const { createProject } = require('../lib/create');
7
+
8
+ program
9
+ .version('1.0.0')
10
+ .command('create <project-name>')
11
+ .description('创建一个新项目')
12
+ .option('-t, --template <template>', '选择模板类型', 'default')
13
+ .action((projectName, options) => {
14
+ createProject(projectName, options);
15
+ });
16
+
17
+ program.parse(process.argv);
package/lib/create.js ADDED
@@ -0,0 +1,69 @@
1
+ // lib/create.js
2
+ const fs = require('fs-extra');
3
+ const path = require('path');
4
+ const inquirer = require('inquirer');
5
+ const chalk = require('chalk');
6
+ const ora = require('ora');
7
+
8
+ // 模拟可选的模板列表,实际可以是远程 git 地址或本地路径
9
+ const templates = [
10
+ { name: 'Vue3 + Vite', value: 'vue-vite-template' },
11
+ { name: 'React + Vite', value: 'react-vite-template' },
12
+ // 也可以是本地模板目录,比如 './templates/vue-basic'
13
+ ];
14
+
15
+
16
+ async function createProject(projectName) {
17
+ console.log(chalk.blue(`\n🚀 开始创建项目: ${projectName}`));
18
+ // 1. 选择模板
19
+ const answers = await inquirer.prompt([
20
+ {
21
+ type: 'list',
22
+ name: 'template',
23
+ message: '请选择项目模板:',
24
+ choices: templates,
25
+ },
26
+ {
27
+ type: 'input',
28
+ name: 'author',
29
+ message: '请输入作者名称(可选):',
30
+ default: '',
31
+ },
32
+ ]);
33
+ const { template, author } = answers;
34
+ // 2. 目标目录
35
+ const projectPath = path.resolve(process.cwd(), projectName);
36
+ if (fs.existsSync(projectPath)) {
37
+ console.log(chalk.red(`❌ 目录 "${projectName}" 已存在!`));
38
+ process.exit(1);
39
+ }
40
+ // 3. 开始下载模板(这里以模拟为例,真实情况可能是从 git 下载)
41
+ const spinner = ora(`📦 正在下载模板: ${template}`).start();
42
+ try {
43
+ // 如果你有自己的模板仓库,比如放在 GitHub 上:
44
+ // await download('github:yourname/your-template-repo#main', projectPath, { clone: true });
45
+ // 演示:这里只是模拟,你可以先准备一个本地模板文件夹,然后拷贝
46
+ const templatePath = path.resolve(__dirname, '../templates', template);
47
+ if (!fs.existsSync(templatePath)) {
48
+ throw new Error(`模板 ${template} 不存在!`);
49
+ }
50
+ await fs.copy(templatePath, projectPath);
51
+ spinner.succeed(`✅ 模板下载完成`);
52
+ // 4. 替换模板中的变量(可选,比如 package.json 中的 {{name}}、{{author}})
53
+ const pkgPath = path.join(projectPath, 'package.json');
54
+ if (fs.existsSync(pkgPath)) {
55
+ let pkgContent = await fs.readFile(pkgPath, 'utf-8');
56
+ pkgContent = pkgContent
57
+ .replace(/\{\{name\}\}/g, projectName)
58
+ .replace(/\{\{author\}\}/g, author || 'Anonymous');
59
+ await fs.writeFile(pkgPath, pkgContent, 'utf-8');
60
+ }
61
+ console.log(chalk.green(`🎉 项目 "${projectName}" 创建成功!`));
62
+ console.log(chalk.blue(`👉 请进入项目目录并安装依赖:\ncd ${projectName} && npm install\n`));
63
+ } catch (error) {
64
+ spinner.fail('❌ 模板下载失败');
65
+ console.error(chalk.red(`错误: ${error.message}`));
66
+ process.exit(1);
67
+ }
68
+ }
69
+ module.exports = { createProject };
package/lib/index.js ADDED
@@ -0,0 +1,5 @@
1
+ // lib/index.js
2
+ // 主入口文件,可以根据需要扩展
3
+ module.exports = {
4
+ create: require('./create')
5
+ };
@@ -0,0 +1,11 @@
1
+ // lib/utils/logger.js
2
+ const chalk = require('chalk');
3
+
4
+ const logger = {
5
+ info: (message) => console.log(chalk.blue(`ℹ ${message}`)),
6
+ success: (message) => console.log(chalk.green(`✓ ${message}`)),
7
+ warning: (message) => console.log(chalk.yellow(`⚠ ${message}`)),
8
+ error: (message) => console.log(chalk.red(`✗ ${message}`))
9
+ };
10
+
11
+ module.exports = logger;
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "my-cli-vue-template-demo2",
3
+ "version": "0.0.1",
4
+ "description": "模版测试",
5
+ "main": "index.js",
6
+ "bin": {
7
+ "my-cli-vue-demo": "bin/cli.js"
8
+ },
9
+ "directories": {
10
+ "lib": "lib"
11
+ },
12
+ "scripts": {
13
+ "test": "echo \"Error: no test specified\" && exit 1"
14
+ },
15
+ "author": "",
16
+ "license": "ISC",
17
+ "dependencies": {
18
+ "chalk": "^4.1.2",
19
+ "commander": "^8.3.0",
20
+ "fs-extra": "^10.0.0",
21
+ "inquirer": "^8.2.0",
22
+ "ora": "^5.4.1"
23
+ }
24
+ }
@@ -0,0 +1 @@
1
+ console.log('vue-模版');