@done-coding/cli-utils 0.3.1-alpha.0 → 0.3.2

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,8 +1,8 @@
1
1
  #!/usr/bin/env node
2
- import D from "chalk";
3
- import { default as ce } from "chalk";
4
- import a from "node:path";
5
- import O, { existsSync as h, mkdirSync as k, writeFileSync as g, readFileSync as m } from "node:fs";
2
+ import P from "chalk";
3
+ import { default as ar } 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
6
  import l from "crypto";
7
7
  import H from "prompts";
8
8
  import E from "yargs";
@@ -10,37 +10,42 @@ import { hideBin as F } from "yargs/helpers";
10
10
  import { execSync as y } from "node:child_process";
11
11
  import v from "json5";
12
12
  export * from "json5";
13
- import { default as ue } from "json5";
14
- import { default as pe } from "lodash.get";
15
- import { default as ge } from "lodash.set";
16
- import { default as ye } from "lodash.curry";
17
- const i = Object.assign(
18
- (e, ...o) => console.log(...o.map((r) => D[e](r))),
13
+ import { default as lr } from "json5";
14
+ import { default as dr } from "lodash.get";
15
+ import { default as mr } from "lodash.set";
16
+ import { default as hr } from "lodash.curry";
17
+ const c = Object.assign(
18
+ (r, ...e) => console.log(...e.map((t) => P[r](t))),
19
19
  {
20
20
  /** 成功 */
21
- success: (...e) => i("green", ...e),
21
+ success: (...r) => c("green", ...r),
22
22
  /** /步骤 */
23
- stage: (...e) => i("blue", ...e),
23
+ stage: (...r) => c("blue", ...r),
24
24
  /** 提示信息 */
25
- info: (...e) => i("cyan", ...e),
25
+ info: (...r) => c("cyan", ...r),
26
26
  /** 警告 */
27
- warn: (...e) => i("yellow", ...e),
27
+ warn: (...r) => c("yellow", ...r),
28
28
  /** 错误 */
29
- error: (...e) => i("red", ...e),
29
+ error: (...r) => c("red", ...r),
30
30
  /** 跳过 */
31
- skip: (...e) => i("gray", ...e)
31
+ skip: (...r) => c("gray", ...r)
32
32
  }
33
- ), G = (e, o = process.cwd()) => {
34
- const r = a.resolve(o).split(a.sep).map((t, n, c) => n ? a.join(c.slice(0, n).join(a.sep), t) : t);
35
- for (; r.length; ) {
36
- const t = r.pop(), n = a.join(t, e);
37
- if (O.existsSync(n))
38
- return t;
33
+ ), Q = (r, {
34
+ /** 当前目录 */
35
+ currentDir: e = process.cwd(),
36
+ /** 优先找最远的父目录 */
37
+ isFindFarthest: t = !0
38
+ } = {}) => {
39
+ const o = f.resolve(e).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, r);
42
+ if (b.existsSync(i))
43
+ return n;
39
44
  }
40
45
  }, C = "aes-256-cbc", S = 16, p = "hex", d = ":";
