@done-coding/cli-utils 0.3.5 → 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
- import P from "chalk";
3
- import { default as ae } from "chalk";
4
- import f from "node:path";
5
- import b, { existsSync as h, mkdirSync as k, writeFileSync as g, readFileSync as m } from "node:fs";
6
- import l from "crypto";
7
- import H from "prompts";
8
- import E from "yargs";
9
- import { hideBin as R } from "yargs/helpers";
10
- import { execSync as y } from "node:child_process";
2
+ import O from "chalk";
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
+ import D from "prompts";
8
+ import N from "yargs";
9
+ import { hideBin as A } from "yargs/helpers";
10
+ import { execSync as a } from "node:child_process";
11
11
  import v from "json5";
12
12
  export * from "json5";
13
- import { default as le } from "json5";
14
- import { default as de } from "lodash.get";
15
- import { default as me } from "lodash.set";
16
- import { default as he } 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) => P[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
- ), Q = (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 o = f.resolve(r).split(f.sep).map((n, i, a) => i ? f.join(a.slice(0, i).join(f.sep), n) : n);
40
- for (; o.length; ) {
41
- const n = t ? o.shift() : o.pop(), i = f.join(n, e);
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);
41
+ for (; n.length; ) {
42
+ const o = e ? n.shift() : n.pop(), i = g.join(o, t);
42
43
  if (b.existsSync(i))
43
- return n;
44
+ return o;
44
45
  }
45
- }, C = "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 X({
59
- text: e,
59
+ function rt({
60
+ text: t,
60
61
  secretKey: r
61
62
  }) {
62
63
  try {
63
- const t = $(r), o = l.randomBytes(S), n = l.createCipheriv(C, t, o);
64
- let i = n.update(e);
65
- i = Buffer.concat([i, n.final()]);
66
- const a = o.toString(p), s = i.toString(p);
67
- return `${a}${d}${s}`;
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 Z({
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), [o, n] = e.split(d);
82
- if (o.length !== S * 2)
82
+ const e = w(r), [n, o] = t.split(h);
83
+ if (n.length !== P * 2)
83
84
  return "";
84
- const i = Buffer.from(o, p), a = Buffer.from(n, p), s = l.createDecipheriv(C, t, i);
85
- let u = s.update(a);
86
- return u = Buffer.concat([u, s.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 x = (...e) => {
94
- const [r, t = {}] = e;
95
- return H(r, {
96
- onCancel(o) {
97
- return c.error(`退出${o == null ? void 0 : o.name}输入`), process.exit(1);
94
+ const E = (...t) => {
95
+ const [r, e = {}] = t;
96
+ return D(r, {
97
+ onCancel(n) {
98
+ return c.error(`退出${n == null ? void 0 : n.name}输入`), process.exit(1);
98
99
  },
99
- ...t
100
+ ...e
100
101
  });
101
- }, F = (e, r) => {
102
- e ? c.error(e) : c.error(r.message), r != null && r.stack && c.error(r.stack), process.exit(1);
102
+ }, R = (t, r) => {
103
+ t ? c.error(t) : c.error(r.message), r != null && r.stack && c.error(r.stack), process.exit(1);
103
104
  }, T = () => {
104
- const e = R(process.argv);
105
- return E(e);
106
- }, j = (e, {
105
+ const t = A(process.argv);
106
+ return N(t);
107
+ }, j = (t, {
107
108
  usage: r,
108
- version: t,
109
- demandCommandCount: o,
110
- options: n,
109
+ version: e,
110
+ demandCommandCount: n,
111
+ options: o,
111
112
  positionals: i,
112
- subcommands: a
113
+ subcommands: l
113
114
  }) => {
114
- let s = e.strict();
115
- r && (s = s.usage(`Usage: ${r}`)), o && (s = s.demandCommand(o));
116
- const u = "help";
117
- return s = s.help(u), t ? s = s.version(t).alias("h", u).alias("v", "version") : s = s.alias("h", u), n && (s = s.options(n)), i && (s = Object.entries(i).reduce((w, [D, O]) => w.positional(D, O), s)), a && (s = s.command(a)), s;
118
- }, ee = async ({ handler: e, ...r }) => {
119
- const t = await j(T(), r).fail(F).argv;
120
- return e ? e(t) : t;
121
- }, re = (e) => {
122
- const { command: r, describe: t, handler: o = () => {
123
- }, ...n } = 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
+ describe: e,
127
128
  builder(i) {
128
- return j(i, n);
129
+ return j(i, o);
129
130
  },
130
- handler: o
131
+ handler: n
131
132
  };
132
133
  };
133
- var _ = /* @__PURE__ */ ((e) => (e.VSCODE = "VsCode", e.CURSOR = "Cursor", e.OTHER = "其他", e))(_ || {});
134
- const A = async () => {
135
- const { editorType: e } = await x([
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,105 +149,163 @@ const A = async () => {
148
149
  }))
149
150
  }
150
151
  ]);
151
- return e;
152
- }, B = {
152
+ return t;
153
+ }, F = {
153
154
  Cursor: "cursor",
154
155
  VsCode: "code"
155
- }, N = (e, r, t) => {
156
+ }, M = (t, r, e) => {
156
157
  try {
157
- y(`${e} -v`, { stdio: "ignore" }), y(`${e} ${r}`);
158
+ a(`${t} -v`, { stdio: "ignore" }), a(`${t} ${r}`);
158
159
  } catch {
159
- t();
160
+ e();
160
161
  }
161
- }, V = (e, r) => {
162
- const t = (o) => c.info(`
163
- ${o}, 请用编辑器打开 ${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 o = B[r];
169
- N(o, e, () => {
170
- t(`${o}命令未安装`);
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
- }, L = (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
- }), te = ({
187
- configPathDefault: e,
187
+ }), it = ({
188
+ configPathDefault: t,
188
189
  rootDirDefault: r
189
190
  }) => ({
190
191
  /** 必须保留 */
191
- ...L(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
- }), I = async (e, r) => {
201
- const { configPath: t, rootDir: o } = r, n = f.resolve(o, t), i = f.dirname(n);
202
- return h(i) || k(i, {
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
- }), n.endsWith(".json5") ? (c.info(`json5模式写入 ${n}`), g(n, v.stringify(e, null, 2)), n) : (c.info(`json模式写入 ${n}`), g(n, JSON.stringify(e, null, 2)), n);
205
- }, oe = async (e, r, {
206
- onFileGenerated: t,
207
- edit: o = !1
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,
208
+ edit: n = !1
208
209
  } = {}) => {
209
- const n = await I(e, r);
210
- if (t == null || t(n), o) {
211
- const i = await A();
212
- V(r.configPath, i);
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
- }, ne = async (e) => {
215
- const { configPath: r, rootDir: t } = e, o = f.resolve(t, r);
216
- if (!h(o)) {
217
- c.warn(`配置文件不存在 ${o}`);
218
- return;
215
+ }, ct = async (t, r) => {
216
+ const { configPath: e, rootDir: n } = t, o = g.resolve(n, e);
217
+ if (!y(o)) {
218
+ if (r)
219
+ return c.info("配置文件不存在,使用onNotExists返回值"), r();
220
+ const i = `配置文件不存在 ${o}`;
221
+ throw new Error(i);
219
222
  }
220
- return o.endsWith(".json5") ? (c.info(`json5模式解析 ${o}`), v.parse(m(o, "utf8"))) : (c.info(`json模式解析 ${o}`), JSON.parse(m(o, "utf8")));
221
- }, se = async () => {
222
- const { useDefaultConfig: e } = await x({
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({
223
226
  name: "useDefaultConfig",
224
227
  type: "confirm",
225
228
  message: "使用默认模板配置",
226
229
  initial: !0
227
230
  });
228
- return e;
231
+ return t;
232
+ }, Y = "./package.json", ft = ({
233
+ rootDir: t
234
+ }) => {
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
+ }));
229
285
  };
230
286
  export {
231
287
  _ as EditorTypeEnum,
232
- he as _curry,
233
- de as _get,
234
- me as _set,
235
- ae as chalk,
236
- ee as createMainCommand,
237
- re as createSubcommand,
238
- Z as decryptAES,
239
- X as encryptAES,
240
- te as getConfigFileCommonOptions,
241
- A as getEditorType,
242
- L as getRootDirOptions,
243
- se as getUseDefaultConfig,
244
- I as initConfigFile,
245
- oe as initHandlerCommon,
246
- le as json5,
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,
247
305
  c as log,
248
- Q as lookForParentTarget,
249
- V as openFileInEditor,
250
- ne as readConfigFile,
251
- x as xPrompts
306
+ tt as lookForParentTarget,
307
+ B as openFileInEditor,
308
+ gt as pushGitPublishInfoToRemote,
309
+ ct as readConfigFile,
310
+ E as xPrompts
252
311
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@done-coding/cli-utils",
3
- "version": "0.3.5",
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": "eccf973ebc52fc3b06d26391d371e8d2d1629a61"
66
+ "gitHead": "86cdd3eaf8cc8be3e48a812b93fd7c2110fcc879"
65
67
  }
@@ -32,6 +32,6 @@ export declare const initHandlerCommon: <T>(content: T, argv: CliHandlerArgv<Ini
32
32
  edit?: boolean | undefined;
33
33
  }) => Promise<void>;
34
34
  /** 读取配置文件 */
35
- export declare const readConfigFile: <T>(argv: CliHandlerArgv<ReadConfigFileOptions>) => Promise<T | undefined>;
35
+ export declare const readConfigFile: <T>(options: ReadConfigFileOptions, onNotExists?: (() => T) | undefined) => Promise<T>;
36
36
  /** 获取是否使用默认配置 */
37
37
  export declare const getUseDefaultConfig: () => Promise<any>;
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
@@ -7,3 +7,5 @@ export * from "./editor";
7
7
  export * from "./config-file";
8
8
  export * from "./json5";
9
9
  export * from "./lodash";
10
+ export * from "./package-json";
11
+ export * from "./git";
@@ -0,0 +1,7 @@
1
+ export interface PackageJson {
2
+ name: string;
3
+ version?: string;
4
+ }
5
+ export declare const getPackageJson: <R extends PackageJson>({ rootDir, }: {
6
+ rootDir: string;
7
+ }) => R;