zhixuan-plugin-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/bin/index.js ADDED
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/env node
2
+
3
+ const program = require('commander');
4
+ const chalk = require('chalk');
5
+ const { version } = require('../package.json');
6
+
7
+ program
8
+ .version(version, '-v, --version')
9
+ .usage('<command> [options]');
10
+
11
+ // 注册 create 命令
12
+ program
13
+ .command('create <project-name>')
14
+ .description('创建一个新的Vue2项目')
15
+ .option('-t, --template <template>', '指定模板类型 (default/ts/multi)')
16
+ .action((projectName, options) => {
17
+ require('../commands/create')(projectName, options);
18
+ });
19
+
20
+ // 注册 config 命令
21
+ program
22
+ .command('config')
23
+ .description('管理脚手架配置')
24
+ .option('-s, --set <key> <value>', '设置配置')
25
+ .option('-g, --get <key>', '获取配置')
26
+ .option('-l, --list', '列出所有配置')
27
+ .action((options) => {
28
+ require('../commands/config')(options);
29
+ });
30
+
31
+ // 处理未知命令
32
+ program
33
+ .arguments('<command>')
34
+ .action((cmd) => {
35
+ program.outputHelp();
36
+ console.log();
37
+ console.log(chalk.red(` 未知命令 ${chalk.yellow(cmd)}`));
38
+ console.log();
39
+ });
40
+
41
+ program.parse(process.argv);
42
+
43
+ // 如果没有参数,显示帮助
44
+ if (!process.argv.slice(2).length) {
45
+ program.outputHelp();
46
+ }
@@ -0,0 +1,29 @@
1
+ const Conf = require('conf');
2
+ const chalk = require('chalk');
3
+
4
+ const config = new Conf({
5
+ projectName: 'zhixuan-plugin-cli'
6
+ });
7
+
8
+ module.exports = function (options) {
9
+ if (options.list) {
10
+ console.log(chalk.cyan('当前配置:'));
11
+ console.log(config.store);
12
+ return;
13
+ }
14
+
15
+ if (options.get) {
16
+ const value = config.get(options.get);
17
+ console.log(chalk.cyan(`${options.get}=${value}`));
18
+ return;
19
+ }
20
+
21
+ if (options.set) {
22
+ const [key, value] = options.set;
23
+ config.set(key, value);
24
+ console.log(chalk.green(`设置 ${key}=${value} 成功`));
25
+ return;
26
+ }
27
+
28
+ console.log(chalk.yellow('请指定操作参数'));
29
+ };
@@ -0,0 +1,70 @@
1
+ const chalk = require('chalk');
2
+ const inquirer = require('inquirer');
3
+ const downloadTemplate = require('../utils/download');
4
+ const fs = require('fs-extra');
5
+ const path = require('path');
6
+
7
+ module.exports = async function create(projectName, options) {
8
+ console.log();
9
+ console.log(chalk.cyan(`✨ 正在创建项目: ${projectName}`));
10
+
11
+ // 检查目录是否已存在
12
+ const targetDir = path.join(process.cwd(), projectName);
13
+ if (await fs.pathExists(targetDir)) {
14
+ const { action } = await inquirer.prompt([
15
+ {
16
+ name: 'action',
17
+ type: 'list',
18
+ message: `目标目录 ${chalk.cyan(targetDir)} 已存在,请选择操作:`,
19
+ choices: [
20
+ { name: '覆盖', value: 'overwrite' },
21
+ { name: '取消', value: false }
22
+ ]
23
+ }
24
+ ]);
25
+
26
+ if (!action) {
27
+ console.log(chalk.yellow('操作已取消'));
28
+ return;
29
+ }
30
+
31
+ if (action === 'overwrite') {
32
+ console.log(chalk.yellow(`\n移除目录 ${chalk.cyan(targetDir)} ...`));
33
+ await fs.remove(targetDir);
34
+ }
35
+ }
36
+
37
+ // 选择模板
38
+ let templateType = options.template;
39
+ if (!templateType) {
40
+ const answers = await inquirer.prompt([
41
+ {
42
+ name: 'template',
43
+ type: 'list',
44
+ message: '请选择项目模板:',
45
+ choices: [
46
+ { name: '标准模板 (h5)', value: 'default' },
47
+ { name: 'vue模板 (Vue2)', value: 'vue2' },
48
+ ]
49
+ }
50
+ ]);
51
+ templateType = answers.template;
52
+ }
53
+
54
+ try {
55
+ // 下载模板
56
+ await downloadTemplate(projectName, templateType);
57
+
58
+ console.log();
59
+ console.log(chalk.green('✅ 项目创建成功!'));
60
+ console.log();
61
+ console.log(chalk.cyan('cd ' + projectName));
62
+ console.log(chalk.cyan('npm install'));
63
+ console.log(chalk.cyan('npm run dev'));
64
+ console.log();
65
+
66
+ } catch (err) {
67
+ console.log(chalk.red('❌ 项目创建失败'));
68
+ console.log(chalk.red(err));
69
+ }
70
+ };
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "zhixuan-plugin-cli",
3
+ "version": "1.0.0",
4
+ "description": "zhixuan插件项目脚手架CLI工具",
5
+ "main": "bin/index.js",
6
+ "bin": {
7
+ "zhixuan-plugin-cli": "bin/index.js",
8
+ "zhixuan-cli": "bin/index.js",
9
+ "zx-cli": "bin/index.js"
10
+ },
11
+ "files": [
12
+ "bin/",
13
+ "commands/",
14
+ "utils/"
15
+ ],
16
+ "keywords": [
17
+ "zhixuan",
18
+ "plugin",
19
+ "cli"
20
+ ],
21
+ "author": "shituanquan",
22
+ "license": "MIT",
23
+ "dependencies": {
24
+ "chalk": "^4.1.2",
25
+ "commander": "^9.4.1",
26
+ "conf": "^10.2.0",
27
+ "download-git-repo": "^3.0.2",
28
+ "fs-extra": "^10.1.0",
29
+ "inquirer": "^8.2.5",
30
+ "ora": "^5.4.1"
31
+ },
32
+ "engines": {
33
+ "node": ">=16.18.0"
34
+ }
35
+ }
@@ -0,0 +1,69 @@
1
+ const download = require('download-git-repo');
2
+ const ora = require('ora');
3
+ const chalk = require('chalk');
4
+ const path = require('path');
5
+ const fs = require('fs-extra');
6
+
7
+ // 模板仓库配置
8
+ const templates = {
9
+ //
10
+ 'default': {
11
+ url: 'direct:https://gitee.com/zhixuan_stq/zhixuan-plugin-h5.git',
12
+ branch: 'master',
13
+ description: 'h5、css、js'
14
+ },
15
+ // Vue2基础模板
16
+ 'vue2': {
17
+ url: 'direct:https://gitee.com/zhixuan_stq/zhixuan-plugin-vue2.git',
18
+ branch: 'v_1.0.0',
19
+ description: 'Vue2-cli'
20
+ },
21
+ };
22
+
23
+ /**
24
+ * 下载模板
25
+ * @param {string} projectName 项目名称
26
+ * @param {string} templateType 模板类型
27
+ */
28
+ module.exports = async function downloadTemplate(projectName, templateType = 'default') {
29
+ const template = templates[templateType];
30
+
31
+ if (!template) {
32
+ console.log(chalk.red(`模板 ${templateType} 不存在`));
33
+ return;
34
+ }
35
+
36
+ // console.log(chalk.blue(`正在拉取模板: ${template.url}#${template.branch}`)); // 新增日志
37
+
38
+ const spinner = ora('正在拉取模板...').start();
39
+ const dest = path.join(process.cwd(), projectName);
40
+
41
+ return new Promise((resolve, reject) => {
42
+ download(
43
+ `${template.url}#${template.branch}`,
44
+ dest,
45
+ { clone: true },
46
+ async (err) => {
47
+ if (err) {
48
+ spinner.fail(chalk.red('模板拉取失败'));
49
+ console.error(chalk.red('详细错误:', err.message)); // 新增错误详情
50
+ reject(err);
51
+ return;
52
+ }
53
+
54
+ spinner.succeed(chalk.green('模板拉取成功'));
55
+
56
+ // 修改package.json中的项目名称
57
+ const pkgPath = path.join(dest, 'package.json');
58
+ if (await fs.pathExists(pkgPath)) {
59
+ const pkg = await fs.readJson(pkgPath);
60
+ pkg.name = projectName;
61
+ pkg.version = '1.0.0';
62
+ await fs.writeJson(pkgPath, pkg, { spaces: 2 });
63
+ }
64
+
65
+ resolve(dest);
66
+ }
67
+ );
68
+ });
69
+ };