@done-coding/cli-utils 0.7.1 → 0.7.3-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,25 +1,25 @@
1
1
  #!/usr/bin/env node
2
- import m from "chalk";
3
- import { default as oe } from "chalk";
4
- import f from "node:path";
5
- import u, { existsSync as p, mkdirSync as W, writeFileSync as b, readFileSync as v, rmSync as X } from "node:fs";
2
+ import p from "chalk";
3
+ import { default as se } from "chalk";
4
+ import g from "node:path";
5
+ import m, { existsSync as y, mkdirSync as W, writeFileSync as w, readFileSync as v, rmSync as X } from "node:fs";
6
6
  import _ from "crypto";
7
7
  import q from "prompts";
8
8
  import z from "yargs";
9
9
  import { hideBin as Q } from "yargs/helpers";
10
- import { execSync as g } from "node:child_process";
11
- import j from "json5";
10
+ import { execSync as u } from "node:child_process";
11
+ import H from "json5";
12
12
  export * from "json5";
13
- import { default as ie } from "json5";
13
+ import { default as ce } from "json5";
14
14
  import Z from "lodash.merge";
15
15
  import tt from "semver";
16
16
  import { tmpdir as et } from "node:os";
17
- import { default as ae } from "lodash.get";
18
- import { default as fe } from "lodash.set";
19
- import { default as ue } from "lodash.curry";
17
+ import { default as fe } from "lodash.get";
18
+ import { default as ge } from "lodash.set";
19
+ import { default as me } from "lodash.curry";
20
20
  import E from "pinyin";
