abao-cli 1.0.0 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
package/bin/cli.js CHANGED
@@ -12,12 +12,19 @@ const { del } = require("../commands/del")
12
12
 
13
13
  const { init } = require("../commands/init")
14
14
 
15
+ const fs = require("fs")
16
+
17
+ const path = require("path")
18
+
19
+ const chalk = require("chalk")
20
+
15
21
  program.version(packageConfig.version).option("-v, --version")
16
22
 
17
23
  program.name("my-command").usage("可执行命令")
18
24
 
19
25
  program
20
- .command("ls")
26
+ .command("list")
27
+ .alias("ls")
21
28
  .description("查看模板列表")
22
29
  .action(() => {
23
30
  showList()
@@ -38,10 +45,18 @@ program
38
45
  })
39
46
 
40
47
  program
41
- .command("init")
48
+ .command("init <projectName> [项目名称]")
42
49
  .description("初始化模板")
43
- .action(() => {
44
- init()
50
+ .action((projectName) => {
51
+ const cwd = process.cwd()
52
+ const fullPath = path.join(cwd, projectName)
53
+ if (fs.existsSync(fullPath)) {
54
+ console.log("\n")
55
+ console.log(chalk.yellow("警告:"), "目录已存在,请重新输入")
56
+ console.log("\n")
57
+ } else {
58
+ init(cwd, projectName)
59
+ }
45
60
  })
46
61
 
47
62
  program.parse()
package/commands/del.js CHANGED
@@ -8,7 +8,7 @@ exports.del = async () => {
8
8
  const tempJson = await readFile()
9
9
  const nameArray = (tempJson || []).map(item => item.name)
10
10
 
11
- if (nameArray.length === 0) return console.log("不存在可以删除的模板")
11
+ if (nameArray.length === 0) return console.log("模板已为空,无需删除")
12
12
 
13
13
  const question = [
14
14
  {
package/commands/init.js CHANGED
@@ -1,18 +1,31 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- const path = require("path")
4
- const ora = require("ora")
5
3
  const inquirer = require("inquirer")
6
- const download = require("download-git-repo")
7
4
  const { readFile } = require("../utils/file")
8
- // 定义一个loading
9
- const spinner = ora("Generate ing...");
5
+ const path = require("path")
6
+ const download = require("download-git-repo")
7
+ const shell = require("shelljs")
8
+ const chalk = require("chalk")
9
+ const ora = require("ora")
10
+ const spinner = ora("Generate ing...")
10
11
 
11
- exports.init = async () => {
12
+ const gitRegExp = /^http.*\.git$/
13
+
14
+ exports.init = async (cwd, projectName) => {
12
15
  const tempJson = await readFile()
13
16
  const nameArray = (tempJson || []).map(item => item.name)
14
17
 
15
18
  const question = [
19
+ {
20
+ name: "git_origin",
21
+ type: "input",
22
+ message: "请输入git源地址",
23
+ validate(val) {
24
+ if (val === '') return 'git源地址必填'
25
+ else if (gitRegExp.test(val)) return true
26
+ else return '这不是一个有效的地址'
27
+ }
28
+ },
16
29
  {
17
30
  name: "name",
18
31
  type: "list",
@@ -22,22 +35,36 @@ exports.init = async () => {
22
35
  ]
23
36
 
24
37
  inquirer.prompt(question).then(async answer => {
25
- const { name } = answer
38
+ const { name, git_origin } = answer
26
39
  const { git_url, branch } = tempJson.find(item => item.name === name)
27
-
28
- console.log('\n');
29
- // console.log(`direct:${git_url}#${branch}`);
30
- spinner.start();
31
- download(`direct:${git_url}#${branch}`, path.join(__dirname, '../test'), { clone: true }, err => {
40
+ spinner.start()
41
+ download(`direct:${git_url}#${branch}`, path.join(cwd, projectName), { clone: true }, err => {
42
+ spinner.stop()
32
43
  if (err) {
33
- console.log(err.message);
34
- console.log('\n');
44
+ console.log(err.message)
45
+ console.log('\n')
35
46
  // loading 失败
36
47
  spinner.fail("Generate failed");
37
48
  } else {
38
- console.log('\n');
49
+ console.log('\n')
39
50
  // loading 成功
40
- spinner.succeed("Generate success");
51
+ spinner.succeed("Generate success")
52
+
53
+ console.log("\n")
54
+ //判定git命令是否可用
55
+ if (!shell.which('git')) {
56
+ //向命令行打印git命令不可用的提示信息
57
+ shell.echo('抱歉, git命令不可用')
58
+ //退出当前进程
59
+ shell.exit(1)
60
+ } else {
61
+ shell.cd(projectName)
62
+ shell.exec(`git init`)
63
+ shell.exec(`git remote add origin ${git_origin}`)
64
+ console.log("\n")
65
+ console.log(chalk.white("git 初始化成功:"), chalk.blue(git_origin))
66
+ console.log("\n")
67
+ }
41
68
  }
42
69
  })
43
70
  })
package/package.json CHANGED
@@ -1,19 +1,25 @@
1
1
  {
2
2
  "name": "abao-cli",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "",
5
5
  "main": "index.js",
6
- "bin": "./index.js",
6
+ "bin": {
7
+ "abao-cli": "./bin/cli.js"
8
+ },
7
9
  "scripts": {
8
- "publish": "npm publish"
10
+ "buildPublish": "npm publish"
9
11
  },
10
- "keywords": [],
11
- "author": "",
12
+ "keywords": [
13
+ "abao-cli"
14
+ ],
15
+ "author": "bao",
12
16
  "license": "ISC",
13
17
  "dependencies": {
18
+ "chalk": "4.1.2",
14
19
  "cli-table": "0.3.11",
15
20
  "commander": "10.0.0",
16
21
  "download-git-repo": "3.0.2",
17
- "inquirer": "8.2.5"
22
+ "inquirer": "8.2.5",
23
+ "shelljs": "0.8.5"
18
24
  }
19
- }
25
+ }
package/utils/file.js CHANGED
@@ -1,3 +1,5 @@
1
+ #!/usr/bin/env node
2
+
1
3
  const path = require("path")
2
4
  const fs = require("fs")
3
5
 
package/utils/temp.json CHANGED
@@ -0,0 +1 @@
1
+ []