@done-coding/cli-utils 0.3.5 → 0.4.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,21 +1,21 @@
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";
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
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";
11
- import v from "json5";
7
+ import D from "prompts";
8
+ import H from "yargs";
9
+ import { hideBin as A } from "yargs/helpers";
10
+ import { execSync as h } from "node:child_process";
11
+ import C 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 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";
17
17
  const c = Object.assign(
18
- (e, ...r) => console.log(...r.map((t) => P[e](t))),
18
+ (e, ...r) => console.log(...r.map((t) => O[e](t))),
19
19
  {
20
20
  /** 成功 */
21
21
  success: (...e) => c("green", ...e),
@@ -30,19 +30,19 @@ const c = Object.assign(
30
30
  /** 跳过 */
31
31
  skip: (...e) => c("gray", ...e)
32
32
  }
33
- ), Q = (e, {
33
+ ), X = (e, {
34
34
  /** 当前目录 */
35
35
  currentDir: r = process.cwd(),
36
36
  /** 优先找最远的父目录 */
37
37
  isFindFarthest: t = !0
38
38
  } = {}) => {
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);
42
- if (b.existsSync(i))
43
- return n;
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
+ for (; n.length; ) {
41
+ const o = t ? n.shift() : n.pop(), s = a.join(o, e);
42
+ if (E.existsSync(s))
43
+ return o;
44
44
  }
45
- }, C = "aes-256-cbc", S = 16, p = "hex", d = ":";
45
+ }, v = "aes-256-cbc", S = 16, p = "hex", d = ":";
46
46
  function $(e) {
47
47
  return l.pbkdf2Sync(
48
48
  e,
@@ -55,84 +55,84 @@ function $(e) {
55
55
  "sha256"
56
56
  );
57
57
  }
58
- function X({
58
+ function Z({
59
59
  text: e,
60
60
  secretKey: r
61
61
  }) {
62
62
  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}`;
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
68
  } catch (t) {
69
69
  return c.error(
70
70
  `加密失败: ${t instanceof Error ? t.message : String(t)}`
71
71
  ), "";
72
72
  }
73
73
  }
74
- function Z({
74
+ function ee({
75
75
  encryptedText: e,
76
76
  secretKey: r
77
77
  }) {
78
78
  try {
79
79
  if (!e.includes(d))
80
80
  return "";
81
- const t = $(r), [o, n] = e.split(d);
82
- if (o.length !== S * 2)
81
+ const t = $(r), [n, o] = e.split(d);
82
+ if (n.length !== S * 2)
83
83
  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();
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
87
  } catch (t) {
88
88
  return c.error(
89
89
  `解密失败: ${t instanceof Error ? t.message : String(t)}`
90
90
  ), "";
91
91
  }
92
92
  }
93
- const x = (...e) => {
93
+ const k = (...e) => {
94
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);
95
+ return D(r, {
96
+ onCancel(n) {
97
+ return c.error(`退出${n == null ? void 0 : n.name}输入`), process.exit(1);
98
98
  },
99
99
  ...t
100
100
  });
101
- }, F = (e, r) => {
101
+ }, R = (e, r) => {
102
102
  e ? c.error(e) : c.error(r.message), r != null && r.stack && c.error(r.stack), process.exit(1);
103
- }, T = () => {
104
- const e = R(process.argv);
105
- return E(e);
106
- }, j = (e, {
103
+ }, _ = () => {
104
+ const e = A(process.argv);
105
+ return H(e);
106
+ }, w = (e, {
107
107
  usage: r,
108
108
  version: t,
109
- demandCommandCount: o,
110
- options: n,
111
- positionals: i,
112
- subcommands: a
109
+ demandCommandCount: n,
110
+ options: o,
111
+ positionals: s,
112
+ subcommands: f
113
113
  }) => {
114
- let s = e.strict();
115
- r && (s = s.usage(`Usage: ${r}`)), o && (s = s.demandCommand(o));
114
+ let i = e.strict();
115
+ r && (i = i.usage(`Usage: ${r}`)), n && (i = i.demandCommand(n));
116
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;
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
120
  return e ? e(t) : t;
121
- }, re = (e) => {
122
- const { command: r, describe: t, handler: o = () => {
123
- }, ...n } = e;
121
+ }, te = (e) => {
122
+ const { command: r, describe: t, handler: n = () => {
123
+ }, ...o } = e;
124
124
  return {
125
125
  command: r,
126
126
  describe: t,
127
- builder(i) {
128
- return j(i, n);
127
+ builder(s) {
128
+ return w(s, o);
129
129
  },
130
- handler: o
130
+ handler: n
131
131
  };
132
132
  };
133
- var _ = /* @__PURE__ */ ((e) => (e.VSCODE = "VsCode", e.CURSOR = "Cursor", e.OTHER = "其他", e))(_ || {});
134
- const A = async () => {
135
- const { editorType: e } = await x([
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([
136
136
  {
137
137
  name: "editorType",
138
138
  type: "select",
@@ -149,32 +149,32 @@ const A = async () => {
149
149
  }
150
150
  ]);
151
151
  return e;
152
- }, B = {
152
+ }, T = {
153
153
  Cursor: "cursor",
154
154
  VsCode: "code"
155
- }, N = (e, r, t) => {
155
+ }, B = (e, r, t) => {
156
156
  try {
157
- y(`${e} -v`, { stdio: "ignore" }), y(`${e} ${r}`);
157
+ h(`${e} -v`, { stdio: "ignore" }), h(`${e} ${r}`);
158
158
  } catch {
159
159
  t();
160
160
  }
161
- }, V = (e, r) => {
162
- const t = (o) => c.info(`
163
- ${o}, 请用编辑器打开 ${e} 进行编辑
161
+ }, J = (e, r) => {
162
+ const t = (n) => c.info(`
163
+ ${n}, 请用编辑器打开 ${e} 进行编辑
164
164
  `);
165
165
  switch (r) {
166
166
  case "Cursor":
167
167
  case "VsCode": {
168
- const o = B[r];
169
- N(o, e, () => {
170
- t(`${o}命令未安装`);
168
+ const n = T[r];
169
+ B(n, e, () => {
170
+ t(`${n}命令未安装`);
171
171
  });
172
172
  break;
173
173
  }
174
174
  default:
175
175
  t("其他编辑器");
176
176
  }
177
- }, L = (e = process.cwd()) => ({
177
+ }, V = (e = process.cwd()) => ({
178
178
  /** 必须保留 */
179
179
  rootDir: {
180
180
  type: "string",
@@ -183,12 +183,12 @@ const A = async () => {
183
183
  /** 必须设置默认值 */
184
184
  default: e
185
185
  }
186
- }), te = ({
186
+ }), oe = ({
187
187
  configPathDefault: e,
188
188
  rootDirDefault: r
189
189
  }) => ({
190
190
  /** 必须保留 */
191
- ...L(r),
191
+ ...V(r),
192
192
  /** 必须保留 */
193
193
  configPath: {
194
194
  type: "string",
@@ -197,56 +197,67 @@ const A = async () => {
197
197
  /** 必须设置默认值 */
198
198
  default: e
199
199
  }
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, {
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, {
203
203
  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, {
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
206
  onFileGenerated: t,
207
- edit: o = !1
207
+ edit: n = !1
208
208
  } = {}) => {
209
- const n = await I(e, r);
210
- if (t == null || t(n), o) {
211
- const i = await A();
212
- V(r.configPath, i);
209
+ const o = await L(e, r);
210
+ if (t == null || t(o), n) {
211
+ const s = await N();
212
+ J(r.configPath, s);
213
213
  }
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;
214
+ }, se = async (e, r) => {
215
+ const { configPath: t, rootDir: n } = e, o = a.resolve(n, t);
216
+ if (!m(o)) {
217
+ if (r)
218
+ return c.info("配置文件不存在,使用onNotExists返回值"), r();
219
+ const s = `配置文件不存在 ${o}`;
220
+ throw new Error(s);
219
221
  }
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({
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
225
  name: "useDefaultConfig",
224
226
  type: "confirm",
225
227
  message: "使用默认模板配置",
226
228
  initial: !0
227
229
  });
228
230
  return e;
231
+ }, I = "./package.json", ce = ({
232
+ rootDir: e
233
+ }) => {
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);
229
239
  };
230
240
  export {
231
- _ 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,
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,
247
258
  c as log,
248
- Q as lookForParentTarget,
249
- V as openFileInEditor,
250
- ne as readConfigFile,
251
- x as xPrompts
259
+ X as lookForParentTarget,
260
+ J as openFileInEditor,
261
+ se as readConfigFile,
262
+ k as xPrompts
252
263
  };
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.4.0-alpha.0",
4
4
  "description": "cli utils",
5
5
  "private": false,
6
6
  "module": "es/index.mjs",
@@ -61,5 +61,5 @@
61
61
  "prompts": "^2.4.2",
62
62
  "yargs": "^17.7.2"
63
63
  },
64
- "gitHead": "eccf973ebc52fc3b06d26391d371e8d2d1629a61"
64
+ "gitHead": "360b14a641064f732b01ffd6339c2f1963945b61"
65
65
  }
@@ -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/index.d.ts CHANGED
@@ -7,3 +7,4 @@ export * from "./editor";
7
7
  export * from "./config-file";
8
8
  export * from "./json5";
9
9
  export * from "./lodash";
10
+ export * from "./package-json";
@@ -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;