cmyr-template-cli 1.0.2 → 1.3.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/dist/index.js CHANGED
@@ -19,7 +19,7 @@ const argv = minimist__default["default"](args);
19
19
  program.option('-d, --debug', 'debug');
20
20
  const create = new commander.Command('create')
21
21
  .description('创建项目')
22
- .action((_source, _destination) => {
22
+ .action(() => {
23
23
  plop.Plop.launch({
24
24
  cwd: argv.cwd,
25
25
  configPath: path__default["default"].resolve(__dirname, './plopfile.js'),
package/dist/plopfile.js CHANGED
@@ -5,7 +5,9 @@ var path = require('path');
5
5
  var fs = require('fs-extra');
6
6
  var ora = require('ora');
7
7
  var download = require('download-git-repo');
8
+ var axios = require('axios');
8
9
  var child_process = require('child_process');
10
+ var any = require('promise.any');
9
11
 
10
12
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
13
 
@@ -13,16 +15,27 @@ var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
13
15
  var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
14
16
  var ora__default = /*#__PURE__*/_interopDefaultLegacy(ora);
15
17
  var download__default = /*#__PURE__*/_interopDefaultLegacy(download);
18
+ var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);
19
+ var any__default = /*#__PURE__*/_interopDefaultLegacy(any);
16
20
 
17
- const env = process.env;
18
- env.NODE_ENV;
19
- const __DEV__ = env.NODE_ENV === 'development';
21
+ process.env;
22
+ const PACKAGE_MANAGER = 'pnpm';
20
23
 
24
+ if (!Promise.any) {
25
+ Promise.any = any__default["default"];
26
+ }
27
+ const REMOTES = [
28
+ 'https://github.com',
29
+ 'https://hub.fastgit.org',
30
+ 'https://gitclone.com',
31
+ 'https://github.com.cnpmjs.org',
32
+ ];
21
33
  async function downloadGitRepo(repository, destination, options = {}) {
34
+ const fastRepo = await getFastGitRepo(repository);
22
35
  const loading = ora__default["default"](`download - ${repository}`);
23
36
  loading.start();
24
37
  return new Promise((resolve, reject) => {
25
- download__default["default"](repository, destination, options, (err) => {
38
+ download__default["default"](fastRepo, destination, options, (err) => {
26
39
  loading.stop();
27
40
  if (err) {
28
41
  return reject(err);
@@ -46,12 +59,11 @@ async function asyncExec(cmd, options) {
46
59
  }
47
60
  async function init(projectPath, pkgData) {
48
61
  const loading = ora__default["default"]('正在安装依赖……');
49
- loading.start();
50
62
  try {
51
63
  await asyncExec('git --version', {
52
64
  cwd: projectPath,
53
65
  });
54
- await asyncExec('npm -v', {
66
+ await asyncExec(`${PACKAGE_MANAGER} -v`, {
55
67
  cwd: projectPath,
56
68
  });
57
69
  await asyncExec('git init', {
@@ -67,7 +79,8 @@ async function init(projectPath, pkgData) {
67
79
  await asyncExec('git commit -m "chore: init"', {
68
80
  cwd: projectPath,
69
81
  });
70
- await asyncExec('npm i', {
82
+ loading.start();
83
+ await asyncExec(`${PACKAGE_MANAGER} i`, {
71
84
  cwd: projectPath,
72
85
  });
73
86
  await asyncExec('git add .', {
@@ -81,14 +94,40 @@ async function init(projectPath, pkgData) {
81
94
  loading.stop();
82
95
  }
83
96
  }
97
+ async function getGitUserName() {
98
+ const username = (await asyncExec('git config user.name'));
99
+ return username.trim();
100
+ }
101
+ async function getFastGitRepo(repository) {
102
+ const loading = ora__default["default"](`正在选择镜像源 - ${repository}`);
103
+ loading.start();
104
+ try {
105
+ const fast = await Promise.any(REMOTES.map((remote) => {
106
+ const url = `${remote}/${repository}/archive/refs/heads/master.zip`;
107
+ return axios__default["default"]({
108
+ url,
109
+ method: 'HEAD',
110
+ timeout: 15 * 1000,
111
+ });
112
+ }));
113
+ return `direct:${fast.config.url}`;
114
+ }
115
+ catch (error) {
116
+ console.error(error);
117
+ process.exit(1);
118
+ }
119
+ finally {
120
+ loading.stop();
121
+ }
122
+ }
84
123
 
85
124
  module.exports = function (plop) {
86
- plop.setActionType('initProject', async (answers, config) => {
125
+ plop.setActionType('initProject', async (answers) => {
87
126
  const name = answers.name;
88
127
  const author = answers.author;
89
128
  const template = answers.template;
90
129
  const projectPath = path__default["default"].join(process.cwd(), name);
91
- await downloadGitRepo(`github:CaoMeiYouRen/${template}`, projectPath);
130
+ await downloadGitRepo(`CaoMeiYouRen/${template}`, projectPath);
92
131
  await init(projectPath, {
93
132
  name,
94
133
  author,
@@ -97,55 +136,58 @@ module.exports = function (plop) {
97
136
  });
98
137
  plop.setGenerator('create', {
99
138
  description: '草梅项目创建器',
100
- prompts: [
101
- {
102
- type: 'input',
103
- name: 'name',
104
- message: '请输入项目名称',
105
- validate(input, answers) {
106
- return input.trim().length !== 0;
139
+ async prompts(inquirer) {
140
+ const questions = [
141
+ {
142
+ type: 'input',
143
+ name: 'name',
144
+ message: '请输入项目名称',
145
+ validate(input) {
146
+ return input.trim().length !== 0;
147
+ },
148
+ default: '',
149
+ filter: (e) => e.trim(),
107
150
  },
108
- default: __DEV__ ? 'temp' : '',
109
- filter: (e) => e.trim(),
110
- },
111
- {
112
- type: 'input',
113
- name: 'author',
114
- message: '请输入作者名称',
115
- validate(input, answers) {
116
- return input.trim().length !== 0;
151
+ {
152
+ type: 'input',
153
+ name: 'author',
154
+ message: '请输入作者名称',
155
+ validate(input) {
156
+ return input.trim().length !== 0;
157
+ },
158
+ default: await getGitUserName(),
159
+ filter: (e) => e.trim(),
117
160
  },
118
- default: __DEV__ ? 'CaoMeiYouRen' : '',
119
- filter: (e) => e.trim(),
120
- },
121
- {
122
- type: 'list',
123
- name: 'template',
124
- message: '请选择项目模板',
125
- choices({ projectType }) {
126
- return [
127
- 'vue',
128
- 'vue3',
129
- 'vite2',
130
- 'vite2-vue2',
131
- 'electron-vue',
132
- 'nuxt',
133
- 'uni',
134
- 'react',
135
- 'react16',
136
- 'ts',
137
- 'express',
138
- 'koa2',
139
- 'nest',
140
- 'auto-release',
141
- 'rollup',
142
- 'webpack',
143
- 'github-action',
144
- ].map((e) => `${e}-template`);
161
+ {
162
+ type: 'list',
163
+ name: 'template',
164
+ message: '请选择项目模板',
165
+ choices() {
166
+ return [
167
+ 'vue',
168
+ 'vue3',
169
+ 'vite2',
170
+ 'vite2-vue2',
171
+ 'electron-vue',
172
+ 'nuxt',
173
+ 'uni',
174
+ 'react',
175
+ 'react16',
176
+ 'ts',
177
+ 'express',
178
+ 'koa2',
179
+ 'nest',
180
+ 'auto-release',
181
+ 'rollup',
182
+ 'webpack',
183
+ 'github-action',
184
+ ].map((e) => `${e}-template`);
185
+ },
145
186
  },
146
- },
147
- ],
148
- actions(answers) {
187
+ ];
188
+ return inquirer.prompt(questions);
189
+ },
190
+ actions() {
149
191
  const actions = [];
150
192
  actions.push({
151
193
  type: 'initProject',
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "cmyr-template-cli",
3
- "version": "1.0.2",
3
+ "version": "1.3.0",
4
4
  "description": "草梅友仁自制的项目模板创建器",
5
5
  "author": "CaoMeiYouRen",
6
6
  "license": "MIT",
7
7
  "main": "dist/index.js",
8
+ "type": "commonjs",
8
9
  "files": [
9
10
  "dist"
10
11
  ],
@@ -26,12 +27,14 @@
26
27
  "release": "semantic-release",
27
28
  "commit": "git add . && git cz",
28
29
  "create": "ct create",
29
- "build:dev": "npm run build && rimraf temp && cross-env NODE_ENV=development ct create"
30
+ "build:dev": "rimraf dist && cross-env NODE_ENV=development rollup -c && rimraf temp && cross-env NODE_ENV=development ct create",
31
+ "build:prod": "npm run build && rimraf temp && cross-env NODE_ENV=production ct create"
30
32
  },
31
33
  "devDependencies": {
32
34
  "@rollup/plugin-commonjs": "^21.0.0",
33
35
  "@rollup/plugin-json": "^4.1.0",
34
36
  "@rollup/plugin-node-resolve": "^13.0.5",
37
+ "@rollup/plugin-replace": "^3.0.0",
35
38
  "@rollup/plugin-typescript": "^8.0.0",
36
39
  "@semantic-release/changelog": "^6.0.0",
37
40
  "@semantic-release/git": "^10.0.0",
@@ -39,6 +42,7 @@
39
42
  "@types/fs-extra": "^9.0.4",
40
43
  "@types/lodash": "^4.14.165",
41
44
  "@types/node": "^16.9.6",
45
+ "@types/promise.any": "^2.0.0",
42
46
  "@typescript-eslint/eslint-plugin": "^4.9.0",
43
47
  "@typescript-eslint/parser": "^4.9.0",
44
48
  "commitizen": "^4.2.2",
@@ -48,9 +52,9 @@
48
52
  "cz-conventional-changelog": "^3.3.0",
49
53
  "debug": "^4.3.1",
50
54
  "eslint": "^7.14.0",
51
- "eslint-config-cmyr": "^1.0.2",
55
+ "eslint-config-cmyr": "^1.1.13",
52
56
  "husky": "^7.0.2",
53
- "lint-staged": "^11.1.2",
57
+ "lint-staged": "^12.0.2",
54
58
  "lodash": "^4.17.20",
55
59
  "rimraf": "^3.0.2",
56
60
  "rollup": "^2.33.3",
@@ -62,6 +66,7 @@
62
66
  "validate-commit-msg": "^2.14.0"
63
67
  },
64
68
  "dependencies": {
69
+ "axios": "^0.24.0",
65
70
  "colors": "^1.4.0",
66
71
  "commander": "^8.2.0",
67
72
  "dayjs": "^1.9.6",
@@ -69,7 +74,8 @@
69
74
  "fs-extra": "^10.0.0",
70
75
  "minimist": "^1.2.5",
71
76
  "ora": "^5.4.1",
72
- "plop": "^2.7.4"
77
+ "plop": "^2.7.6",
78
+ "promise.any": "^2.0.2"
73
79
  },
74
80
  "config": {
75
81
  "commitizen": {