cmyr-template-cli 1.13.4 → 1.14.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.
package/README.md CHANGED
@@ -53,6 +53,23 @@ ct
53
53
  ct create
54
54
  ```
55
55
 
56
+ ## 配置
57
+
58
+ 在当前目录下或 `HOME` 路径下创建 `.ctrc` 文件即可,格式为 `json`
59
+
60
+ ```json
61
+ {
62
+ "GITHUB_TOKEN": "",
63
+ "GITEE_TOKEN": ""
64
+ }
65
+ ```
66
+
67
+ GITHUB_TOKEN 请参考: [创建用于命令行的个人访问令牌](https://help.github.com/cn/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line)
68
+
69
+ GITEE_TOKEN 请参考:https://gitee.com/profile/personal_access_tokens
70
+
71
+ **如果不使用自动初始化远程仓库功能,可以跳过该配置**
72
+
56
73
  ## 开发
57
74
 
58
75
  ```sh
package/dist/index.js CHANGED
@@ -13,7 +13,7 @@ var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
13
13
 
14
14
  const program = new commander.Command('ct')
15
15
  .description('草梅项目创建器');
16
- program.version("1.13.3" , '-v, --version');
16
+ program.version("1.14.0" , '-v, --version');
17
17
  const args = process.argv.slice(2);
18
18
  if (args.length === 0) {
19
19
  args.push('create');
package/dist/plopfile.js CHANGED
@@ -52,6 +52,7 @@ if (!Promise.any) {
52
52
  });
53
53
  }
54
54
  const GITHUB_API_URL = 'https://api.github.com';
