hr-optimus-cli 1.0.0 → 1.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.
Files changed (2) hide show
  1. package/index.js +62 -44
  2. package/package.json +5 -1
package/index.js CHANGED
@@ -1,35 +1,45 @@
1
- import { command } from 'commander'; //设计命令行
2
- import download from 'download-git-repo'; //github仓库下载
3
- import { prompt } from 'inquirer'; //命令行答询
4
- import { compile } from 'handlebars'; //修改字符
5
- import ora from 'ora'; //命令行中加载状态标识
6
- import { blue, yellow, red, gray, magentaBright, cyan, green } from 'chalk'; //命令行输出字符颜色
7
- import { error } from 'log-symbols'; //命令行输出符号
1
+ #!/usr/bin/env node
8
2
 
3
+ import { Command } from 'commander';
4
+ import download from 'download-git-repo';
5
+ // 修改 inquirer 的导入方式
6
+ import inquirer from 'inquirer';
7
+ import Handlebars from 'handlebars';
8
+ import ora from 'ora';
9
+ import chalk from 'chalk';
10
+ import logSymbols from 'log-symbols';
9
11
  import { readFileSync, writeFileSync } from 'fs';
10
- import { version as _version } from './package.json';
12
+
13
+ // 动态导入 package.json
14
+ let _version;
15
+ try {
16
+ const packageJson = JSON.parse(readFileSync('./package.json', 'utf8'));
17
+ _version = packageJson.version;
18
+ } catch (err) {
19
+ console.error('无法读取 package.json:', err);
20
+ process.exit(1);
21
+ }
22
+
23
+ const program = new Command();
11
24
 
12
25
  const version = `hr-optimus-cli cli v${_version}`;
13
26
 
14
27
  const templates = {
15
28
  'v3e': {
16
29
  downloadUrl: 'https://git.easyzf.com/root/optimus-cli-template.git'
17
- },
18
- // 'v3a': {
19
- // downloadUrl: 'github:username/v3a#main'
20
- // }
30
+ }
21
31
  };
22
32
 
23
- // create <project>
24
- command('create <project>')
33
+ program
34
+ .command('create <project>')
35
+ .option('-f, --force', '强制覆盖本地同名项目')
25
36
  .description('创建项目')
26
- .action((projectName) => {
27
- //命令行答询
37
+ .action((projectName, options) => {
28
38
  console.clear();
29
- console.log(blue(version));
39
+ console.log(chalk.blue(version));
30
40
  console.log('');
31
41
 
32
- prompt([{
42
+ inquirer.prompt([{
33
43
  type: 'input',
34
44
  name: 'name',
35
45
  message: '请输入项目名称',
@@ -39,38 +49,46 @@ command('create <project>')
39
49
  name: 'templateName',
40
50
  message: '请选择项目模版',
41
51
  choices: [
42
- `v3e (${yellow('脚手架 Optimus-Cli-Template')})`,
43
- // `v3a (${yellow('Vue3 Antd Admin')})`
52
+ `v3e (${chalk.yellow('脚手架 Optimus-Cli-Template')})`
44
53
  ],
45
54
  filter: function (val) {
46
55
  return val.substr(0, 3);
47
56
  }
48
- }
49
- ]).then(answers => {
57
+ }])
58
+ .then(answers => {
59
+ answers.name = projectName;
50
60
 
51
- const downloadUrl = templates[answers.templateName].downloadUrl;
52
- //下载github项目,下载墙loading提示
53
- console.log('');
54
- const spinner = ora('正在下载模板...').start();
61
+ const downloadUrl = templates[answers.templateName].downloadUrl;
62
+ console.log('');
63
+ const spinner = ora('正在下载模板...').start();
55
64
 
56
- //第一个参数是github仓库地址,第二个参数是创建的项目目录名,第三个参数是clone
57
- download(downloadUrl, projectName, { clone: true }, err => {
58
- if (err) {
59
- console.log(error, red(err));
60
- } else {
61
- spinner.succeed('项目模板下载成功');
62
- console.log('');
63
- //根据命令行答询结果修改package.json文件
64
- let packageContent = readFileSync(`${projectName}/package.json`, 'utf8');
65
- let packageResult = compile(packageContent)(answers);
66
- writeFileSync(`${projectName}/package.json`, packageResult);
65
+ download(downloadUrl, projectName, { clone: true }, err => {
66
+ if (err) {
67
+ console.log(logSymbols.error, chalk.red(err));
68
+ } else {
69
+ spinner.succeed('项目模板下载成功');
70
+ console.log('');
67
71
 
68
- console.log(' ', gray('$'), yellow(`cd ${projectName}`));
69
- console.log(' ', gray('$'), magentaBright('pnpm i'));
70
- console.log(' ', gray('$'), cyan('pnpm dev'));
71
- console.log(' ', gray('$'), green('pnpm build'));
72
- console.log('');
73
- }
72
+ try {
73
+ let packageContent = readFileSync(`${projectName}/package.json`, 'utf8');
74
+ let packageResult = Handlebars.compile(packageContent)(answers);
75
+ writeFileSync(`${projectName}/package.json`, packageResult);
76
+
77
+ console.log(' ', chalk.gray('$'), chalk.yellow(`cd ${projectName}`));
78
+ console.log(' ', chalk.gray('$'), chalk.magentaBright('pnpm i'));
79
+ console.log(' ', chalk.gray('$'), chalk.cyan('pnpm dev'));
80
+ console.log(' ', chalk.gray('$'), chalk.green('pnpm build'));
81
+ console.log('');
82
+ } catch (readErr) {
83
+ console.log(logSymbols.error, chalk.red('更新 package.json 时出错:', readErr.message));
84
+ }
85
+ }
86
+ });
74
87
  })
75
- })
88
+ .catch(promptErr => {
89
+ console.log(logSymbols.error, chalk.red('命令行交互出错:', promptErr.message));
90
+ });
76
91
  });
92
+
93
+ program.version(_version);
94
+ program.parse(process.argv);
package/package.json CHANGED
@@ -1,8 +1,12 @@
1
1
  {
2
2
  "name": "hr-optimus-cli",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
+ "type": "module",
7
+ "scripts": {
8
+ "hr-optimus-cli": "index.js"
9
+ },
6
10
  "bin": {
7
11
  "hr-optimus-cli": "index.js"
8
12
  },