@done-coding/cli-utils 0.3.4 → 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 +121 -110
- package/package.json +2 -2
- package/types/config-file.d.ts +1 -1
- package/types/index.d.ts +1 -0
- package/types/package-json.d.ts +7 -0
package/es/index.mjs
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import
|
|
3
|
-
import { default as
|
|
4
|
-
import
|
|
5
|
-
import
|
|
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
|
|
8
|
-
import
|
|
9
|
-
import { hideBin as
|
|
10
|
-
import { execSync as
|
|
11
|
-
import
|
|
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
|
|
14
|
-
import { default as
|
|
15
|
-
import { default as
|
|
16
|
-
import { default as
|
|
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) =>
|
|
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
|
-
),
|
|
33
|
+
), X = (e, {
|
|
34
34
|
/** 当前目录 */
|
|
35
35
|
currentDir: r = process.cwd(),
|
|
36
36
|
/** 优先找最远的父目录 */
|
|
37
37
|
isFindFarthest: t = !0
|
|
38
38
|
} = {}) => {
|
|
39
|
-
const
|
|
40
|
-
for (;
|
|
41
|
-
const
|
|
42
|
-
if (
|
|
43
|
-
return
|
|
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
|
-
},
|
|
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
|
|
58
|
+
function Z({
|
|
59
59
|
text: e,
|
|
60
60
|
secretKey: r
|
|
61
61
|
}) {
|
|
62
62
|
try {
|
|
63
|
-
const t = $(r),
|
|
64
|
-
let
|
|
65
|
-
|
|
66
|
-
const
|
|
67
|
-
return `${
|
|
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
|
|
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), [
|
|
82
|
-
if (
|
|
81
|
+
const t = $(r), [n, o] = e.split(d);
|
|
82
|
+
if (n.length !== S * 2)
|
|
83
83
|
return "";
|
|
84
|
-
const
|
|
85
|
-
let u =
|
|
86
|
-
return u = Buffer.concat([u,
|
|
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
|
|
93
|
+
const k = (...e) => {
|
|
94
94
|
const [r, t = {}] = e;
|
|
95
|
-
return
|
|
96
|
-
onCancel(
|
|
97
|
-
return c.error(`退出${
|
|
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
|
-
},
|
|
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
|
-
},
|
|
104
|
-
const e =
|
|
105
|
-
return
|
|
106
|
-
},
|
|
103
|
+
}, _ = () => {
|
|
104
|
+
const e = A(process.argv);
|
|
105
|
+
return H(e);
|
|
106
|
+
}, w = (e, {
|
|
107
107
|
usage: r,
|
|
108
108
|
version: t,
|
|
109
|
-
demandCommandCount:
|
|
110
|
-
options:
|
|
111
|
-
positionals:
|
|
112
|
-
subcommands:
|
|
109
|
+
demandCommandCount: n,
|
|
110
|
+
options: o,
|
|
111
|
+
positionals: s,
|
|
112
|
+
subcommands: f
|
|
113
113
|
}) => {
|
|
114
|
-
let
|
|
115
|
-
r && (
|
|
114
|
+
let i = e.strict();
|
|
115
|
+
r && (i = i.usage(`Usage: ${r}`)), n && (i = i.demandCommand(n));
|
|
116
116
|
const u = "help";
|
|
117
|
-
return
|
|
118
|
-
},
|
|
119
|
-
const t = await
|
|
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
|
-
},
|
|
122
|
-
const { command: r, describe: t, handler:
|
|
123
|
-
}, ...
|
|
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(
|
|
128
|
-
return
|
|
127
|
+
builder(s) {
|
|
128
|
+
return w(s, o);
|
|
129
129
|
},
|
|
130
|
-
handler:
|
|
130
|
+
handler: n
|
|
131
131
|
};
|
|
132
132
|
};
|
|
133
|
-
var
|
|
134
|
-
const
|
|
135
|
-
const { editorType: e } = await
|
|
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
|
-
},
|
|
152
|
+
}, T = {
|
|
153
153
|
Cursor: "cursor",
|
|
154
154
|
VsCode: "code"
|
|
155
|
-
},
|
|
155
|
+
}, B = (e, r, t) => {
|
|
156
156
|
try {
|
|
157
|
-
|
|
157
|
+
h(`${e} -v`, { stdio: "ignore" }), h(`${e} ${r}`);
|
|
158
158
|
} catch {
|
|
159
159
|
t();
|
|
160
160
|
}
|
|
161
|
-
},
|
|
162
|
-
const t = (
|
|
163
|
-
${
|
|
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
|
|
169
|
-
|
|
170
|
-
t(`${
|
|
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
|
-
},
|
|
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
|
-
}),
|
|
186
|
+
}), oe = ({
|
|
187
187
|
configPathDefault: e,
|
|
188
188
|
rootDirDefault: r
|
|
189
189
|
}) => ({
|
|
190
190
|
/** 必须保留 */
|
|
191
|
-
...
|
|
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
|
-
}),
|
|
201
|
-
const { configPath: t, rootDir:
|
|
202
|
-
return
|
|
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
|
-
}),
|
|
205
|
-
},
|
|
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:
|
|
207
|
+
edit: n = !1
|
|
208
208
|
} = {}) => {
|
|
209
|
-
const
|
|
210
|
-
if (t == null || t(
|
|
211
|
-
const
|
|
212
|
-
|
|
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
|
-
},
|
|
215
|
-
const { configPath:
|
|
216
|
-
if (!
|
|
217
|
-
|
|
218
|
-
|
|
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}`),
|
|
221
|
-
},
|
|
222
|
-
const { useDefaultConfig: e } = await
|
|
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
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
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
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
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
|
+
"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": "
|
|
64
|
+
"gitHead": "360b14a641064f732b01ffd6339c2f1963945b61"
|
|
65
65
|
}
|
package/types/config-file.d.ts
CHANGED
|
@@ -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>(
|
|
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