@done-coding/cli-utils 0.3.1-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 +144 -134
- package/package.json +2 -2
- package/types/config-file.d.ts +6 -1
- package/types/look-for.d.ts +4 -4
package/es/index.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import
|
|
3
|
-
import { default as
|
|
4
|
-
import
|
|
5
|
-
import
|
|
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
|
|
14
|
-
import { default as
|
|
15
|
-
import { default as
|
|
16
|
-
import { default as
|
|
17
|
-
const
|
|
18
|
-
(
|
|
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: (...
|
|
21
|
+
success: (...r) => c("green", ...r),
|
|
22
22
|
/** /步骤 */
|
|
23
|
-
stage: (...
|
|
23
|
+
stage: (...r) => c("blue", ...r),
|
|
24
24
|
/** 提示信息 */
|
|
25
|
-
info: (...
|
|
25
|
+
info: (...r) => c("cyan", ...r),
|
|
26
26
|
/** 警告 */
|
|
27
|
-
warn: (...
|
|
27
|
+
warn: (...r) => c("yellow", ...r),
|
|
28
28
|
/** 错误 */
|
|
29
|
-
error: (...
|
|
29
|
+
error: (...r) => c("red", ...r),
|
|
30
30
|
/** 跳过 */
|
|
31
|
-
skip: (...
|
|
31
|
+
skip: (...r) => c("gray", ...r)
|
|
32
32
|
}
|
|
33
|
-
),
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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 $(
|
|
46
|
+
function $(r) {
|
|
42
47
|
return l.pbkdf2Sync(
|
|
43
|
-
|
|
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
|
|
54
|
-
text:
|
|
55
|
-
secretKey:
|
|
58
|
+
function X({
|
|
59
|
+
text: r,
|
|
60
|
+
secretKey: e
|
|
56
61
|
}) {
|
|
57
62
|
try {
|
|
58
|
-
const
|
|
59
|
-
let
|
|
60
|
-
|
|
61
|
-
const
|
|
62
|
-
return `${
|
|
63
|
-
} catch (
|
|
64
|
-
return
|
|
65
|
-
`加密失败: ${
|
|
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
|
|
70
|
-
encryptedText:
|
|
71
|
-
secretKey:
|
|
74
|
+
function Z({
|
|
75
|
+
encryptedText: r,
|
|
76
|
+
secretKey: e
|
|
72
77
|
}) {
|
|
73
78
|
try {
|
|
74
|
-
if (!
|
|
79
|
+
if (!r.includes(d))
|
|
75
80
|
return "";
|
|
76
|
-
const
|
|
77
|
-
if (
|
|
81
|
+
const t = $(e), [o, n] = r.split(d);
|
|
82
|
+
if (o.length !== S * 2)
|
|
78
83
|
return "";
|
|
79
|
-
const
|
|
80
|
-
let
|
|
81
|
-
return
|
|
82
|
-
} catch (
|
|
83
|
-
return
|
|
84
|
-
`解密失败: ${
|
|
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 = (...
|
|
89
|
-
const [
|
|
90
|
-
return H(
|
|
91
|
-
onCancel(
|
|
92
|
-
return
|
|
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
|
-
...
|
|
99
|
+
...t
|
|
95
100
|
});
|
|
96
|
-
}, R = (
|
|
97
|
-
|
|
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
|
|
100
|
-
return E(
|
|
101
|
-
}, j = (
|
|
102
|
-
usage:
|
|
103
|
-
version:
|
|
104
|
-
demandCommandCount:
|
|
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:
|
|
107
|
-
subcommands:
|
|
111
|
+
positionals: i,
|
|
112
|
+
subcommands: a
|
|
108
113
|
}) => {
|
|
109
|
-
let s =
|
|
110
|
-
|
|
111
|
-
const
|
|
112
|
-
return s = s.help(
|
|
113
|
-
},
|
|
114
|
-
const
|
|
115
|
-
return
|
|
116
|
-
},
|
|
117
|
-
const { command:
|
|
118
|
-
}, ...n } =
|
|
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:
|
|
121
|
-
describe:
|
|
122
|
-
builder(
|
|
123
|
-
return j(
|
|
125
|
+
command: e,
|
|
126
|
+
describe: t,
|
|
127
|
+
builder(i) {
|
|
128
|
+
return j(i, n);
|
|
124
129
|
},
|
|
125
|
-
handler:
|
|
130
|
+
handler: o
|
|
126
131
|
};
|
|
127
132
|
};
|
|
128
|
-
var _ = /* @__PURE__ */ ((
|
|
133
|
+
var _ = /* @__PURE__ */ ((r) => (r.VSCODE = "VsCode", r.CURSOR = "Cursor", r.OTHER = "其他", r))(_ || {});
|
|
129
134
|
const A = async () => {
|
|
130
|
-
const { editorType:
|
|
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((
|
|
141
|
-
title:
|
|
142
|
-
value:
|
|
145
|
+
].map((e) => ({
|
|
146
|
+
title: e,
|
|
147
|
+
value: e
|
|
143
148
|
}))
|
|
144
149
|
}
|
|
145
150
|
]);
|
|
146
|
-
return
|
|
151
|
+
return r;
|
|
147
152
|
}, B = {
|
|
148
153
|
Cursor: "cursor",
|
|
149
154
|
VsCode: "code"
|
|
150
|
-
}, N = (
|
|
155
|
+
}, N = (r, e, t) => {
|
|
151
156
|
try {
|
|
152
|
-
y(`${
|
|
157
|
+
y(`${r} -v`, { stdio: "ignore" }), y(`${r} ${e}`);
|
|
153
158
|
} catch {
|
|
154
|
-
|
|
159
|
+
t();
|
|
155
160
|
}
|
|
156
|
-
}, V = (
|
|
157
|
-
const
|
|
158
|
-
${
|
|
161
|
+
}, V = (r, e) => {
|
|
162
|
+
const t = (o) => c.info(`
|
|
163
|
+
${o}, 请用编辑器打开 ${r} 进行编辑
|
|
159
164
|
`);
|
|
160
|
-
switch (
|
|
165
|
+
switch (e) {
|
|
161
166
|
case "Cursor":
|
|
162
167
|
case "VsCode": {
|
|
163
|
-
const
|
|
164
|
-
N(
|
|
165
|
-
|
|
168
|
+
const o = B[e];
|
|
169
|
+
N(o, r, () => {
|
|
170
|
+
t(`${o}命令未安装`);
|
|
166
171
|
});
|
|
167
172
|
break;
|
|
168
173
|
}
|
|
169
174
|
default:
|
|
170
|
-
|
|
175
|
+
t("其他编辑器");
|
|
171
176
|
}
|
|
172
|
-
},
|
|
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:
|
|
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:
|
|
198
|
+
default: r
|
|
190
199
|
}
|
|
191
|
-
}),
|
|
192
|
-
const { configPath:
|
|
193
|
-
return h(
|
|
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") ? (
|
|
196
|
-
},
|
|
197
|
-
onFileGenerated:
|
|
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
|
|
200
|
-
|
|
208
|
+
const o = await I(r, e);
|
|
209
|
+
t == null || t(o);
|
|
201
210
|
const n = await A();
|
|
202
|
-
V(
|
|
203
|
-
},
|
|
204
|
-
const { configPath:
|
|
205
|
-
if (!h(
|
|
206
|
-
|
|
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
|
|
210
|
-
},
|
|
211
|
-
const { useDefaultConfig:
|
|
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
|
|
226
|
+
return r;
|
|
218
227
|
};
|
|
219
228
|
export {
|
|
220
229
|
_ as EditorTypeEnum,
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
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
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
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
|
-
|
|
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.
|
|
3
|
+
"version": "0.3.2-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": "
|
|
64
|
+
"gitHead": "7faf662e137d46f776c8e5abdf75ab3db541362c"
|
|
65
65
|
}
|
package/types/config-file.d.ts
CHANGED
|
@@ -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>;
|
package/types/look-for.d.ts
CHANGED
|
@@ -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
|
|
4
|
+
export declare const lookForParentTarget: (target: string, { currentDir, isFindFarthest, }?: {
|
|
5
|
+
currentDir?: string | undefined;
|
|
6
|
+
isFindFarthest?: boolean | undefined;
|
|
7
|
+
}) => string | undefined;
|