@gx-design-vue/create-gx-cli 0.1.6 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -11,6 +11,12 @@ npx @gx-design-vue/create-gx-cli
11
11
  # or
12
12
  npm install -g @gx-design-vue/create-gx-cli
13
13
 
14
+ # update
15
+ npm install -g @gx-design-vue/create-gx-cli
16
+
17
+ # uninstall
18
+ npm uninstall -g @gx-design-vue/create-gx-cli
19
+
14
20
  # create project
15
21
  create-gx-cli
16
22
  # cli version
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gx-design-vue/create-gx-cli",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "license": "MIT",
5
5
  "description": "a cli to bootstrap gx project",
6
6
  "main": "src/index.js",
@@ -15,6 +15,7 @@
15
15
  "author": "gx12358",
16
16
  "dependencies": {
17
17
  "chalk": "^2.4.2",
18
+ "child_process": "^1.0.2",
18
19
  "esm": "^3.2.18",
19
20
  "execa": "^8.0.1",
20
21
  "inquirer": "^6.2.2",
@@ -23,8 +24,8 @@
23
24
  "minimist": "^1.2.7",
24
25
  "nanospinner": "^1.1.0",
25
26
  "ncp": "^2.0.0",
26
- "obtain-git-repo": "^1.0.2",
27
- "prompts": "^2.4.1"
27
+ "prompts": "^2.4.1",
28
+ "rimraf": "^3.0.2"
28
29
  },