55
+ const GITEE_API_URL = 'https://gitee.com/api/v5';
55
56
  const NODEJS_URLS = [
56
57
  'https://nodejs.org/zh-cn/download/',
57
58
  'http://nodejs.cn/download/',
@@ -61,6 +62,63 @@ const REMOTES = [
61
62
  'https://hub.fastgit.xyz',
62
63
  'https://download.fastgit.org',
63
64
  ];
65
+ async function createGiteeRepo(data) {
66
+ try {
67
+ const formData = new URLSearchParams();
68
+ Object.entries(data).forEach(([key, value]) => {
69
+ formData.append(key, String(value));
70
+ });
71
+ return await axios__default["default"]({
72
+ url: '/user/repos',
73
+ baseURL: GITEE_API_URL,
74
+ method: 'POST',
75
+ data: formData.toString(),
76
+ });
77
+ }
78
+ catch (error) {
79
+ console.error(error);
80
+ return null;
81
+ }
82
+ }
83
+ async function createGithubRepo(authToken, data) {
84
+ try {
85
+ return await axios__default["default"]({
86
+ url: '/user/repos',
87
+ baseURL: GITHUB_API_URL,
88
+ method: 'POST',
89
+ headers: {
90
+ Authorization: `Bearer ${authToken}`,
91
+ Accept: 'application/vnd.github+json',
92
+ },
93
+ data,
94
+ });
95
+ }
96
+ catch (error) {
97
+ console.error(error);
98
+ return null;
99
+ }
100
+ }
101
+ async function loadToken(type) {
102
+ const paths = [process.cwd(), process.env.HOME].map((e) => path__default["default"].join(e, '.ctrc'));
103
+ if (!['GITHUB_TOKEN', 'GITEE_TOKEN'].includes(type)) {
104
+ throw new Error(`无效的 token 类型:${type}`);
105
+ }
106
+ const CONFIG_KEY = type;
107
+ for await (const p of paths) {
108
+ try {
109
+ if (await fs__default["default"].pathExists(p)) {
110
+ const config = await fs__default["default"].readJSON(p);
111
+ if (config === null || config === void 0 ? void 0 : config[CONFIG_KEY]) {
112
+ return config[CONFIG_KEY];
113
+ }
114
+ }
115
+ }
116
+ catch (error) {
117
+ console.error(error);
118
+ }
119
+ }
120
+ return '';
121
+ }
64
122
  async function downloadGitRepo(repository, destination, options = {}) {
65
123
  const fastRepo = await getFastGitRepo(repository);
66
124
  const loading = ora__default["default"](`正在下载模板 - ${repository}`);
@@ -125,7 +183,7 @@ async function initProject(answers) {
125
183
  }
126
184
  async function init(projectPath, answers) {
127
185
  var _a;
128
- const { isOpenSource, gitRemoteUrl, isInitReadme, isInitContributing, isInitHusky, isInitSemanticRelease, isInitDocker } = answers;
186
+ const { isOpenSource, gitRemoteUrl, isInitRemoteRepo, isInitReadme, isInitContributing, isInitHusky, isInitSemanticRelease, isInitDocker } = answers;
129
187
  try {
130
188
  await asyncExec('git --version', {
131
189
  cwd: projectPath,
@@ -155,11 +213,8 @@ async function init(projectPath, answers) {
155
213
  }
156
214
  await initGithubWorkflows(projectPath, answers);
157
215
  }
158
- if (gitRemoteUrl) {
159
- await asyncExec(`git remote add origin ${gitRemoteUrl}`, {
160
- cwd: projectPath,
161
- });
162
- console.info(colors__default["default"].green(`请在远程 Git 仓库初始化 ${gitRemoteUrl}`));
216
+ if (isInitRemoteRepo && gitRemoteUrl) {
217
+ await initRemoteGitRepo(projectPath, answers);
163
218
  }
164
219
  if (isInitSemanticRelease) {
165
220
  await initSemanticRelease(projectPath);
@@ -198,6 +253,75 @@ async function getGitUserName() {
198
253
  const username = (await asyncExec('git config user.name')) || '';
199
254
  return username.trim();
200
255
  }
256
+ async function initRemoteGitRepo(projectPath, answers) {
257
+ var _a, _b;
258
+ const loading = ora__default["default"]('正在初始化远程 Git 仓库……').start();
259
+ try {
260
+ const { name, description, gitRemoteUrl, isOpenSource } = answers;
261
+ await asyncExec(`git remote add origin ${gitRemoteUrl}`, {
262
+ cwd: projectPath,
263
+ });
264
+ let type = '';
265
+ if (/github\.com/.test(gitRemoteUrl)) {
266
+ type = 'github';
267
+ }
268
+ else if (/gitee\.com/.test(gitRemoteUrl)) {
269
+ type = 'gitee';
270
+ }
271
+ switch (type) {
272
+ case 'github': {
273
+ const authToken = await loadToken('GITHUB_TOKEN');
274
+ if (!authToken) {
275
+ console.error(colors__default["default"].red(`未找到 ${type} token !跳过初始化!`));
276
+ break;
277
+ }
278
+ const resp = await createGithubRepo(authToken, {
279
+ name,
280
+ description,
281
+ private: isOpenSource,
282
+ });
283
+ if ((resp === null || resp === void 0 ? void 0 : resp.status) >= 200) {
284
+ loading.succeed('远程 Git 仓库初始化成功!');
285
+ console.info(colors__default["default"].green(`远程 Git 仓库地址 ${(_a = resp.data) === null || _a === void 0 ? void 0 : _a.html_url}`));
286
+ }
287
+ else {
288
+ loading.fail('远程 Git 仓库初始化失败!');
289
+ }
290
+ return;
291
+ }
292
+ case 'gitee': {
293
+ const access_token = await loadToken('GITEE_TOKEN');
294
+ if (!access_token) {
295
+ console.error(colors__default["default"].red(`未找到 ${type} token !跳过初始化!`));
296
+ break;
297
+ }
298
+ const resp = await createGiteeRepo({
299
+ access_token,
300
+ name,
301
+ description,
302
+ private: true,
303
+ });
304
+ if ((resp === null || resp === void 0 ? void 0 : resp.status) >= 200) {
305
+ loading.succeed('远程 Git 仓库初始化成功!');
306
+ console.info(colors__default["default"].green(`远程 Git 仓库地址 ${(_b = resp.data) === null || _b === void 0 ? void 0 : _b.html_url}`));
307
+ }
308
+ else {
309
+ loading.fail('远程 Git 仓库初始化失败!');
310
+ }
311
+ return;
312
+ }
313
+ default: {
314
+ break;
315
+ }
316
+ }
317
+ loading.stop();
318
+ console.info(colors__default["default"].green(`请在远程 Git 仓库初始化 ${gitRemoteUrl}`));
319
+ }
320
+ catch (error) {
321
+ loading.fail('远程 Git 仓库初始化失败!');
322
+ console.error(error);
323
+ }
324
+ }
201
325
  async function installNpmPackages(projectPath) {
202
326
  const loading = ora__default["default"]('正在安装依赖……').start();
203
327
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cmyr-template-cli",
3
- "version": "1.13.4",
3
+ "version": "1.14.1",
4
4
  "description": "草梅友仁自制的项目模板创建器",
5
5
  "author": "CaoMeiYouRen",
6
6
  "license": "MIT",
@@ -34,7 +34,7 @@
34
34
  "devDependencies": {
35
35
  "@rollup/plugin-commonjs": "^22.0.1",
36
36
  "@rollup/plugin-json": "^4.1.0",
37
- "@rollup/plugin-node-resolve": "^13.3.0",
37
+ "@rollup/plugin-node-resolve": "^14.0.0",
38
38
  "@rollup/plugin-replace": "^4.0.0",
39
39
  "@rollup/plugin-typescript": "^8.0.0",
40
40
  "@semantic-release/changelog": "^6.0.0",
@@ -59,7 +59,7 @@
59
59
  "husky": "^8.0.1",
60
60
  "lint-staged": "^13.0.0",
61
61
  "rimraf": "^3.0.2",
62
- "rollup": "^2.75.7",
62
+ "rollup": "^2.79.0",
63
63
  "rollup-plugin-terser": "^7.0.2",
64
64
  "semantic-release": "^19.0.2",
65
65
  "ts-node": "^10.2.1",
@@ -69,7 +69,7 @@
69
69
  },
70
70
  "dependencies": {
71
71
  "@lint-md/core": "^0.2.1",
72
- "axios": "^0.27.1",
72
+ "axios": "^1.0.0",
73
73
  "colors": "^1.4.0",
74
74
  "commander": "^9.0.0",
75
75
  "dayjs": "^1.9.6",