41
- function $(e) {
46
+ function $(r) {
42
47
  return l.pbkdf2Sync(
43
- e,
48
+ r,
44
49
  "done-coding-cli-salt",
45
50
  // 使用固定的盐值
46
51
  1e4,
@@ -50,84 +55,84 @@ function $(e) {
50
55
  "sha256"
51
56
  );
52
57
  }
53
- function Q({
54
- text: e,
55
- secretKey: o
58
+ function X({
59
+ text: r,
60
+ secretKey: e
56
61
  }) {
57
62
  try {
58
- const r = $(o), t = l.randomBytes(S), n = l.createCipheriv(C, r, t);
59
- let c = n.update(e);
60
- c = Buffer.concat([c, n.final()]);
61
- const u = t.toString(p), s = c.toString(p);
62
- return `${u}${d}${s}`;
63
- } catch (r) {
64
- return i.error(
65
- `加密失败: ${r instanceof Error ? r.message : String(r)}`
63
+ const t = $(e), o = l.randomBytes(S), n = l.createCipheriv(C, t, o);
64
+ let i = n.update(r);
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) {
69
+ return c.error(
70
+ `加密失败: ${t instanceof Error ? t.message : String(t)}`
66
71
  ), "";
67
72
  }
68
73
  }
69
- function X({
70
- encryptedText: e,
71
- secretKey: o
74
+ function Z({
75
+ encryptedText: r,
76
+ secretKey: e
72
77
  }) {
73
78
  try {
74
- if (!e.includes(d))
79
+ if (!r.includes(d))
75
80
  return "";
76
- const r = $(o), [t, n] = e.split(d);
77
- if (t.length !== S * 2)
81
+ const t = $(e), [o, n] = r.split(d);
82
+ if (o.length !== S * 2)
78
83
  return "";
79
- const c = Buffer.from(t, p), u = Buffer.from(n, p), s = l.createDecipheriv(C, r, c);
80
- let f = s.update(u);
81
- return f = Buffer.concat([f, s.final()]), f.toString();
82
- } catch (r) {
83
- return i.error(
84
- `解密失败: ${r instanceof Error ? r.message : String(r)}`
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) {
88
+ return c.error(
89
+ `解密失败: ${t instanceof Error ? t.message : String(t)}`
85
90
  ), "";
86
91
  }
87
92
  }
88
- const x = (...e) => {
89
- const [o, r = {}] = e;
90
- return H(o, {
91
- onCancel(t) {
92
- return i.error(`退出${t == null ? void 0 : t.name}输入`), process.exit(1);
93
+ const x = (...r) => {
94
+ const [e, t = {}] = r;
95
+ return H(e, {
96
+ onCancel(o) {
97
+ return c.error(`退出${o == null ? void 0 : o.name}输入`), process.exit(1);
93
98
  },
94
- ...r
99
+ ...t
95
100
  });
96
- }, R = (e, o) => {
97
- e ? i.error(e) : i.error(o.message), o != null && o.stack && i.error(o.stack), process.exit(1);
101
+ }, R = (r, e) => {
102
+ r ? c.error(r) : c.error(e.message), e != null && e.stack && c.error(e.stack), process.exit(1);
98
103
  }, T = () => {
99
- const e = F(process.argv);
100
- return E(e);
101
- }, j = (e, {
102
- usage: o,
103
- version: r,
104
- demandCommandCount: t,
104
+ const r = F(process.argv);
105
+ return E(r);
106
+ }, j = (r, {
107
+ usage: e,
108
+ version: t,
109
+ demandCommandCount: o,
105
110
  options: n,
106
- positionals: c,
107
- subcommands: u
111
+ positionals: i,
112
+ subcommands: a
108
113
  }) => {
109
- let s = e.strict();
110
- o && (s = s.usage(`Usage: ${o}`)), t && (s = s.demandCommand(t));
111
- const f = "help";
112
- return s = s.help(f), r ? s = s.version(r).alias("h", f).alias("v", "version") : s = s.alias("h", f), n && (s = s.options(n)), c && (s = Object.entries(c).reduce((w, [P, b]) => w.positional(P, b), s)), u && (s = s.command(u)), s;
113
- }, Z = async ({ handler: e, ...o }) => {
114
- const r = await j(T(), o).fail(R).argv;
115
- return e ? e(r) : r;
116
- }, ee = (e) => {
117
- const { command: o, describe: r, handler: t = () => {
118
- }, ...n } = e;
114
+ let s = r.strict();
115
+ e && (s = s.usage(`Usage: ${e}`)), 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
+ }, rr = async ({ handler: r, ...e }) => {
119
+ const t = await j(T(), e).fail(R).argv;
120
+ return r ? r(t) : t;
121
+ }, er = (r) => {
122
+ const { command: e, describe: t, handler: o = () => {
123
+ }, ...n } = r;
119
124
  return {
120
- command: o,
121
- describe: r,
122
- builder(c) {
123
- return j(c, n);
125
+ command: e,
126
+ describe: t,
127
+ builder(i) {
128
+ return j(i, n);
124
129
  },
125
- handler: t
130
+ handler: o
126
131
  };
127
132
  };
128
- var _ = /* @__PURE__ */ ((e) => (e.VSCODE = "VsCode", e.CURSOR = "Cursor", e.OTHER = "其他", e))(_ || {});
133
+ var _ = /* @__PURE__ */ ((r) => (r.VSCODE = "VsCode", r.CURSOR = "Cursor", r.OTHER = "其他", r))(_ || {});
129
134
  const A = async () => {
130
- const { editorType: e } = await x([
135
+ const { editorType: r } = await x([
131
136
  {
132
137
  name: "editorType",
133
138
  type: "select",
@@ -137,104 +142,109 @@ const A = async () => {
137
142
  "VsCode",
138
143
  "其他"
139
144
  /* OTHER */
140
- ].map((o) => ({
141
- title: o,
142
- value: o
145
+ ].map((e) => ({
146
+ title: e,
147
+ value: e
143
148
  }))
144
149
  }
145
150
  ]);
146
- return e;
151
+ return r;
147
152
  }, B = {
148
153
  Cursor: "cursor",
149
154
  VsCode: "code"
150
- }, N = (e, o, r) => {
155
+ }, N = (r, e, t) => {
151
156
  try {
152
- y(`${e} -v`, { stdio: "ignore" }), y(`${e} ${o}`);
157
+ y(`${r} -v`, { stdio: "ignore" }), y(`${r} ${e}`);
153
158
  } catch {
154
- r();
159
+ t();
155
160
  }
156
- }, V = (e, o) => {
157
- const r = (t) => i.info(`
158
- ${t}, 请用编辑器打开 ${e} 进行编辑
161
+ }, V = (r, e) => {
162
+ const t = (o) => c.info(`
163
+ ${o}, 请用编辑器打开 ${r} 进行编辑
159
164
  `);
160
- switch (o) {
165
+ switch (e) {
161
166
  case "Cursor":
162
167
  case "VsCode": {
163
- const t = B[o];
164
- N(t, e, () => {
165
- r(`${t}命令未安装`);
168
+ const o = B[e];
169
+ N(o, r, () => {
170
+ t(`${o}命令未安装`);
166
171
  });
167
172
  break;
168
173
  }
169
174
  default:
170
- r("其他编辑器");
175
+ t("其他编辑器");
171
176
  }
172
- }, re = ({
173
- configPathDefault: e
174
- }) => ({
177
+ }, L = (r = process.cwd()) => ({
175
178
  /** 必须保留 */
176
179
  rootDir: {
177
180
  type: "string",
178
181
  alias: "r",
179
182
  describe: "运行目录",
180
183
  /** 必须设置默认值 */
181
- default: process.cwd()
182
- },
184
+ default: r
185
+ }
186
+ }), tr = ({
187
+ configPathDefault: r,
188
+ rootDirDefault: e
189
+ }) => ({
190
+ /** 必须保留 */
191
+ ...L(e),
183
192
  /** 必须保留 */
184
193
  configPath: {
185
194
  type: "string",
186
195
  alias: "c",
187
196
  describe: "配置文件相对路径",
188
197
  /** 必须设置默认值 */
189
- default: e
198
+ default: r
190
199
  }
191
- }), L = async (e, o) => {
192
- const { configPath: r, rootDir: t } = o, n = a.resolve(t, r), c = a.dirname(n);
193
- return h(c) || k(c, {
200
+ }), I = async (r, e) => {
201
+ const { configPath: t, rootDir: o } = e, n = f.resolve(o, t), i = f.dirname(n);
202
+ return h(i) || k(i, {
194
203
  recursive: !0
195
- }), n.endsWith(".json5") ? (i.info(`json5模式写入 ${n}`), g(n, v.stringify(e, null, 2)), n) : (i.info(`json模式写入 ${n}`), g(n, JSON.stringify(e, null, 2)), n);
196
- }, te = async (e, o, {
197
- onFileGenerated: r
204
+ }), n.endsWith(".json5") ? (c.info(`json5模式写入 ${n}`), g(n, v.stringify(r, null, 2)), n) : (c.info(`json模式写入 ${n}`), g(n, JSON.stringify(r, null, 2)), n);
205
+ }, or = async (r, e, {
206
+ onFileGenerated: t
198
207
  } = {}) => {
199
- const t = await L(e, o);
200
- r == null || r(t);
208
+ const o = await I(r, e);
209
+ t == null || t(o);
201
210
  const n = await A();
202
- V(o.configPath, n);
203
- }, oe = async (e) => {
204
- const { configPath: o, rootDir: r } = e, t = a.resolve(r, o);
205
- if (!h(t)) {
206
- i.warn(`配置文件不存在 ${t}`);
211
+ V(e.configPath, n);
212
+ }, nr = async (r) => {
213
+ const { configPath: e, rootDir: t } = r, o = f.resolve(t, e);
214
+ if (!h(o)) {
215
+ c.warn(`配置文件不存在 ${o}`);
207
216
  return;
208
217
  }
209
- return t.endsWith(".json5") ? (i.info(`json5模式解析 ${t}`), v.parse(m(t, "utf8"))) : (i.info(`json模式解析 ${t}`), JSON.parse(m(t, "utf8")));
210
- }, ne = async () => {
211
- const { useDefaultConfig: e } = await x({
218
+ return o.endsWith(".json5") ? (c.info(`json5模式解析 ${o}`), v.parse(m(o, "utf8"))) : (c.info(`json模式解析 ${o}`), JSON.parse(m(o, "utf8")));
219
+ }, sr = async () => {
220
+ const { useDefaultConfig: r } = await x({
212
221
  name: "useDefaultConfig",
213
222
  type: "confirm",
214
223
  message: "使用默认模板配置",
215
224
  initial: !0
216
225
  });
217
- return e;
226
+ return r;
218
227
  };
219
228
  export {
220
229
  _ as EditorTypeEnum,
221
- ye as _curry,
222
- pe as _get,
223
- ge as _set,
224
- ce as chalk,
225
- Z as createMainCommand,
226
- ee as createSubcommand,
227
- X as decryptAES,
228
- Q as encryptAES,
229
- re as getConfigFileCommonOptions,
230
+ hr as _curry,
231
+ dr as _get,
232
+ mr as _set,
233
+ ar as chalk,
234
+ rr as createMainCommand,
235
+ er as createSubcommand,
236
+ Z as decryptAES,
237
+ X as encryptAES,
238
+ tr as getConfigFileCommonOptions,
230
239
  A as getEditorType,
231
- ne as getUseDefaultConfig,
232
- L as initConfigFile,
233
- te as initHandlerCommon,
234
- ue as json5,
235
- i as log,
236
- G as lookForParentTarget,
240
+ L as getRootDirOptions,
241
+ sr as getUseDefaultConfig,
242
+ I as initConfigFile,
243
+ or as initHandlerCommon,
244
+ lr as json5,
245
+ c as log,
246
+ Q as lookForParentTarget,
237
247
  V as openFileInEditor,
238
- oe as readConfigFile,
248
+ nr as readConfigFile,
239
249
  x as xPrompts
240
250
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@done-coding/cli-utils",
3
- "version": "0.3.1-alpha.0",
3
+ "version": "0.3.2",
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": "ec6957606bbae68d7268159b38b9d36dab4a2731"
64
+ "gitHead": "7a8d554748b844c8b7694c587983b21af902d5d1"
65
65
  }
@@ -14,9 +14,14 @@ export interface ConfigFileCommonOptions {
14
14
  export type InitConfigFileOptions = ConfigFileCommonOptions;
15
15
  /** 读取配置文件选项 */
16
16
  export type ReadConfigFileOptions = ConfigFileCommonOptions;
17
+ /** 获取 rootDir 选项 */
18
+ export declare const getRootDirOptions: (rootDirDefault?: string) => {
19
+ rootDir: YargsOptions;
20
+ };
17
21
  /** 获取配置文件通用选项 */
18
- export declare const getConfigFileCommonOptions: ({ configPathDefault, }: {
22
+ export declare const getConfigFileCommonOptions: ({ configPathDefault, rootDirDefault, }: {
19
23
  configPathDefault: string;
24
+ rootDirDefault?: string | undefined;
20
25
  }) => Record<keyof ConfigFileCommonOptions, YargsOptions>;
21
26
  /** 初始化配置文件 */
22
27
  export declare const initConfigFile: <T>(content: T, argv: CliHandlerArgv<InitConfigFileOptions>) => Promise<string>;
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * 查找目标文件或目录
3
- * @param target 目标文件或目录
4
- * @param currentDir 当前目录
5
- * @returns 目标文件或目录路径
6
3
  */
7
- export declare const lookForParentTarget: (target: string, currentDir?: string) => string | undefined;
4
+ export declare const lookForParentTarget: (target: string, { currentDir, isFindFarthest, }?: {
5
+ currentDir?: string | undefined;
6
+ isFindFarthest?: boolean | undefined;
7
+ }) => string | undefined;