29
30
  "files": [
30
31
  "bin/",
package/src/git.js ADDED
@@ -0,0 +1,78 @@
1
+ import { spawn } from 'child_process'
2
+ import rimraf from 'rimraf'
3
+
4
+ function git(opts) {
5
+ return opts.git || 'git'
6
+ }
7
+
8
+ function buildCloneCommand(repo, targetPath, opts) {
9
+ let args = [ 'clone' ]
10
+ const userArgs = opts.args || []
11
+
12
+ if (opts.shallow) {
13
+ if (userArgs.indexOf('--depth') >= 0) {
14
+ throw new Error('\'--depth\' cannot be specified when shallow is set to \'true\'')
15
+ }
16
+ args.push('--depth', '1')
17
+ }
18
+
19
+ if (opts.progress) {
20
+ args.push('--progress')
21
+ }
22
+
23
+ args = args.concat(userArgs)
24
+ args.push('--', repo, targetPath)
25
+
26
+ return [ git(opts), args ]
27
+ }
28
+
29
+ function buildCheckoutCommand(ref, opts) {
30
+ return [ git(opts), [ 'checkout', ref ] ]
31
+ }
32
+
33
+ export function clone(repo, targetPath, opts, onSuccess, onError) {
34
+ const [ cmd, args ] = buildCloneCommand(repo, targetPath, opts)
35
+ const proc = spawn(cmd, args)
36
+
37
+ if (opts.progress) {
38
+ proc.stderr.on('data', (evt) => {
39
+ const line = evt.toString()
40
+ if (line.match(/Receiving objects:\s+(\d+)%/)) {
41
+ opts.progress({
42
+ phase: 'receivingObjects',
43
+ percent: Number(RegExp.$1)
44
+ })
45
+ } else if (line.match(/Resolving deltas:\s+(\d+)%/)) {
46
+ opts.progress({
47
+ phase: 'resolvingDeltas',
48
+ percent: Number(RegExp.$1)
49
+ })
50
+ }
51
+ })
52
+ }
53
+
54
+ proc.on('close', (status) => {
55
+ if (status == 0) {
56
+ if (opts.checkout) {
57
+ _checkout()
58
+ } else {
59
+ rimraf(`${targetPath}/.git`, () => {})
60
+ onSuccess()
61
+ }
62
+ } else {
63
+ onError(new Error('\'git clone\' failed with status ' + status))
64
+ }
65
+ })
66
+
67
+ function _checkout() {
68
+ const [ cmd, args ] = buildCheckoutCommand(opts.checkout, opts)
69
+ const proc = spawn(cmd, args, { cwd: targetPath })
70
+ proc.on('close', function (status) {
71
+ if (status == 0) {
72
+ onSuccess()
73
+ } else {
74
+ onError(new Error('\'git checkout\' failed with status ' + status))
75
+ }
76
+ })
77
+ }
78
+ }
package/src/main.js CHANGED
@@ -1,23 +1,34 @@
1
1
  import chalk from 'chalk'
2
2
  import fs from 'fs'
3
+ import { reset } from 'kolorist'
4
+ import prompts from 'prompts'
3
5
  import { fileURLToPath } from 'url'
4
6
  import Listr from 'listr'
5
7
  import ncp from 'ncp'
6
8
  import path from 'path'
7
9
  import { promisify } from 'util'
8
10
  import { createSpinner } from 'nanospinner'
9
- import { download } from 'obtain-git-repo'
11
+ import { clone } from './git'
10
12
 
11
13
  const copy = promisify(ncp)
12
14
 
13
15
  const gitRepositoryList = [
14
16
  {
15
17
  name: 'gx-design-pro',
16
- git: 'https://gitee.com/gx12358/vue3-antd-admin.git'
18
+ children: [
19
+ {
20
+ name: 'Gitee',
21
+ url: 'https://gitee.com/gx12358/vue3-antd-admin.git',
22
+ },
23
+ {
24
+ name: 'Github',
25
+ url: 'https://github.com/gx12358/vue3-antd-admin.git',
26
+ }
27
+ ]
17
28
  },
18
29
  {
19
30
  name: 'gx-design-thin',
20
- git: 'https://gitee.com/gx12358/gx-admin-thin.git'
31
+ url: 'https://gitee.com/gx12358/gx-admin-thin.git'
21
32
  }
22
33
  ]
23
34
 
@@ -69,14 +80,30 @@ function doneLog(cwd, root) {
69
80
  console.log()
70
81
  }
71
82
 
83
+ function getProjectName(projectName) {
84
+ return projectName === '.' ? path.basename(path.resolve()) : projectName
85
+ }
86
+
87
+ function download(url, root, projectName) {
88
+ const spinner = createSpinner(`开始下载...${url}`).start()
89
+ // 下载git代码
90
+ clone(`${url}`, root, { clone: true }, async () => {
91
+ await editPackageJson(root, projectName || getProjectName(projectName))
92
+ spinner.success({
93
+ text: '项目创建成功,请依次执行以下命令'
94
+ })
95
+ doneLog(process.cwd(), root)
96
+ },async function (err) {
97
+ console.log(err)
98
+ spinner.error({ text: '下载失败' })
99
+ })
100
+ }
101
+
72
102
  export async function createProject(options) {
73
103
  const cwd = process.cwd()
74
104
 
75
105
  const root = path.join(cwd, options.projectName)
76
106
 
77
- const getProjectName = () =>
78
- options.projectName === '.' ? path.basename(path.resolve()) : options.projectName
79
-
80
107
  const downloadGit = gitRepositoryList.find(el => el.name === options.template)
81
108
 
82
109
  const templateDir = path.resolve(
@@ -103,19 +130,25 @@ export async function createProject(options) {
103
130
  }
104
131
 
105
132
  if (downloadGit) {
106
- const spinner = createSpinner(`开始下载...${downloadGit.git}`).start()
107
- // 下载git代码
108
- download(`direct:${downloadGit.git}`, root, { clone: true }, async function (err) {
109
- if (err) {
110
- spinner.error({ text: '下载失败' })
111
- } else {
112
- await editPackageJson(root, options.projectName || getProjectName())
113
- spinner.success({
114
- text: '项目创建成功,请依次执行以下命令'
115
- })
116
- fs.doneLog(cwd, root)
117
- }
118
- })
133
+ if (downloadGit.children && downloadGit.children.length > 0) {
134
+ prompts([
135
+ {
136
+ type: 'select',
137
+ name: 'url',
138
+ message: reset('Select a Url:'),
139
+ choices: downloadGit.children.map((item) => {
140
+ return {
141
+ title: item.name,
142
+ value: item.url
143
+ }
144
+ })
145
+ }
146
+ ]).then(answers => {
147
+ if (answers.url) download(answers.url, root, options.projectName)
148
+ })
149
+ } else if (downloadGit.url) {
150
+ download(downloadGit.url, root, options.projectName)
151
+ }
119
152
  } else {
120
153
  const tasks = new Listr(
121
154
  [
@@ -125,7 +158,7 @@ export async function createProject(options) {
125
158
  },
126
159
  options.template !== 'mobile-vant-html' ? {
127
160
  title: '初始化项目',
128
- task: () => editPackageJson(root, options.projectName || getProjectName())
161
+ task: () => editPackageJson(root, options.projectName || getProjectName(options.projectName))
129
162
  } : {}
130
163
  ],
131
164
  {
@@ -54,4 +54,4 @@
54
54
  "vite-plugin-vue-setup-extend": "^0.4.0",
55
55
  "vue-tsc": "^1.8.27"
56
56
  }
57
- }
57
+ }