21
21
  const l = Object.assign(
22
- (t, ...e) => console.log(...e.map((r) => m[t](r))),
22
+ (t, ...r) => console.log(...r.map((e) => p[t](e))),
23
23
  {
24
24
  /** 成功 */
25
25
  success: (...t) => l("green", ...t),
@@ -36,18 +36,18 @@ const l = Object.assign(
36
36
  }
37
37
  ), Bt = (t, {
38
38
  /** 当前目录 */
39
- currentDir: e = process.cwd(),
39
+ currentDir: r = process.cwd(),
40
40
  /** 优先找最远的父目录 */
41
- isFindFarthest: r = !0
41
+ isFindFarthest: e = !0
42
42
  } = {}) => {
43
- const o = f.resolve(e).split(f.sep).map((s, n, c) => n ? f.join(c.slice(0, n).join(f.sep), s) : s);
43
+ const o = g.resolve(r).split(g.sep).map((s, n, c) => n ? g.join(c.slice(0, n).join(g.sep), s) : s);
44
44
  for (; o.length; ) {
45
- const s = r ? o.shift() : o.pop(), n = f.join(s, t);
46
- if (u.existsSync(n))
45
+ const s = e ? o.shift() : o.pop(), n = g.join(s, t);
46
+ if (m.existsSync(n))
47
47
  return s;
48
48
  }
49
- }, H = "aes-256-cbc", F = 16, M = "hex", A = ":";
50
- function N(t) {
49
+ }, N = "aes-256-cbc", F = 16, M = "hex", A = ":";
50
+ function G(t) {
51
51
  return _.pbkdf2Sync(
52
52
  t,
53
53
  "done-coding-cli-salt",
@@ -61,82 +61,105 @@ function N(t) {
61
61
  }
62
62
  function kt({
63
63
  text: t,
64
- secretKey: e
64
+ secretKey: r
65
65
  }) {
66
66
  try {
67
- const r = N(e), o = _.randomBytes(F), s = _.createCipheriv(H, r, o);
67
+ const e = G(r), o = _.randomBytes(F), s = _.createCipheriv(N, e, o);
68
68
  let n = s.update(t);
69
69
  n = Buffer.concat([n, s.final()]);
70
70
  const c = o.toString(M), i = n.toString(M);
71
71
  return `${c}${A}${i}`;
72
- } catch (r) {
72
+ } catch (e) {
73
73
  return l.error(
74
- `加密失败: ${r instanceof Error ? r.message : String(r)}`
74
+ `加密失败: ${e instanceof Error ? e.message : String(e)}`
75
75
  ), "";
76
76
  }
77
77
  }
78
- function xt({
78
+ function jt({
79
79
  encryptedText: t,
80
- secretKey: e
80
+ secretKey: r
81
81
  }) {
82
82
  try {
83
83
  if (!t.includes(A))
84
84
  return "";
85
- const r = N(e), [o, s] = t.split(A);
85
+ const e = G(r), [o, s] = t.split(A);
86
86
  if (o.length !== F * 2)
87
87
  return "";
88
- const n = Buffer.from(o, M), c = Buffer.from(s, M), i = _.createDecipheriv(H, r, n);
88
+ const n = Buffer.from(o, M), c = Buffer.from(s, M), i = _.createDecipheriv(N, e, n);
89
89
  let a = i.update(c);
90
90
  return a = Buffer.concat([a, i.final()]), a.toString();
91
- } catch (r) {
91
+ } catch (e) {
92
92
  return l.error(
93
- `解密失败: ${r instanceof Error ? r.message : String(r)}`
93
+ `解密失败: ${e instanceof Error ? e.message : String(e)}`
94
94
  ), "";
95
95
  }
96
96
  }
97
- const G = (...t) => {
98
- const [e, r = {}] = t;
99
- return q(e, {
97
+ const L = (...t) => {
98
+ const [r, e = {}] = t;
99
+ return q(r, {
100
100
  onCancel(o) {
101
101
  return l.error(`退出${o == null ? void 0 : o.name}输入`), process.exit(1);
102
102
  },
103
- ...r
103
+ ...e
104
104
  });
105
- }, rt = (t, e) => {
106
- t ? l.error(t) : l.error(e.message), e != null && e.stack && l.error(e.stack), process.exit(1);
105
+ }, rt = (t, r) => {
106
+ t ? l.error(t) : l.error(r.message), r != null && r.stack && l.error(r.stack), process.exit(1);
107
107
  }, ot = () => {
108
108
  const t = Q(process.argv);
109
109
  return z(t);
110
+ }, xt = ({
111
+ rootScriptName: t,
112
+ packageJson: r
113
+ }) => {
114
+ if (t)
115
+ return t;
116
+ if (!r)
117
+ return;
118
+ const { bin: e, name: o } = r;
119
+ if (e) {
120
+ if (typeof e == "string")
121
+ return o.includes("/") ? void 0 : o;
122
+ if (typeof e == "object") {
123
+ const s = Object.entries(e), n = process.argv[1], c = s.filter(([, a]) => n == null ? void 0 : n.endsWith(a));
124
+ if (c.length !== 1)
125
+ return;
126
+ const [i] = c[0];
127
+ return process.platform === "win32" ? i.toLowerCase() : i;
128
+ }
129
+ }
110
130
  }, J = (t, {
111
- usage: e,
112
- version: r,
131
+ usage: r,
132
+ version: e,
113
133
  demandCommandCount: o,
114
134
  options: s,
115
135
  positionals: n,
116
- subcommands: c
117
- }) => {
118
- let i = t.strict();
119
- e && (i = i.usage(`Usage: ${e}`)), o && (i = i.demandCommand(o));
120
- const a = "help";
121
- return i = i.help(a), r ? i = i.version(r).alias("h", a).alias("v", "version") : i = i.alias("h", a), s && (i = i.options(s)), n && (i = Object.entries(n).reduce((y, [R, S]) => y.positional(R, S), i)), c && (i = i.command(c)), i;
122
- }, jt = async ({ handler: t, ...e }) => {
123
- const r = await J(ot(), e).fail(rt).argv;
124
- return t ? t(r) : r;
125
- }, Ht = (t) => {
126
- const { command: e, describe: r, handler: o = () => {
136
+ subcommands: c,
137
+ rootScriptName: i
138
+ }, a) => {
139
+ let f = t.strict();
140
+ r && (f = f.usage(`Usage: ${r}`)), o && (f = f.demandCommand(o));
141
+ const d = "help";
142
+ return f = f.help(d), e ? f = f.version(e).alias("h", d).alias("v", "version") : f = f.alias("h", d), s && (f = f.options(s)), n && (f = Object.entries(n).reduce((S, [P, h]) => S.positional(P, h), f)), c && (f = f.command(c)), a && i && (f = f.scriptName(i)), f;
143
+ }, Ht = async ({ handler: t, ...r }) => {
144
+ const e = await J(ot(), r, !0).fail(
145
+ rt
146
+ ).argv;
147
+ return t ? t(e) : e;
148
+ }, Nt = (t) => {
149
+ const { command: r, describe: e, handler: o = () => {
127
150
  }, ...s } = t;
128
151
  return {
129
- command: e,
130
- describe: r,
152
+ command: r,
153
+ describe: e,
131
154
  builder(n) {
132
- return J(n, s);
155
+ return J(n, s, !1);
133
156
  },
134
157
  handler: o
135
158
  };
136
159
  };
137
160
  var st = /* @__PURE__ */ ((t) => (t.VSCODE = "VsCode", t.CURSOR = "Cursor", t.OTHER = "其他", t))(st || {});
138
161
  const nt = async () => {
139
- const { editorType: t } = await G([
162
+ const { editorType: t } = await L([
140
163
  {
141
164
  name: "editorType",
142
165
  type: "select",
@@ -146,9 +169,9 @@ const nt = async () => {
146
169
  "VsCode",
147
170
  "其他"
148
171
  /* OTHER */
149
- ].map((e) => ({
150
- title: e,
151
- value: e
172
+ ].map((r) => ({
173
+ title: r,
174
+ value: r
152
175
  }))
153
176
  }
154
177
  ]);
@@ -156,29 +179,29 @@ const nt = async () => {
156
179
  }, it = {
157
180
  Cursor: "cursor",
158
181
  VsCode: "code"
159
- }, ct = (t, e, r) => {
182
+ }, ct = (t, r, e) => {
160
183
  try {
161
- g(`${t} -v`, { stdio: "ignore" }), g(`${t} ${e}`);
184
+ u(`${t} -v`, { stdio: "ignore" }), u(`${t} ${r}`);
162
185
  } catch {
163
- r();
186
+ e();
164
187
  }
165
- }, at = (t, e) => {
166
- const r = (o) => l.info(`
188
+ }, at = (t, r) => {
189
+ const e = (o) => l.info(`
167
190
  ${o}, 请用编辑器打开 ${t} 进行编辑
168
191
  `);
169
- switch (e) {
192
+ switch (r) {
170
193
  case "Cursor":
171
194
  case "VsCode": {
172
- const o = it[e];
195
+ const o = it[r];
173
196
  ct(o, t, () => {
174
- r(`${o}命令未安装`);
197
+ e(`${o}命令未安装`);
175
198
  });
176
199
  break;
177
200
  }
178
201
  default:
179
- r("其他编辑器");
202
+ e("其他编辑器");
180
203
  }
181
- }, lt = (t = process.cwd()) => ({
204
+ }, ft = (t = process.cwd()) => ({
182
205
  /** 必须保留 */
183
206
  rootDir: {
184
207
  type: "string",
@@ -189,10 +212,10 @@ const nt = async () => {
189
212
  }
190
213
  }), Ft = ({
191
214
  configPathDefault: t,
192
- rootDirDefault: e
215
+ rootDirDefault: r
193
216
  }) => ({
194
217
  /** 必须保留 */
195
- ...lt(e),
218
+ ...ft(r),
196
219
  /** 必须保留 */
197
220
  configPath: {
198
221
  type: "string",
@@ -201,192 +224,192 @@ const nt = async () => {
201
224
  /** 必须设置默认值 */
202
225
  default: t
203
226
  }
204
- }), ft = async (t, e) => {
205
- const { configPath: r, rootDir: o } = e, s = f.resolve(o, r), n = f.dirname(s);
206
- return p(n) || W(n, {
227
+ }), lt = async (t, r) => {
228
+ const { configPath: e, rootDir: o } = r, s = g.resolve(o, e), n = g.dirname(s);
229
+ return y(n) || W(n, {
207
230
  recursive: !0
208
- }), s.endsWith(".json5") ? (l.info(`json5模式写入 ${s}`), b(s, j.stringify(t, null, 2)), s) : (l.info(`json模式写入 ${s}`), b(s, JSON.stringify(t, null, 2)), s);
209
- }, Nt = async (t, e, {
210
- onFileGenerated: r,
231
+ }), s.endsWith(".json5") ? (l.info(`json5模式写入 ${s}`), w(s, H.stringify(t, null, 2)), s) : (l.info(`json模式写入 ${s}`), w(s, JSON.stringify(t, null, 2)), s);
232
+ }, Gt = async (t, r, {
233
+ onFileGenerated: e,
211
234
  edit: o = !1
212
235
  } = {}) => {
213
- const s = await ft(t, e);
214
- if (r == null || r(s), o) {
236
+ const s = await lt(t, r);
237
+ if (e == null || e(s), o) {
215
238
  const n = await nt();
216
- at(e.configPath, n);
239
+ at(r.configPath, n);
217
240
  }
218
- }, Gt = async (t, e) => {
219
- const { configPath: r, rootDir: o } = t, s = f.resolve(o, r);
220
- if (!p(s)) {
221
- if (e)
222
- return l.info("配置文件不存在,使用onNotExists返回值"), e();
241
+ }, Lt = async (t, r) => {
242
+ const { configPath: e, rootDir: o } = t, s = g.resolve(o, e);
243
+ if (!y(s)) {
244
+ if (r)
245
+ return l.info("配置文件不存在,使用onNotExists返回值"), r();
223
246
  const n = `配置文件不存在 ${s}`;
224
247
  throw new Error(n);
225
248
  }
226
- return s.endsWith(".json5") ? (l.info(`json5模式解析 ${s}`), j.parse(v(s, "utf8"))) : (l.info(`json模式解析 ${s}`), JSON.parse(v(s, "utf8")));
249
+ return s.endsWith(".json5") ? (l.info(`json5模式解析 ${s}`), H.parse(v(s, "utf8"))) : (l.info(`json模式解析 ${s}`), JSON.parse(v(s, "utf8")));
227
250
  }, Jt = async () => {
228
- const { useDefaultConfig: t } = await G({
251
+ const { useDefaultConfig: t } = await L({
229
252
  name: "useDefaultConfig",
230
253
  type: "confirm",
231
254
  message: "使用默认模板配置",
232
255
  initial: !0
233
256
  });
234
257
  return t;
235
- }, L = "package.json", w = ({
258
+ }, U = "package.json", D = ({
236
259
  rootDir: t
237
260
  }) => {
238
- const e = f.resolve(t, L);
239
- if (!p(e))
261
+ const r = g.resolve(t, U);
262
+ if (!y(r))
240
263
  throw new Error(`${t}未找到package.json文件`);
241
- const r = v(e, "utf-8");
242
- return JSON.parse(r);
264
+ const e = v(r, "utf-8");
265
+ return JSON.parse(e);
243
266
  }, gt = ({
244
267
  rootDir: t,
245
- pkgJson: e,
246
- pkgName: r,
268
+ pkgJson: r,
269
+ pkgName: e,
247
270
  isDevPkg: o
248
271
  }) => {
249
- const s = e || w({ rootDir: t }), n = o ? s.devDependencies : s.dependencies;
250
- let c = n == null ? void 0 : n[r];
272
+ const s = r || D({ rootDir: t }), n = o ? s.devDependencies : s.dependencies;
273
+ let c = n == null ? void 0 : n[e];
251
274
  if (!c) {
252
275
  const i = o ? s.dependencies : s.devDependencies;
253
- c = i == null ? void 0 : i[r], c && console.log(
254
- m.yellow(
255
- `${o ? "开发" : "生产"}依赖包${r}可能错误的安装在${o ? "dependencies" : "devDependencies"}`
276
+ c = i == null ? void 0 : i[e], c && console.log(
277
+ p.yellow(
278
+ `${o ? "开发" : "生产"}依赖包${e}可能错误的安装在${o ? "dependencies" : "devDependencies"}`
256
279
  )
257
280
  );
258
281
  return;
259
282
  }
260
- return c || console.log(m.cyan(`依赖包${r}未安装`)), c;
261
- }, Lt = ({
283
+ return c || console.log(p.cyan(`依赖包${e}未安装`)), c;
284
+ }, Ut = ({
262
285
  patchConfig: t,
263
- rootDir: e
286
+ rootDir: r
264
287
  }) => {
265
288
  if (!t)
266
289
  return;
267
- const r = w({ rootDir: e }), o = Z(r, t), s = f.resolve(e, L);
268
- b(s, JSON.stringify(o, null, 2), "utf-8");
290
+ const e = D({ rootDir: r }), o = Z(e, t), s = g.resolve(r, U);
291
+ w(s, JSON.stringify(o, null, 2), "utf-8");
269
292
  }, O = (t) => {
270
- const r = g("git rev-parse --show-toplevel", {
293
+ const e = u("git rev-parse --show-toplevel", {
271
294
  cwd: t
272
295
  }).toString();
273
- if (!r)
296
+ if (!e)
274
297
  throw new Error("获取git根目录失败");
275
- return r.trim();
276
- }, Ut = () => {
298
+ return e.trim();
299
+ }, Kt = () => {
277
300
  var t;
278
301
  try {
279
- const e = g("git symbolic-ref --short HEAD", {
302
+ const r = u("git symbolic-ref --short HEAD", {
280
303
  stdio: "ignore"
281
304
  });
282
- return (t = e == null ? void 0 : e.toString()) == null ? void 0 : t.trim();
305
+ return (t = r == null ? void 0 : r.toString()) == null ? void 0 : t.trim();
283
306
  } catch {
284
307
  try {
285
- const r = g("git rev-parse --short HEAD").toString().trim();
286
- l.skip(`当前未指向具体某个分支, 当前commit hash: ${r}`);
308
+ const e = u("git rev-parse --short HEAD").toString().trim();
309
+ l.skip(`当前未指向具体某个分支, 当前commit hash: ${e}`);
287
310
  } finally {
288
311
  return;
289
312
  }
290
313
  }
291
314
  }, ut = (t) => {
292
- const e = t.match(/moving\s+from\s+(.*)\s+to\s+(.*)/);
293
- if (e) {
294
- const [, r, o] = e;
315
+ const r = t.match(/moving\s+from\s+(.*)\s+to\s+(.*)/);
316
+ if (r) {
317
+ const [, e, o] = r;
295
318
  return {
296
- fromBranch: r.trim(),
319
+ fromBranch: e.trim(),
297
320
  toBranch: o.trim()
298
321
  };
299
322
  }
300
323
  }, I = 73, mt = (t) => {
301
- const r = u.statSync(t).mode;
302
- if ((r & I) === I)
324
+ const e = m.statSync(t).mode;
325
+ if ((e & I) === I)
303
326
  return;
304
- console.log(m.blue(`${t} 没有执行权限 添加... `));
305
- const o = r | I;
306
- u.chmodSync(t, o), console.log(m.green(`${t} 添加执行权限成功`));
327
+ console.log(p.blue(`${t} 没有执行权限 添加... `));
328
+ const o = e | I;
329
+ m.chmodSync(t, o), console.log(p.green(`${t} 添加执行权限成功`));
307
330
  };
308
- var d = /* @__PURE__ */ ((t) => (t.PRE_COMMIT = "pre-commit", t.PRE_MERGE_COMMIT = "pre-merge-commit", t.PREPARE_COMMIT_MSG = "prepare-commit-msg", t.COMMIT_MSG = "commit-msg", t.PRE_REBASE = "pre-rebase", t.POST_COMMIT = "post-commit", t.POST_MERGE = "post-merge", t.PRE_PUSH = "pre-push", t))(d || {});
331
+ var R = /* @__PURE__ */ ((t) => (t.PRE_COMMIT = "pre-commit", t.PRE_MERGE_COMMIT = "pre-merge-commit", t.PREPARE_COMMIT_MSG = "prepare-commit-msg", t.COMMIT_MSG = "commit-msg", t.PRE_REBASE = "pre-rebase", t.POST_COMMIT = "post-commit", t.POST_MERGE = "post-merge", t.PRE_PUSH = "pre-push", t))(R || {});
309
332
  const pt = "husky", ht = ".husky", dt = ({ rootDir: t }) => {
310
- const e = O(t), r = gt({
311
- rootDir: e,
312
- pkgJson: w({ rootDir: e }),
333
+ const r = O(t), e = gt({
334
+ rootDir: r,
335
+ pkgJson: D({ rootDir: r }),
313
336
  pkgName: pt,
314
337
  isDevPkg: !0
315
338
  });
316
- if (!r)
339
+ if (!e)
317
340
  throw new Error("husky版本获取失败, 可能husky未安装");
318
- const o = r.replace(/^(\^|~)/, ""), s = "<9.0.0";
319
- return tt.satisfies(o, s) ? (console.log(m.cyan(`${o}符合${s}`)), `#!/usr/bin/env sh
320
- . "$(dirname -- "$0")/_/husky.sh"`) : (console.log(m.cyan(`${o}不符合${s}`)), "");
341
+ const o = e.replace(/^(\^|~)/, ""), s = "<9.0.0";
342
+ return tt.satisfies(o, s) ? (console.log(p.cyan(`${o}符合${s}`)), `#!/usr/bin/env sh
343
+ . "$(dirname -- "$0")/_/husky.sh"`) : (console.log(p.cyan(`${o}不符合${s}`)), "");
321
344
  }, yt = ({ rootDir: t }) => {
322
- const e = O(t);
323
- return f.resolve(e, ht);
324
- }, Kt = ({
345
+ const r = O(t);
346
+ return g.resolve(r, ht);
347
+ }, Yt = ({
325
348
  hookNames: t,
326
- rootDir: e,
327
- getCode: r
349
+ rootDir: r,
350
+ getCode: e
328
351
  }) => {
329
- const o = yt({ rootDir: e });
330
- u.existsSync(o) || u.mkdirSync(o, { recursive: !0 }), t.forEach((s) => {
331
- const n = f.resolve(o, s), c = u.existsSync(n);
332
- let i = r(s);
352
+ const o = yt({ rootDir: r });
353
+ m.existsSync(o) || m.mkdirSync(o, { recursive: !0 }), t.forEach((s) => {
354
+ const n = g.resolve(o, s), c = m.existsSync(n);
355
+ let i = e(s);
333
356
  if (c)
334
- u.readFileSync(n, "utf-8").includes(i) ? console.log(
335
- m.gray(`${n} ${s}相关调用 ${i} 已存在 跳过`)
336
- ) : (u.appendFileSync(
357
+ m.readFileSync(n, "utf-8").includes(i) ? console.log(
358
+ p.gray(`${n} ${s}相关调用 ${i} 已存在 跳过`)
359
+ ) : (m.appendFileSync(
337
360
  n,
338
361
  `
339
362
  ${i}
340
363
  `
341
- ), console.log(m.green(`${n} 添加 ${s}相关调用成功`)));
364
+ ), console.log(p.green(`${n} 添加 ${s}相关调用成功`)));
342
365
  else {
343
366
  const a = dt({
344
- rootDir: e
367
+ rootDir: r
345
368
  });
346
- u.writeFileSync(
369
+ m.writeFileSync(
347
370
  n,
348
371
  `${a}
349
372
 
350
373
  ${i}
351
374
  `,
352
375
  "utf-8"
353
- ), console.log(m.green(`${n} 添加 ${s}相关调用成功`));
376
+ ), console.log(p.green(`${n} 添加 ${s}相关调用成功`));
354
377
  }
355
378
  mt(n);
356
379
  });
357
- }, Yt = [
380
+ }, Vt = [
358
381
  // HooksNameEnum.PRE_MERGE_COMMIT,
359
- d.PREPARE_COMMIT_MSG,
360
- d.COMMIT_MSG
361
- ], Vt = ({
382
+ R.PREPARE_COMMIT_MSG,
383
+ R.COMMIT_MSG
384
+ ], Wt = ({
362
385
  hookName: t,
363
- rootDir: e
386
+ rootDir: r
364
387
  }) => {
365
- const r = O(e), o = f.resolve(r, ".git");
388
+ const e = O(r), o = g.resolve(e, ".git");
366
389
  switch (t) {
367
- case d.PREPARE_COMMIT_MSG:
368
- case d.COMMIT_MSG: {
369
- const s = f.resolve(o, "MERGE_MSG");
370
- if (p(s))
390
+ case R.PREPARE_COMMIT_MSG:
391
+ case R.COMMIT_MSG: {
392
+ const s = g.resolve(o, "MERGE_MSG");
393
+ if (y(s))
371
394
  return v(s, "utf-8");
372
395
  }
373
396
  }
374
397
  return "";
375
- }, Wt = ({
376
- remoteAlias: t
398
+ }, Xt = ({
399
+ remoteAlias: t = "origin"
377
400
  } = {}) => {
378
401
  try {
379
- const e = g("git rev-parse HEAD").toString().trim(), r = g('git log -1 --pretty=format:"%an"').toString().trim(), o = g('git log -1 --pretty=format:"%ae"').toString().trim(), s = g('git log -1 --pretty=format:"%s"').toString().trim(), n = g("git config user.name").toString().trim(), c = g("git config user.email").toString().trim(), i = g("git rev-parse --abbrev-ref HEAD").toString().trim();
402
+ const r = u("git rev-parse HEAD").toString().trim(), e = u('git log -1 --pretty=format:"%an"').toString().trim(), o = u('git log -1 --pretty=format:"%ae"').toString().trim(), s = u('git log -1 --pretty=format:"%s"').toString().trim(), n = u("git config user.name").toString().trim(), c = u("git config user.email").toString().trim(), i = u("git rev-parse --abbrev-ref HEAD").toString().trim();
380
403
  let a = "";
381
404
  try {
382
- a = g(`git config --get remote.${t}.url`).toString().trim();
405
+ a = u(`git config --get remote.${t}.url`).toString().trim();
383
406
  } catch {
384
407
  l.warn("git远程仓库地址获取失败或者不存在");
385
408
  }
386
409
  return {
387
- lastHash: e,
388
- lastCommitter: r,
389
- lastCommitterPinYin: E(r, {
410
+ lastHash: r,
411
+ lastCommitter: e,
412
+ lastCommitterPinYin: E(e, {
390
413
  style: E.STYLE_NORMAL,
391
414
  heteronym: !1
392
415
  }).join(""),
@@ -404,13 +427,13 @@ ${i}
404
427
  url: a
405
428
  } : void 0
406
429
  };
407
- } catch (e) {
408
- throw l.error("获取git最后提交信息失败"), e;
430
+ } catch (r) {
431
+ throw l.error("获取git最后提交信息失败"), r;
409
432
  }
410
- }, U = (t = "") => {
411
- const e = /\s*Merge\s+branch\s+['|"](.+)['|"]\s+into\s+['|"](.+)['|"]\s*/i, r = t.match(e);
412
- if (r) {
413
- const [, i, a] = r;
433
+ }, K = (t = "") => {
434
+ const r = /\s*Merge\s+branch\s+['|"](.+)['|"]\s+into\s+['|"](.+)['|"]\s*/i, e = t.match(r);
435
+ if (e) {
436
+ const [, i, a] = e;
414
437
  return {
415
438
  fromBranch: i,
416
439
  toBranch: a
@@ -436,13 +459,13 @@ ${i}
436
459
  return {
437
460
  fromBranch: t.replace(T.MERGE, "").trim()
438
461
  };
439
- }, Xt = () => {
462
+ }, qt = () => {
440
463
  const t = process.env.GIT_REFLOG_ACTION || "";
441
464
  if (!t)
442
465
  return;
443
- const e = /merge\s+([^\s]+)\s*/i, r = t.match(e);
444
- if (r) {
445
- const [, n] = r;
466
+ const r = /merge\s+([^\s]+)\s*/i, e = t.match(r);
467
+ if (e) {
468
+ const [, n] = e;
446
469
  return {
447
470
  fromBranch: n
448
471
  };
@@ -456,19 +479,19 @@ ${i}
456
479
  }
457
480
  };
458
481
  var T = /* @__PURE__ */ ((t) => (t.CHECKOUT = "checkout", t.MERGE = "merge", t.COMMIT = "commit", t.COMMIT_MERGE = "commit (merge)", t.PULL = "pull", t.RESET = "reset", t.REBASE_START = "rebase (start)", t.REBASE_FINISH = "rebase (finish)", t.REBASE_ABORT = "rebase (abort)", t.REBASE_CONTINUE = "rebase (continue)", t))(T || {});
459
- const k = "__GIT_REPLACE_MARK__", $ = {
482
+ const j = "__GIT_REPLACE_MARK__", $ = {
460
483
  stringify(t) {
461
- return JSON.stringify(t).replace(/"/g, k);
484
+ return JSON.stringify(t).replace(/"/g, j);
462
485
  },
463
486
  parse(t) {
464
- return JSON.parse(t.replace(new RegExp(k, "g"), '"'));
487
+ return JSON.parse(t.replace(new RegExp(j, "g"), '"'));
465
488
  }
466
- }, qt = ({
489
+ }, zt = ({
467
490
  count: t = 100
468
491
  } = {}) => {
469
492
  if (t <= 0)
470
493
  return [];
471
- const e = {
494
+ const r = {
472
495
  hash: "%H",
473
496
  /** 提交信息 */
474
497
  message: "%s",
@@ -485,112 +508,112 @@ const k = "__GIT_REPLACE_MARK__", $ = {
485
508
  /** 提交日期 */
486
509
  commitTime: "%ci"
487
510
  };
488
- return g(
511
+ return u(
489
512
  `git --no-pager log --oneline -n ${t} --pretty=format:"${$.stringify(
490
- e
513
+ r
491
514
  )}"`
492
515
  ).toString().split(`
493
516
  `).map((n) => {
494
517
  const c = $.parse(n);
495
518
  return {
496
519
  ...c,
497
- mergeInfo: U(c.message)
520
+ mergeInfo: K(c.message)
498
521
  };
499
522
  });
500
- }, zt = ({
523
+ }, Qt = ({
501
524
  count: t = 100,
502
- filterItem: e = () => !0
525
+ filterItem: r = () => !0
503
526
  } = {}) => {
504
527
  if (t <= 0)
505
528
  return [];
506
- const r = {
529
+ const e = {
507
530
  hash: "%H",
508
531
  fullMessage: "%gs",
509
532
  committer: "%cn",
510
533
  committerEmail: "%ce",
511
534
  commitTime: "%ci"
512
535
  };
513
- return g(
536
+ return u(
514
537
  `git --no-pager reflog -n ${t} --pretty=format:"${$.stringify(
515
- r
538
+ e
516
539
  )}"`
517
540
  ).toString().split(`
518
- `).map((i) => $.parse(i)).filter(e).map((i) => {
519
- const { fullMessage: a, ...y } = i, R = ":", [S] = a.split(R, 1), Y = a.slice(`${S}${R}`.length), h = S.trim(), C = Y.trim();
520
- let P, D, V;
541
+ `).map((i) => $.parse(i)).filter(r).map((i) => {
542
+ const { fullMessage: a, ...f } = i, d = ":", [S] = a.split(d, 1), P = a.slice(`${S}${d}`.length), h = S.trim(), C = P.trim();
543
+ let b, B, V;
521
544
  if (h.startsWith(
522
545
  "checkout"
523
546
  /* CHECKOUT */
524
547
  ))
525
- D = ut(C);
548
+ B = ut(C);
526
549
  else if (h.startsWith(
527
550
  "merge"
528
551
  /* MERGE */
529
552
  ))
530
- P = Rt(h);
553
+ b = Rt(h);
531
554
  else if (h.startsWith(
532
555
  "commit (merge)"
533
556
  /* COMMIT_MERGE */
534
557
  )) {
535
- const B = U(C);
536
- B || l.warn(
558
+ const k = K(C);
559
+ k || l.warn(
537
560
  `${i.hash} 是合并提交 但是未从提交信息(${C})中检测到合并分支信息,推测手动更改了提交内容`
538
- ), P = B;
561
+ ), b = k;
539
562
  }
540
563
  return {
541
- ...y,
564
+ ...f,
542
565
  type: h,
543
566
  message: C,
544
- mergeInfo: P,
545
- checkoutInfo: D,
567
+ mergeInfo: b,
568
+ checkoutInfo: B,
546
569
  rebaseInfo: V
547
570
  };
548
571
  });
549
- }, Qt = (t) => {
550
- const e = O(t), r = f.resolve(e, ".git"), o = f.resolve(r, "rebase-merge");
551
- if (u.existsSync(o))
572
+ }, Zt = (t) => {
573
+ const r = O(t), e = g.resolve(r, ".git"), o = g.resolve(e, "rebase-merge");
574
+ if (m.existsSync(o))
552
575
  return !0;
553
- const s = f.resolve(r, "rebase-apply");
554
- if (u.existsSync(s))
576
+ const s = g.resolve(e, "rebase-apply");
577
+ if (m.existsSync(s))
555
578
  return !0;
556
- const n = f.resolve(r, "REBASE_HEAD");
557
- return !!u.existsSync(n);
558
- }, Zt = ({
579
+ const n = g.resolve(e, "REBASE_HEAD");
580
+ return !!m.existsSync(n);
581
+ }, te = ({
559
582
  branchName: t,
560
- version: e,
561
- remoteInfo: r
583
+ version: r,
584
+ remoteInfo: e
562
585
  }) => {
563
- r && (g(`git push ${r.alias} v${e}`, {
586
+ e && (u(`git push ${e.alias} v${r}`, {
564
587
  stdio: "inherit"
565
- }), g(`git push ${r.alias} ${t}`, {
588
+ }), u(`git push ${e.alias} ${t}`, {
566
589
  stdio: "inherit"
567
590
  }));
568
- }, x = "git@gitee.com:justsosu/done-coding-cli-assets-config.git", St = (t) => `./.DONE_CODING_CLI/CONFIG_TEMP_DIR_FOR_${t}`, K = (t) => `./assets/${t}`, Ct = (t) => `${K(t)}/index.json`, te = async ({
591
+ }, x = "git@gitee.com:justsosu/done-coding-cli-assets-config.git", St = (t) => `./.DONE_CODING_CLI/CONFIG_TEMP_DIR_FOR_${t}`, Y = (t) => `./assets/${t}`, Ct = (t) => `${Y(t)}/index.json`, ee = async ({
569
592
  moduleName: t,
570
- onSuccess: e
593
+ onSuccess: r
571
594
  }) => {
572
- console.log(m.blue(`拉取${t}配置,请稍等...`));
573
- const r = St(t), o = f.resolve(
595
+ console.log(p.blue(`拉取${t}配置,请稍等...`));
596
+ const e = St(t), o = g.resolve(
574
597
  et(),
575
- r
598
+ e
576
599
  );
577
- if (p(o))
600
+ if (y(o))
578
601
  return console.log(
579
- m.red(`${o} 已存在,请手动删除该目录再试`)
602
+ p.red(`${o} 已存在,请手动删除该目录再试`)
580
603
  ), process.exit(1);
581
604
  const s = () => {
582
605
  X(o, { recursive: !0, force: !0 });
583
606
  };
584
607
  let n;
585
608
  try {
586
- g(`git clone ${x} ${o} --depth=1`);
587
- const c = Ct(t), i = K(t), a = f.resolve(
609
+ u(`git clone ${x} ${o} --depth=1`);
610
+ const c = Ct(t), i = Y(t), a = g.resolve(
588
611
  o,
589
612
  c
590
613
  );
591
- n = JSON.parse(u.readFileSync(a, "utf-8")), process.once("exit", () => {
592
- p(o) && (console.log("发现进程退出,正在清理临时目录..."), s());
593
- }), await e({
614
+ n = JSON.parse(m.readFileSync(a, "utf-8")), process.once("exit", () => {
615
+ y(o) && (console.log("发现进程退出,正在清理临时目录..."), s());
616
+ }), await r({
594
617
  repoUrl: x,
595
618
  config: n,
596
619
  cliConfigFileRelativePath: c,
@@ -605,44 +628,45 @@ const k = "__GIT_REPLACE_MARK__", $ = {
605
628
  export {
606
629
  st as EditorTypeEnum,
607
630
  T as GitRefLogTypeEnum,
608
- d as HooksNameEnum,
609
- Yt as SUPPORT_GET_COMMIT_BY_HOOKS_NAMES,
610
- ue as _curry,
611
- ae as _get,
612
- fe as _set,
613
- Kt as addHuskyHooks,
614
- Lt as addPackageConfig,
615
- oe as chalk,
616
- Qt as checkCurrentIsRebasing,
617
- jt as createMainCommand,
618
- Ht as createSubcommand,
619
- xt as decryptAES,
631
+ R as HooksNameEnum,
632
+ Vt as SUPPORT_GET_COMMIT_BY_HOOKS_NAMES,
633
+ me as _curry,
634
+ fe as _get,
635
+ ge as _set,
636
+ Yt as addHuskyHooks,
637
+ Ut as addPackageConfig,
638
+ se as chalk,
639
+ Zt as checkCurrentIsRebasing,
640
+ Ht as createMainCommand,
641
+ Nt as createSubcommand,
642
+ jt as decryptAES,
620
643
  kt as encryptAES,
621
644
  mt as fileAddX,
622
- Vt as getCommitByHookName,
645
+ Wt as getCommitByHookName,
623
646
  Ft as getConfigFileCommonOptions,
624
- qt as getCurrentBranchLastCommitList,
625
- Ut as getCurrentBranchName,
647
+ zt as getCurrentBranchLastCommitList,
648
+ Kt as getCurrentBranchName,
626
649
  nt as getEditorType,
627
- Wt as getGitLastCommitInfo,
650
+ Xt as getGitLastCommitInfo,
628
651
  O as getGitProjectDir,
629
- zt as getLastReflogList,
630
- w as getPackageJson,
652
+ Qt as getLastReflogList,
653
+ D as getPackageJson,
631
654
  gt as getRelyPkgVersion,
632
- lt as getRootDirOptions,
655
+ ft as getRootDirOptions,
656
+ xt as getRootScriptName,
633
657
  Jt as getUseDefaultConfig,
634
- ft as initConfigFile,
635
- Nt as initHandlerCommon,
636
- ie as json5,
658
+ lt as initConfigFile,
659
+ Gt as initHandlerCommon,
660
+ ce as json5,
637
661
  l as log,
638
662
  Bt as lookForParentTarget,
639
663
  at as openFileInEditor,
640
- Zt as pushGitPublishInfoToRemote,
641
- te as readCliConfig,
642
- Gt as readConfigFile,
664
+ te as pushGitPublishInfoToRemote,
665
+ ee as readCliConfig,
666
+ Lt as readConfigFile,
643
667
  ut as resolveCheckoutInfoByRefInfo,
644
- U as resolveMergeInfoByCommitMsg,
645
- Xt as resolveMergeInfoByGitReflogAction,
668
+ K as resolveMergeInfoByCommitMsg,
669
+ qt as resolveMergeInfoByGitReflogAction,
646
670
  Rt as resolveMergeInfoByRefType,
647
- G as xPrompts
671
+ L as xPrompts
648
672
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@done-coding/cli-utils",
3
- "version": "0.7.1",
3
+ "version": "0.7.3-alpha.0",
4
4
  "description": "cli utils",
5
5
  "private": false,
6
6
  "module": "es/index.mjs",
@@ -67,5 +67,5 @@
67
67
  "semver": "^7.5.4",
68
68
  "yargs": "^17.7.2"
69
69
  },
70
- "gitHead": "299ab3991d6842c83d65816dfba6efd175aa885a"
70
+ "gitHead": "9d9a2e60f255038a0f065e3a4e6d6d35488408ae"
71
71
  }
package/types/index.d.ts CHANGED
@@ -68,6 +68,8 @@ export declare interface CliInfo {
68
68
  };
69
69
  /** 处理函数 */
70
70
  handler?: CommandModule["handler"];
71
+ /** 根命令 */
72
+ rootScriptName?: string;
71
73
  }
72
74
 
73
75
  export { CommandModule }
@@ -192,6 +194,12 @@ export declare const getRootDirOptions: (rootDirDefault?: string) => {
192
194
  rootDir: YargsOptions;
193
195
  };
194
196
 
197
+ /** 获取根命令名称 */
198
+ export declare const getRootScriptName: ({ rootScriptName, packageJson, }: {
199
+ rootScriptName?: string | undefined;
200
+ packageJson?: Pick<PackageJson, "name" | "bin"> | undefined;
201
+ }) => string | undefined;
202
+
195
203
  /** 获取是否使用默认配置 */
196
204
  export declare const getUseDefaultConfig: () => Promise<any>;
197
205