@done-coding/cli-utils 0.3.0-alpha.0 → 0.3.2-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,37 +1,49 @@
1
1
  #!/usr/bin/env node
2
- import b from "chalk";
3
- import { default as Y } from "chalk";
4
- import p from "node:path";
5
- import E 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
- import k from "yargs";
9
- import { hideBin as $ } from "yargs/helpers";
10
- const i = Object.assign(
11
- (r, ...t) => console.log(...t.map((e) => b[r](e))),
8
+ import E from "yargs";
9
+ import { hideBin as F } from "yargs/helpers";
10
+ import { execSync as y } from "node:child_process";
11
+ import v from "json5";
12
+ export * from "json5";
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))),
12
19
  {
13
20
  /** 成功 */
14
- success: (...r) => i("green", ...r),
21
+ success: (...r) => c("green", ...r),
15
22
  /** /步骤 */
16
- stage: (...r) => i("blue", ...r),
23
+ stage: (...r) => c("blue", ...r),
17
24
  /** 提示信息 */
18
- info: (...r) => i("cyan", ...r),
25
+ info: (...r) => c("cyan", ...r),
19
26
  /** 警告 */
20
- warn: (...r) => i("yellow", ...r),
27
+ warn: (...r) => c("yellow", ...r),
21
28
  /** 错误 */
22
- error: (...r) => i("red", ...r),
29
+ error: (...r) => c("red", ...r),
23
30
  /** 跳过 */
24
- skip: (...r) => i("gray", ...r)
31
+ skip: (...r) => c("gray", ...r)
25
32
  }
