@done-coding/cli-utils 0.4.0-alpha.0 → 0.5.0-alpha.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/es/index.mjs CHANGED
@@ -1,51 +1,52 @@
1
1
  #!/usr/bin/env node
2
2
  import O from "chalk";
3
- import { default as ue } from "chalk";
4
- import a from "node:path";
5
- import E, { existsSync as m, mkdirSync as b, writeFileSync as y, readFileSync as g } from "node:fs";
6
- import l from "crypto";
3
+ import { default as pt } from "chalk";
4
+ import g from "node:path";
5
+ import b, { existsSync as y, mkdirSync as H, writeFileSync as C, readFileSync as d } from "node:fs";
6
+ import m from "crypto";
7
7
  import D from "prompts";
8
- import H from "yargs";
8
+ import N from "yargs";
9
9
  import { hideBin as A } from "yargs/helpers";
10
- import { execSync as h } from "node:child_process";
11
- import C from "json5";
10
+ import { execSync as a } from "node:child_process";
11
+ import v from "json5";
12
12
  export * from "json5";
13
- import { default as ge } from "json5";
14
- import { default as me } from "lodash.get";
15
- import { default as he } from "lodash.set";
16
- import { default as ve } from "lodash.curry";
13
+ import { default as yt } from "json5";
14
+ import u from "pinyin";
15
+ import { default as Ct } from "lodash.get";
16
+ import { default as $t } from "lodash.set";
17
+ import { default as wt } from "lodash.curry";
17
18
  const c = Object.assign(
18
- (e, ...r) => console.log(...r.map((t) => O[e](t))),
19
+ (t, ...r) => console.log(...r.map((e) => O[t](e))),
19
20
  {
20
21
  /** 成功 */
21
- success: (...e) => c("green", ...e),
22
+ success: (...t) => c("green", ...t),
22
23
  /** /步骤 */
23
- stage: (...e) => c("blue", ...e),
24
+ stage: (...t) => c("blue", ...t),
24
25
  /** 提示信息 */
25
- info: (...e) => c("cyan", ...e),
26
+ info: (...t) => c("cyan", ...t),
26
27
  /** 警告 */
27
- warn: (...e) => c("yellow", ...e),
28
+ warn: (...t) => c("yellow", ...t),
28
29
  /** 错误 */
29
- error: (...e) => c("red", ...e),
30
+ error: (...t) => c("red", ...t),
30
31
  /** 跳过 */
31
- skip: (...e) => c("gray", ...e)
32
+ skip: (...t) => c("gray", ...t)
32
33
  }
33
- ), X = (e, {
34
+ ), tt = (t, {
34
35
  /** 当前目录 */
35
36
  currentDir: r = process.cwd(),
36
37
  /** 优先找最远的父目录 */
37
- isFindFarthest: t = !0
38
+ isFindFarthest: e = !0
38
39
  } = {}) => {
39
- const n = a.resolve(r).split(a.sep).map((o, s, f) => s ? a.join(f.slice(0, s).join(a.sep), o) : o);
40
+ const n = g.resolve(r).split(g.sep).map((o, i, l) => i ? g.join(l.slice(0, i).join(g.sep), o) : o);
40
41
  for (; n.length; ) {
41
- const o = t ? n.shift() : n.pop(), s = a.join(o, e);
42
- if (E.existsSync(s))
42
+ const o = e ? n.shift() : n.pop(), i = g.join(o, t);
43
+ if (b.existsSync(i))
43
44
  return o;
44
45
  }
45
- }, v = "aes-256-cbc", S = 16, p = "hex", d = ":";
46
- function $(e) {
47
- return l.pbkdf2Sync(
48
- e,
46
+ }, $ = "aes-256-cbc", P = 16, p = "hex", h = ":";
47
+ function w(t) {
48
+ return m.pbkdf2Sync(
49
+ t,
49
50
  "done-coding-cli-salt",
50
51
  // 使用固定的盐值
51
52
  1e4,
@@ -55,84 +56,84 @@ function $(e) {
55
56
  "sha256"
56
57
  );
57
58
  }
58
- function Z({
59
- text: e,
59
+ function rt({
60
+ text: t,
60
61
  secretKey: r
61
62
  }) {
62
63
  try {
63
- const t = $(r), n = l.randomBytes(S), o = l.createCipheriv(v, t, n);
64
- let s = o.update(e);
65
- s = Buffer.concat([s, o.final()]);
66
- const f = n.toString(p), i = s.toString(p);
67
- return `${f}${d}${i}`;
68
- } catch (t) {
64
+ const e = w(r), n = m.randomBytes(P), o = m.createCipheriv($, e, n);
65
+ let i = o.update(t);
66
+ i = Buffer.concat([i, o.final()]);
67
+ const l = n.toString(p), s = i.toString(p);
68
+ return `${l}${h}${s}`;
69
+ } catch (e) {
69
70
  return c.error(
70
- `加密失败: ${t instanceof Error ? t.message : String(t)}`
71
+ `加密失败: ${e instanceof Error ? e.message : String(e)}`
71
72
  ), "";
72
73
  }
73
74
  }
74
- function ee({
75
- encryptedText: e,
75
+ function et({
76
+ encryptedText: t,
76
77
  secretKey: r
77
78
  }) {
78
79
  try {
79
- if (!e.includes(d))
80
+ if (!t.includes(h))
80
81
  return "";
81
- const t = $(r), [n, o] = e.split(d);
82
- if (n.length !== S * 2)
82
+ const e = w(r), [n, o] = t.split(h);
83
+ if (n.length !== P * 2)
83
84
  return "";
84
- const s = Buffer.from(n, p), f = Buffer.from(o, p), i = l.createDecipheriv(v, t, s);
85
- let u = i.update(f);
86
- return u = Buffer.concat([u, i.final()]), u.toString();
87
- } catch (t) {
85
+ const i = Buffer.from(n, p), l = Buffer.from(o, p), s = m.createDecipheriv($, e, i);
86
+ let f = s.update(l);
87
+ return f = Buffer.concat([f, s.final()]), f.toString();
88
+ } catch (e) {
88
89
  return c.error(
89
- `解密失败: ${t instanceof Error ? t.message : String(t)}`
90
+ `解密失败: ${e instanceof Error ? e.message : String(e)}`
90
91
  ), "";
91
92
  }
92
93
  }
93
- const k = (...e) => {
94
- const [r, t = {}] = e;
94
+ const E = (...t) => {
95
+ const [r, e = {}] = t;
95
96
  return D(r, {
96
97
  onCancel(n) {
97
98
  return c.error(`退出${n == null ? void 0 : n.name}输入`), process.exit(1);
98
99
  },
99
- ...t
100
+ ...e
100
101
  });
101
- }, R = (e, r) => {
102
- e ? c.error(e) : c.error(r.message), r != null && r.stack && c.error(r.stack), process.exit(1);
103
- }, _ = () => {
104
- const e = A(process.argv);
105
- return H(e);
106
- }, w = (e, {
102
+ }, R = (t, r) => {
103
+ t ? c.error(t) : c.error(r.message), r != null && r.stack && c.error(r.stack), process.exit(1);
104
+ }, T = () => {
105
+ const t = A(process.argv);
106
+ return N(t);
107
+ }, j = (t, {
107
108
  usage: r,
108
- version: t,
109
+ version: e,
109
110
  demandCommandCount: n,
110
111
  options: o,
111
- positionals: s,
112
- subcommands: f
112
+ positionals: i,
113
+ subcommands: l
113
114
  }) => {
114
- let i = e.strict();
115
- r && (i = i.usage(`Usage: ${r}`)), n && (i = i.demandCommand(n));
116
- const u = "help";
117
- return i = i.help(u), t ? i = i.version(t).alias("h", u).alias("v", "version") : i = i.alias("h", u), o && (i = i.options(o)), s && (i = Object.entries(s).reduce((x, [P, j]) => x.positional(P, j), i)), f && (i = i.command(f)), i;
118
- }, re = async ({ handler: e, ...r }) => {
119
- const t = await w(_(), r).fail(R).argv;
120
- return e ? e(t) : t;
121
- }, te = (e) => {
122
- const { command: r, describe: t, handler: n = () => {
123
- }, ...o } = e;
115
+ let s = t.strict();
116
+ r && (s = s.usage(`Usage: ${r}`)), n && (s = s.demandCommand(n));
117
+ const f = "help";
118
+ return s = s.help(f), e ? s = s.version(e).alias("h", f).alias("v", "version") : s = s.alias("h", f), o && (s = s.options(o)), i && (s = Object.entries(i).reduce((S, [k, x]) => S.positional(k, x), s)), l && (s = s.command(l)), s;
119
+ }, ot = async ({ handler: t, ...r }) => {
120
+ const e = await j(T(), r).fail(R).argv;
121
+ return t ? t(e) : e;
122
+ }, nt = (t) => {
123
+ const { command: r, describe: e, handler: n = () => {
124
+ }, ...o } = t;
124
125
  return {
125
126
  command: r,
126
- describe: t,
127
- builder(s) {
128
- return w(s, o);
127
+ describe: e,
128
+ builder(i) {
129
+ return j(i, o);
129
130
  },
130
131
  handler: n
131
132
  };
132
133
  };
133
- var F = /* @__PURE__ */ ((e) => (e.VSCODE = "VsCode", e.CURSOR = "Cursor", e.OTHER = "其他", e))(F || {});
134
- const N = async () => {
135
- const { editorType: e } = await k([
134
+ var _ = /* @__PURE__ */ ((t) => (t.VSCODE = "VsCode", t.CURSOR = "Cursor", t.OTHER = "其他", t))(_ || {});
135
+ const L = async () => {
136
+ const { editorType: t } = await E([
136
137
  {
137
138
  name: "editorType",
138
139
  type: "select",
@@ -148,116 +149,163 @@ const N = async () => {
148
149
  }))
149
150
  }
150
151
  ]);
151
- return e;
152
- }, T = {
152
+ return t;
153
+ }, F = {
153
154
  Cursor: "cursor",
154
155
  VsCode: "code"
155
- }, B = (e, r, t) => {
156
+ }, M = (t, r, e) => {
156
157
  try {
157
- h(`${e} -v`, { stdio: "ignore" }), h(`${e} ${r}`);
158
+ a(`${t} -v`, { stdio: "ignore" }), a(`${t} ${r}`);
158
159
  } catch {
159
- t();
160
+ e();
160
161
  }
161
- }, J = (e, r) => {
162
- const t = (n) => c.info(`
163
- ${n}, 请用编辑器打开 ${e} 进行编辑
162
+ }, B = (t, r) => {
163
+ const e = (n) => c.info(`
164
+ ${n}, 请用编辑器打开 ${t} 进行编辑
164
165
  `);
165
166
  switch (r) {
166
167
  case "Cursor":
167
168
  case "VsCode": {
168
- const n = T[r];
169
- B(n, e, () => {
170
- t(`${n}命令未安装`);
169
+ const n = F[r];
170
+ M(n, t, () => {
171
+ e(`${n}命令未安装`);
171
172
  });
172
173
  break;
173
174
  }
174
175
  default:
175
- t("其他编辑器");
176
+ e("其他编辑器");
176
177
  }
177
- }, V = (e = process.cwd()) => ({
178
+ }, J = (t = process.cwd()) => ({
178
179
  /** 必须保留 */
179
180
  rootDir: {
180
181
  type: "string",
181
182
  alias: "R",
182
183
  describe: "运行目录",
183
184
  /** 必须设置默认值 */
184
- default: e
185
+ default: t
185
186
  }
186
- }), oe = ({
187
- configPathDefault: e,
187
+ }), it = ({
188
+ configPathDefault: t,
188
189
  rootDirDefault: r
189
190
  }) => ({
190
191
  /** 必须保留 */
191
- ...V(r),
192
+ ...J(r),
192
193
  /** 必须保留 */
193
194
  configPath: {
194
195
  type: "string",
195
196
  alias: "C",
196
197
  describe: "配置文件相对路径",
197
198
  /** 必须设置默认值 */
198
- default: e
199
+ default: t
199
200
  }
200
- }), L = async (e, r) => {
201
- const { configPath: t, rootDir: n } = r, o = a.resolve(n, t), s = a.dirname(o);
202
- return m(s) || b(s, {
201
+ }), V = async (t, r) => {
202
+ const { configPath: e, rootDir: n } = r, o = g.resolve(n, e), i = g.dirname(o);
203
+ return y(i) || H(i, {
203
204
  recursive: !0
204
- }), o.endsWith(".json5") ? (c.info(`json5模式写入 ${o}`), y(o, C.stringify(e, null, 2)), o) : (c.info(`json模式写入 ${o}`), y(o, JSON.stringify(e, null, 2)), o);
205
- }, ne = async (e, r, {
206
- onFileGenerated: t,
205
+ }), o.endsWith(".json5") ? (c.info(`json5模式写入 ${o}`), C(o, v.stringify(t, null, 2)), o) : (c.info(`json模式写入 ${o}`), C(o, JSON.stringify(t, null, 2)), o);
206
+ }, st = async (t, r, {
207
+ onFileGenerated: e,
207
208
  edit: n = !1
208
209
  } = {}) => {
209
- const o = await L(e, r);
210
- if (t == null || t(o), n) {
211
- const s = await N();
212
- J(r.configPath, s);
210
+ const o = await V(t, r);
211
+ if (e == null || e(o), n) {
212
+ const i = await L();
213
+ B(r.configPath, i);
213
214
  }
214
- }, se = async (e, r) => {
215
- const { configPath: t, rootDir: n } = e, o = a.resolve(n, t);
216
- if (!m(o)) {
215
+ }, ct = async (t, r) => {
216
+ const { configPath: e, rootDir: n } = t, o = g.resolve(n, e);
217
+ if (!y(o)) {
217
218
  if (r)
218
219
  return c.info("配置文件不存在,使用onNotExists返回值"), r();
219
- const s = `配置文件不存在 ${o}`;
220
- throw new Error(s);
220
+ const i = `配置文件不存在 ${o}`;
221
+ throw new Error(i);
221
222
  }
222
- return o.endsWith(".json5") ? (c.info(`json5模式解析 ${o}`), C.parse(g(o, "utf8"))) : (c.info(`json模式解析 ${o}`), JSON.parse(g(o, "utf8")));
223
- }, ie = async () => {
224
- const { useDefaultConfig: e } = await k({
223
+ return o.endsWith(".json5") ? (c.info(`json5模式解析 ${o}`), v.parse(d(o, "utf8"))) : (c.info(`json模式解析 ${o}`), JSON.parse(d(o, "utf8")));
224
+ }, at = async () => {
225
+ const { useDefaultConfig: t } = await E({
225
226
  name: "useDefaultConfig",
226
227
  type: "confirm",
227
228
  message: "使用默认模板配置",
228
229
  initial: !0
229
230
  });
230
- return e;
231
- }, I = "./package.json", ce = ({
232
- rootDir: e
231
+ return t;
232
+ }, Y = "./package.json", ft = ({
233
+ rootDir: t
233
234
  }) => {
234
- const r = a.resolve(e, I);
235
- if (!m(r))
236
- throw new Error(`${e}未找到package.json文件`);
237
- const t = g(r, "utf-8");
238
- return JSON.parse(t);
235
+ const r = g.resolve(t, Y);
236
+ if (!y(r))
237
+ throw new Error(`${t}未找到package.json文件`);
238
+ const e = d(r, "utf-8");
239
+ return JSON.parse(e);
240
+ }, lt = ({
241
+ remoteAlias: t
242
+ } = {}) => {
243
+ try {
244
+ const r = a("git rev-parse HEAD").toString().trim(), e = a('git log -1 --pretty=format:"%an"').toString().trim(), n = a('git log -1 --pretty=format:"%ae"').toString().trim(), o = a('git log -1 --pretty=format:"%s"').toString().trim(), i = a("git config user.name").toString().trim(), l = a("git config user.email").toString().trim(), s = a("git rev-parse --abbrev-ref HEAD").toString().trim();
245
+ let f = "";
246
+ try {
247
+ f = a(`git config --get remote.${t}.url`).toString().trim();
248
+ } catch {
249
+ c.warn("git远程仓库地址获取失败或者不存在");
250
+ }
251
+ return {
252
+ lastHash: r,
253
+ lastCommitter: e,
254
+ lastCommitterPinYin: u(e, {
255
+ style: u.STYLE_NORMAL,
256
+ heteronym: !1
257
+ }).join(""),
258
+ lastCommitEmail: n,
259
+ lastCommitMsg: o,
260
+ userName: i,
261
+ userNamePinYin: u(i, {
262
+ style: u.STYLE_NORMAL,
263
+ heteronym: !1
264
+ }).join(""),
265
+ userEmail: l,
266
+ branchName: s,
267
+ remoteInfo: f ? {
268
+ alias: t,
269
+ url: f
270
+ } : void 0
271
+ };
272
+ } catch (r) {
273
+ throw c.error("获取git最后提交信息失败"), r;
274
+ }
275
+ }, gt = ({
276
+ branchName: t,
277
+ version: r,
278
+ remoteInfo: e
279
+ }) => {
280
+ e && (a(`git push ${e.alias} v${r}`, {
281
+ stdio: "inherit"
282
+ }), a(`git push ${e.alias} ${t}`, {
283
+ stdio: "inherit"
284
+ }));
239
285
  };
240
286
  export {
241
- F as EditorTypeEnum,
242
- ve as _curry,
243
- me as _get,
244
- he as _set,
245
- ue as chalk,
246
- re as createMainCommand,
247
- te as createSubcommand,
248
- ee as decryptAES,
249
- Z as encryptAES,
250
- oe as getConfigFileCommonOptions,
251
- N as getEditorType,
252
- ce as getPackageJson,
253
- V as getRootDirOptions,
254
- ie as getUseDefaultConfig,
255
- L as initConfigFile,
256
- ne as initHandlerCommon,
257
- ge as json5,
287
+ _ as EditorTypeEnum,
288
+ wt as _curry,
289
+ Ct as _get,
290
+ $t as _set,
291
+ pt as chalk,
292
+ ot as createMainCommand,
293
+ nt as createSubcommand,
294
+ et as decryptAES,
295
+ rt as encryptAES,
296
+ it as getConfigFileCommonOptions,
297
+ L as getEditorType,
298
+ lt as getGitLastCommitInfo,
299
+ ft as getPackageJson,
300
+ J as getRootDirOptions,
301
+ at as getUseDefaultConfig,
302
+ V as initConfigFile,
303
+ st as initHandlerCommon,
304
+ yt as json5,
258
305
  c as log,
259
- X as lookForParentTarget,
260
- J as openFileInEditor,
261
- se as readConfigFile,
262
- k as xPrompts
306
+ tt as lookForParentTarget,
307
+ B as openFileInEditor,
308
+ gt as pushGitPublishInfoToRemote,
309
+ ct as readConfigFile,
310
+ E as xPrompts
263
311
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@done-coding/cli-utils",
3
- "version": "0.4.0-alpha.0",
3
+ "version": "0.5.0-alpha.0",
4
4
  "description": "cli utils",
5
5
  "private": false,
6
6
  "module": "es/index.mjs",
@@ -42,6 +42,7 @@
42
42
  "@types/lodash.get": "^4.4.9",
43
43
  "@types/lodash.set": "^4.3.9",
44
44
  "@types/node": "^18.0.0",
45
+ "@types/pinyin": "^2.10.0",
45
46
  "@types/prompts": "^2.4.6",
46
47
  "@types/yargs": "^17.0.28",
47
48
  "rimraf": "^6.0.1",
@@ -58,8 +59,9 @@
58
59
  "lodash.curry": "^4.1.1",
59
60
  "lodash.get": "^4.4.2",
60
61
  "lodash.set": "^4.3.2",
62
+ "pinyin": "^2.11.2",
61
63
  "prompts": "^2.4.2",
62
64
  "yargs": "^17.7.2"
63
65
  },
64
- "gitHead": "360b14a641064f732b01ffd6339c2f1963945b61"
66
+ "gitHead": "86cdd3eaf8cc8be3e48a812b93fd7c2110fcc879"
65
67
  }
package/types/git.d.ts ADDED
@@ -0,0 +1,71 @@
1
+ /** 获取git最好提交信息参数 */
2
+ export interface GetGitLastCommitParams {
3
+ /**
4
+ * 远程仓库别名
5
+ */
6
+ remoteAlias?: string;
7
+ }
8
+ /** 远程仓库信息 */
9
+ export interface GitRemoteInfo {
10
+ /**
11
+ * 远程仓库别名
12
+ */
13
+ alias?: GetGitLastCommitParams["remoteAlias"];
14
+ /**
15
+ * 仓库地址
16
+ */
17
+ url?: string;
18
+ }
19
+ /**
20
+ * git最后提交信息
21
+ */
22
+ export interface GitLastCommitInfo {
23
+ /**
24
+ * 最后一次提交hash值
25
+ */
26
+ lastHash: string;
27
+ /**
28
+ * 最后一次提交者
29
+ */
30
+ lastCommitter: string;
31
+ /**
32
+ * 最后一次提交者拼音
33
+ */
34
+ lastCommitterPinYin: string;
35
+ /**
36
+ * 最后一次提交者邮箱
37
+ */
38
+ lastCommitEmail: string;
39
+ /**
40
+ * 最后一次提交信息
41
+ */
42
+ lastCommitMsg: string;
43
+ /**
44
+ * 用户名
45
+ */
46
+ userName: string;
47
+ /**
48
+ * 用户名拼音
49
+ */
50
+ userNamePinYin: string;
51
+ /**
52
+ * 邮箱
53
+ */
54
+ userEmail: string;
55
+ /**
56
+ * 分知名
57
+ */
58
+ branchName: string;
59
+ /** 远程仓库信息 */
60
+ remoteInfo?: GitRemoteInfo;
61
+ }
62
+ /**
63
+ * 获取git 最后提交信息
64
+ */
65
+ export declare const getGitLastCommitInfo: ({ remoteAlias, }?: GetGitLastCommitParams) => GitLastCommitInfo;
66
+ /** 推送git发布信息到远程仓库 */
67
+ export declare const pushGitPublishInfoToRemote: ({ branchName, version, remoteInfo, }: {
68
+ branchName: string;
69
+ version: string;
70
+ remoteInfo?: GitRemoteInfo | undefined;
71
+ }) => void;
package/types/index.d.ts CHANGED
@@ -8,3 +8,4 @@ export * from "./config-file";
8
8
  export * from "./json5";
9
9
  export * from "./lodash";
10
10
  export * from "./package-json";
11
+ export * from "./git";