26
- ), P = (r, t = process.cwd()) => {
27
- const e = p.resolve(t).split(p.sep).map((o, c, s) => c ? p.join(s.slice(0, c).join(p.sep), o) : o);
28
- for (; e.length; ) {
29
- const o = e.pop(), c = p.join(o, r);
30
- if (E.existsSync(c))
31
- return o;
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;
32
44
  }
33
- }, g = "aes-256-cbc", m = 16, u = "hex", d = ":";
34
- function y(r) {
45
+ }, C = "aes-256-cbc", S = 16, p = "hex", d = ":";
46
+ function $(r) {
35
47
  return l.pbkdf2Sync(
36
48
  r,
37
49
  "done-coding-cli-salt",
@@ -43,88 +55,196 @@ function y(r) {
43
55
  "sha256"
44
56
  );
45
57
  }
46
- function D({
58
+ function X({
47
59
  text: r,
48
- secretKey: t
60
+ secretKey: e
49
61
  }) {
50
62
  try {
51
- const e = y(t), o = l.randomBytes(m), c = l.createCipheriv(g, e, o);
52
- let s = c.update(r);
53
- s = Buffer.concat([s, c.final()]);
54
- const f = o.toString(u), n = s.toString(u);
55
- return `${f}${d}${n}`;
56
- } catch (e) {
57
- return i.error(
58
- `加密失败: ${e instanceof Error ? e.message : String(e)}`
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)}`
59
71
  ), "";
60
72
  }
61
73
  }
62
- function F({
74
+ function Z({
63
75
  encryptedText: r,
64
- secretKey: t
76
+ secretKey: e
65
77
  }) {
66
78
  try {
67
79
  if (!r.includes(d))
68
80
  return "";
69
- const e = y(t), [o, c] = r.split(d);
70
- if (o.length !== m * 2)
81
+ const t = $(e), [o, n] = r.split(d);
82
+ if (o.length !== S * 2)
71
83
  return "";
72
- const s = Buffer.from(o, u), f = Buffer.from(c, u), n = l.createDecipheriv(g, e, s);
73
- let a = n.update(f);
74
- return a = Buffer.concat([a, n.final()]), a.toString();
75
- } catch (e) {
76
- return i.error(
77
- `解密失败: ${e instanceof Error ? e.message : String(e)}`
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)}`
78
90
  ), "";
79
91
  }
80
92
  }
81
- const I = (...r) => {
82
- const [t, e = {}] = r;
83
- return H(t, {
93
+ const x = (...r) => {
94
+ const [e, t = {}] = r;
95
+ return H(e, {
84
96
  onCancel(o) {
85
- return i.error(`退出${o == null ? void 0 : o.name}输入`), process.exit(1);
97
+ return c.error(`退出${o == null ? void 0 : o.name}输入`), process.exit(1);
86
98
  },
87
- ...e
99
+ ...t
88
100
  });
89
- }, A = (r, t) => {
90
- r ? i.error(r) : i.error(t.message), t != null && t.stack && i.error(t.stack), process.exit(1);
91
- }, B = () => {
92
- const r = $(process.argv);
93
- return k(r);
94
- }, h = (r, {
95
- usage: t,
96
- version: e,
101
+ }, R = (r, e) => {
102
+ r ? c.error(r) : c.error(e.message), e != null && e.stack && c.error(e.stack), process.exit(1);
103
+ }, T = () => {
104
+ const r = F(process.argv);
105
+ return E(r);
106
+ }, j = (r, {
107
+ usage: e,
108
+ version: t,
97
109
  demandCommandCount: o,
98
- options: c,
99
- positionals: s,
100
- subcommands: f
110
+ options: n,
111
+ positionals: i,
112
+ subcommands: a
101
113
  }) => {
102
- let n = r.strict();
103
- t && (n = n.usage(`Usage: ${t}`)), o && (n = n.demandCommand(o));
104
- const a = "help";
105
- return n = n.help(a), e ? n = n.version(e).alias("h", a).alias("v", "version") : n = n.alias("h", a), c && (n = n.options(c)), s && (n = Object.entries(s).reduce((v, [S, x]) => v.positional(S, x), n)), f && (n = n.command(f)), n;
106
- }, K = async ({ handler: r, ...t }) => {
107
- const e = await h(B(), t).fail(A).argv;
108
- return r ? r(e) : e;
109
- }, R = (r) => {
110
- const { command: t, describe: e, handler: o = () => {
111
- }, ...c } = r;
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;
112
124
  return {
113
- command: t,
114
- describe: e,
115
- builder(s) {
116
- return h(s, c);
125
+ command: e,
126
+ describe: t,
127
+ builder(i) {
128
+ return j(i, n);
117
129
  },
118
130
  handler: o
119
131
  };
120
132
  };
133
+ var _ = /* @__PURE__ */ ((r) => (r.VSCODE = "VsCode", r.CURSOR = "Cursor", r.OTHER = "其他", r))(_ || {});
134
+ const A = async () => {
135
+ const { editorType: r } = await x([
136
+ {
137
+ name: "editorType",
138
+ type: "select",
139
+ message: "编辑器类型",
140
+ choices: [
141
+ "Cursor",
142
+ "VsCode",
143
+ "其他"
144
+ /* OTHER */
145
+ ].map((e) => ({
146
+ title: e,
147
+ value: e
148
+ }))
149
+ }
150
+ ]);
151
+ return r;
152
+ }, B = {
153
+ Cursor: "cursor",
154
+ VsCode: "code"
155
+ }, N = (r, e, t) => {
156
+ try {
157
+ y(`${r} -v`, { stdio: "ignore" }), y(`${r} ${e}`);
158
+ } catch {
159
+ t();
160
+ }
161
+ }, V = (r, e) => {
162
+ const t = (o) => c.info(`
163
+ ${o}, 请用编辑器打开 ${r} 进行编辑
164
+ `);
165
+ switch (e) {
166
+ case "Cursor":
167
+ case "VsCode": {
168
+ const o = B[e];
169
+ N(o, r, () => {
170
+ t(`${o}命令未安装`);
171
+ });
172
+ break;
173
+ }
174
+ default:
175
+ t("其他编辑器");
176
+ }
177
+ }, L = (r = process.cwd()) => ({
178
+ /** 必须保留 */
179
+ rootDir: {
180
+ type: "string",
181
+ alias: "r",
182
+ describe: "运行目录",
183
+ /** 必须设置默认值 */
184
+ default: r
185
+ }
186
+ }), tr = ({
187
+ configPathDefault: r,
188
+ rootDirDefault: e
189
+ }) => ({
190
+ /** 必须保留 */
191
+ ...L(e),
192
+ /** 必须保留 */
193
+ configPath: {
194
+ type: "string",
195
+ alias: "c",
196
+ describe: "配置文件相对路径",
197
+ /** 必须设置默认值 */
198
+ default: r
199
+ }
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, {
203
+ recursive: !0
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
207
+ } = {}) => {
208
+ const o = await I(r, e);
209
+ t == null || t(o);
210
+ const n = await A();
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}`);
216
+ return;
217
+ }
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({
221
+ name: "useDefaultConfig",
222
+ type: "confirm",
223
+ message: "使用默认模板配置",
224
+ initial: !0
225
+ });
226
+ return r;
227
+ };
121
228
  export {
122
- Y as chalk,
123
- K as createMainCommand,
124
- R as createSubcommand,
125
- F as decryptAES,
126
- D as encryptAES,
127
- i as log,
128
- P as lookForParentTarget,
129
- I as xPrompts
229
+ _ as EditorTypeEnum,
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,
239
+ A as getEditorType,
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,
247
+ V as openFileInEditor,
248
+ nr as readConfigFile,
249
+ x as xPrompts
130
250
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@done-coding/cli-utils",
3
- "version": "0.3.0-alpha.0",
3
+ "version": "0.3.2-alpha.0",
4
4
  "description": "cli utils",
5
5
  "private": false,
6
6
  "module": "es/index.mjs",
@@ -14,8 +14,7 @@
14
14
  "files": [
15
15
  "es",
16
16
  "lib",
17
- "types",
18
- "gif"
17
+ "types"
19
18
  ],
20
19
  "scripts": {
21
20
  "clean": "rimraf es lib types",
@@ -38,6 +37,10 @@
38
37
  "license": "MIT",
39
38
  "sideEffects": false,
40
39
  "devDependencies": {
40
+ "@types/json5": "^2.2.0",
41
+ "@types/lodash.curry": "^4.1.8",
42
+ "@types/lodash.get": "^4.4.9",
43
+ "@types/lodash.set": "^4.3.9",
41
44
  "@types/node": "^18.0.0",
42
45
  "@types/prompts": "^2.4.6",
43
46
  "@types/yargs": "^17.0.28",
@@ -51,8 +54,12 @@
51
54
  },
52
55
  "dependencies": {
53
56
  "chalk": "^5.3.0",
57
+ "json5": "^2.2.3",
58
+ "lodash.curry": "^4.1.1",
59
+ "lodash.get": "^4.4.2",
60
+ "lodash.set": "^4.3.2",
54
61
  "prompts": "^2.4.2",
55
62
  "yargs": "^17.7.2"
56
63
  },
57
- "gitHead": "c5a54bdf08759425701a2024442a923883065189"
64
+ "gitHead": "7faf662e137d46f776c8e5abdf75ab3db541362c"
58
65
  }
@@ -0,0 +1,36 @@
1
+ import type { CliHandlerArgv, YargsOptions } from "./cli";
2
+ /** 配置文件通用选项 */
3
+ export interface ConfigFileCommonOptions {
4
+ /**
5
+ * 相对于路径
6
+ * ---
7
+ * 绝对路径 path.resolve(rootDir, configPath)
8
+ */
9
+ configPath: string;
10
+ /** 项目根目录 */
11
+ rootDir: string;
12
+ }
13
+ /** 初始化文件选项 */
14
+ export type InitConfigFileOptions = ConfigFileCommonOptions;
15
+ /** 读取配置文件选项 */
16
+ export type ReadConfigFileOptions = ConfigFileCommonOptions;
17
+ /** 获取 rootDir 选项 */
18
+ export declare const getRootDirOptions: (rootDirDefault?: string) => {
19
+ rootDir: YargsOptions;
20
+ };
21
+ /** 获取配置文件通用选项 */
22
+ export declare const getConfigFileCommonOptions: ({ configPathDefault, rootDirDefault, }: {
23
+ configPathDefault: string;
24
+ rootDirDefault?: string | undefined;
25
+ }) => Record<keyof ConfigFileCommonOptions, YargsOptions>;
26
+ /** 初始化配置文件 */
27
+ export declare const initConfigFile: <T>(content: T, argv: CliHandlerArgv<InitConfigFileOptions>) => Promise<string>;
28
+ /** 初始化配置文件通用处理器 */
29
+ export declare const initHandlerCommon: <T>(content: T, argv: CliHandlerArgv<InitConfigFileOptions>, { onFileGenerated, }?: {
30
+ /** 文件已生成 */
31
+ onFileGenerated?: ((path: string) => void) | undefined;
32
+ }) => Promise<void>;
33
+ /** 读取配置文件 */
34
+ export declare const readConfigFile: <T>(argv: CliHandlerArgv<ReadConfigFileOptions>) => Promise<T | undefined>;
35
+ /** 获取是否使用默认配置 */
36
+ export declare const getUseDefaultConfig: () => Promise<any>;
@@ -0,0 +1,13 @@
1
+ /** 编辑器类型枚举 */
2
+ export declare enum EditorTypeEnum {
3
+ /** vscode */
4
+ VSCODE = "VsCode",
5
+ /** cursor */
6
+ CURSOR = "Cursor",
7
+ /** 其他编辑器 */
8
+ OTHER = "\u5176\u4ED6"
9
+ }
10
+ /** 获取编辑器类型 */
11
+ export declare const getEditorType: () => Promise<any>;
12
+ /** 用编辑器打开文件 */
13
+ export declare const openFileInEditor: (path: string, editorType: EditorTypeEnum) => void;
package/types/index.d.ts CHANGED
@@ -3,3 +3,7 @@ export * from "./look-for";
3
3
  export * from "./aes";
4
4
  export * from "./prompts";
5
5
  export * from "./cli";
6
+ export * from "./editor";
7
+ export * from "./config-file";
8
+ export * from "./json5";
9
+ export * from "./lodash";
@@ -0,0 +1,3 @@
1
+ import json5 from "json5";
2
+ export { json5 };
3
+ export * from "json5";
@@ -0,0 +1,4 @@
1
+ import _get from "lodash.get";
2
+ import _set from "lodash.set";
3
+ import _curry from "lodash.curry";
4
+ export { _get, _set, _curry };
@@ -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;