@done-coding/cli-template 0.7.1-alpha.0 → 0.7.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/cli.mjs +2 -4
- package/es/index.mjs +22 -15
- package/es/main-935d4280.js +488 -0
- package/package.json +5 -12
- package/types/index.d.ts +1 -1
- package/types/injectInfo.json.d.ts +1 -1
- package/types/utils/compile.d.ts +5 -3
- package/types/utils/config.d.ts +2 -1
- package/types/utils/dealOptions.d.ts +16 -5
- package/types/utils/index.d.ts +1 -1
- package/types/utils/init.d.ts +2 -1
- package/types/utils/path.d.ts +4 -0
- package/types/utils/types.d.ts +17 -18
- package/es/index-34359db9.js +0 -521
- package/types/utils/const.d.ts +0 -2
- /package/types/json/{template-default.json.d.ts → default.json.d.ts} +0 -0
package/es/cli.mjs
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
2
|
+
import { d as m } from "./main-935d4280.js";
|
|
3
|
+
import "@done-coding/cli-utils";
|
|
3
4
|
import "node:path";
|
|
4
5
|
import "node:fs";
|
|
5
|
-
import "@done-coding/cli-utils";
|
|
6
|
-
import "node:child_process";
|
|
7
6
|
import "lodash.template";
|
|
8
7
|
import "lodash.assign";
|
|
9
|
-
import "lodash.curry";
|
|
10
8
|
m();
|
package/es/index.mjs
CHANGED
|
@@ -1,21 +1,28 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
3
|
-
import "
|
|
4
|
-
import "node:
|
|
2
|
+
import { M as r } from "./main-935d4280.js";
|
|
3
|
+
import { O as E, S as h, T as b, b as T, c as A, a as H, h as O, i as S } from "./main-935d4280.js";
|
|
4
|
+
import t from "node:path";
|
|
5
|
+
import { existsSync as n } from "node:fs";
|
|
5
6
|
import "@done-coding/cli-utils";
|
|
6
|
-
import "node:child_process";
|
|
7
7
|
import "lodash.template";
|
|
8
8
|
import "lodash.assign";
|
|
9
|
-
|
|
9
|
+
const u = ({
|
|
10
|
+
rootDir: e,
|
|
11
|
+
configPath: o = r
|
|
12
|
+
}) => {
|
|
13
|
+
const a = t.resolve(e, o);
|
|
14
|
+
if (n(a))
|
|
15
|
+
return a;
|
|
16
|
+
};
|
|
10
17
|
export {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
r as MODULE_DEFAULT_CONFIG_RELATIVE_PATH,
|
|
19
|
+
E as OutputModeEnum,
|
|
20
|
+
h as SubcommandEnum,
|
|
21
|
+
b as TemplateBindKeyEnum,
|
|
22
|
+
T as batchCompileHandler,
|
|
23
|
+
A as compileHandler,
|
|
24
|
+
H as crateAsSubcommand,
|
|
25
|
+
u as getConfigPath,
|
|
26
|
+
O as handler,
|
|
27
|
+
S as initHandler
|
|
21
28
|
};
|
|
@@ -0,0 +1,488 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { getConfigFileCommonOptions as N, getUseDefaultConfig as V, initHandlerCommon as W, log as a, xPrompts as R, readConfigFile as G, _curry as T, createSubcommand as A, createMainCommand as Y } from "@done-coding/cli-utils";
|
|
3
|
+
import k from "node:path";
|
|
4
|
+
import f from "node:fs";
|
|
5
|
+
import O from "lodash.template";
|
|
6
|
+
import B from "lodash.assign";
|
|
7
|
+
var h = /* @__PURE__ */ ((e) => (e.INIT = "init", e.COMPILE = "compile", e))(h || {}), J = /* @__PURE__ */ ((e) => (e.REPOSITORY_URL = "REPOSITORY_URL", e))(J || {}), b = /* @__PURE__ */ ((e) => (e.OVERWRITE = "overwrite", e.APPEND = "append", e.REPLACE = "replace", e.RETURN = "return", e))(b || {});
|
|
8
|
+
const _ = {
|
|
9
|
+
name: "@done-coding/cli-template",
|
|
10
|
+
version: "0.7.2-alpha.0",
|
|
11
|
+
description: "预编译命令行工具",
|
|
12
|
+
cliConfig: {
|
|
13
|
+
namespaceDir: ".done-coding",
|
|
14
|
+
moduleName: "template"
|
|
15
|
+
}
|
|
16
|
+
}, {
|
|
17
|
+
cliConfig: { namespaceDir: K, moduleName: q }
|
|
18
|
+
} = _, z = `./${K}/${q}`, I = `${z}.json`, v = {
|
|
19
|
+
rollback: !1,
|
|
20
|
+
dealMarkdown: !1,
|
|
21
|
+
mode: b.OVERWRITE,
|
|
22
|
+
batch: !1,
|
|
23
|
+
rollbackDelNullFile: !1
|
|
24
|
+
}, S = (e) => {
|
|
25
|
+
const {
|
|
26
|
+
rollback: t,
|
|
27
|
+
dealMarkdown: s,
|
|
28
|
+
mode: c,
|
|
29
|
+
batch: l,
|
|
30
|
+
rootDir: o,
|
|
31
|
+
configPath: i,
|
|
32
|
+
rollbackDelNullFile: r,
|
|
33
|
+
...u
|
|
34
|
+
} = e, { rootDir: g, configPath: D } = N({
|
|
35
|
+
configPathDefault: I
|
|
36
|
+
});
|
|
37
|
+
return {
|
|
38
|
+
rollback: t ?? v.rollback,
|
|
39
|
+
rollbackDelNullFile: r ?? v.rollbackDelNullFile,
|
|
40
|
+
dealMarkdown: s ?? v.dealMarkdown,
|
|
41
|
+
mode: c ?? v.mode,
|
|
42
|
+
batch: l ?? v.batch,
|
|
43
|
+
rootDir: o ?? g.default,
|
|
44
|
+
configPath: i ?? D.default,
|
|
45
|
+
...u
|
|
46
|
+
};
|
|
47
|
+
}, Z = () => ({
|
|
48
|
+
env: {
|
|
49
|
+
alias: "e",
|
|
50
|
+
describe: "环境数据文件JSON文件相对路径(优先级高于envData)",
|
|
51
|
+
type: "string"
|
|
52
|
+
},
|
|
53
|
+
envData: {
|
|
54
|
+
alias: "E",
|
|
55
|
+
describe: "环境变量数据(JSON字符串)",
|
|
56
|
+
type: "string"
|
|
57
|
+
},
|
|
58
|
+
input: {
|
|
59
|
+
alias: "i",
|
|
60
|
+
describe: "模板文件相对路径(优先级高于inputTemplate)",
|
|
61
|
+
type: "string"
|
|
62
|
+
},
|
|
63
|
+
inputData: {
|
|
64
|
+
alias: "I",
|
|
65
|
+
describe: "模板数据",
|
|
66
|
+
type: "string"
|
|
67
|
+
},
|
|
68
|
+
mode: {
|
|
69
|
+
alias: "m",
|
|
70
|
+
describe: "输出模式",
|
|
71
|
+
type: "string",
|
|
72
|
+
choices: [
|
|
73
|
+
b.OVERWRITE,
|
|
74
|
+
b.APPEND,
|
|
75
|
+
b.REPLACE,
|
|
76
|
+
b.RETURN
|
|
77
|
+
],
|
|
78
|
+
default: v.mode
|
|
79
|
+
},
|
|
80
|
+
output: {
|
|
81
|
+
alias: "o",
|
|
82
|
+
describe: "输出文件路径",
|
|
83
|
+
type: "string"
|
|
84
|
+
},
|
|
85
|
+
rollback: {
|
|
86
|
+
alias: "r",
|
|
87
|
+
describe: "是否回滚",
|
|
88
|
+
type: "boolean",
|
|
89
|
+
default: v.rollback
|
|
90
|
+
},
|
|
91
|
+
dealMarkdown: {
|
|
92
|
+
alias: "d",
|
|
93
|
+
describe: "(检测是markdown)是否处理(单个)代码块包裹",
|
|
94
|
+
type: "boolean",
|
|
95
|
+
default: v.dealMarkdown
|
|
96
|
+
},
|
|
97
|
+
batch: {
|
|
98
|
+
alias: "b",
|
|
99
|
+
describe: "是否批量处理",
|
|
100
|
+
type: "boolean",
|
|
101
|
+
default: v.batch
|
|
102
|
+
},
|
|
103
|
+
...N({
|
|
104
|
+
configPathDefault: I
|
|
105
|
+
})
|
|
106
|
+
}), Q = {
|
|
107
|
+
globalEnvData: {},
|
|
108
|
+
collectEnvDataForm: [
|
|
109
|
+
{
|
|
110
|
+
key: "name",
|
|
111
|
+
label: "项目名"
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
key: "chineseName",
|
|
115
|
+
label: "项目中文名"
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
key: "description",
|
|
119
|
+
label: "描述",
|
|
120
|
+
initial: "这是一个描述"
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
key: "repositoryUrl",
|
|
124
|
+
label: "仓库地址",
|
|
125
|
+
initial: "",
|
|
126
|
+
bindKey: "REPOSITORY_URL"
|
|
127
|
+
}
|
|
128
|
+
],
|
|
129
|
+
list: [
|
|
130
|
+
{
|
|
131
|
+
input: "./package.json",
|
|
132
|
+
output: "./package.json",
|
|
133
|
+
mode: "replace"
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
input: "./README.md",
|
|
137
|
+
output: "./README.md",
|
|
138
|
+
mode: "replace"
|
|
139
|
+
}
|
|
140
|
+
]
|
|
141
|
+
}, X = () => N({
|
|
142
|
+
configPathDefault: I
|
|
143
|
+
}), ee = async (e) => {
|
|
144
|
+
let t = {};
|
|
145
|
+
if (e)
|
|
146
|
+
t = Q;
|
|
147
|
+
else {
|
|
148
|
+
const { globalEnvCount: s, collectEnvCount: c, compileFileCount: l } = await R([
|
|
149
|
+
{
|
|
150
|
+
name: "globalEnvCount",
|
|
151
|
+
type: "number",
|
|
152
|
+
message: "全局固定变量数量",
|
|
153
|
+
initial: 1,
|
|
154
|
+
format: (o) => Number(o) || 0,
|
|
155
|
+
validate: (o) => o >= 0
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
name: "collectEnvCount",
|
|
159
|
+
type: "number",
|
|
160
|
+
message: "采集变量数量",
|
|
161
|
+
initial: 1,
|
|
162
|
+
format: (o) => Number(o) || 0,
|
|
163
|
+
validate: (o) => o >= 0
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
name: "compileFileCount",
|
|
167
|
+
type: "number",
|
|
168
|
+
message: "预编译文件数量",
|
|
169
|
+
initial: 1,
|
|
170
|
+
format: (o) => Number(o) || 0,
|
|
171
|
+
validate: (o) => o >= 0
|
|
172
|
+
}
|
|
173
|
+
]);
|
|
174
|
+
s && (t.globalEnvData = Array.from({
|
|
175
|
+
length: s
|
|
176
|
+
}).reduce((o, i, r) => (o[`GLOBAL_${r}`] = "", o), {})), c && (t.collectEnvDataForm = Array.from({
|
|
177
|
+
length: c
|
|
178
|
+
}).map((o, i) => ({
|
|
179
|
+
key: `COLLECT_KEY_${i}`,
|
|
180
|
+
label: `COLLECT_LABEL_${i}`,
|
|
181
|
+
initial: void 0
|
|
182
|
+
}))), l && (t.list = Array.from({
|
|
183
|
+
length: l
|
|
184
|
+
}).map((o, i) => ({
|
|
185
|
+
input: `(相对于命令运行目录)需要编译的模板文件路径${i}`,
|
|
186
|
+
output: `(相对于命令运行目录)编译后输出的文件路径${i}`,
|
|
187
|
+
mode: b.REPLACE
|
|
188
|
+
})));
|
|
189
|
+
}
|
|
190
|
+
return t;
|
|
191
|
+
}, te = async (e) => {
|
|
192
|
+
const t = await V(), s = await ee(t);
|
|
193
|
+
await W(s, e, {
|
|
194
|
+
onFileGenerated(c) {
|
|
195
|
+
t || a.success(`配置文件已生成:${c}
|
|
196
|
+
请具体需要替换
|
|
197
|
+
globalEnvData中的 GLOBAL_\${index}及其对应值
|
|
198
|
+
collectEnvDataForm各项中 COLLECT_KEY_\${index} COLLECT_LABEL_\${index}
|
|
199
|
+
list各项中的 input值 output值
|
|
200
|
+
`);
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
}, x = ({
|
|
204
|
+
rootDir: e,
|
|
205
|
+
filePath: t,
|
|
206
|
+
dataInit: s,
|
|
207
|
+
limitJson: c,
|
|
208
|
+
filePathKey: l,
|
|
209
|
+
dataInitKey: o,
|
|
210
|
+
dealMarkdown: i = !1
|
|
211
|
+
}) => {
|
|
212
|
+
if (t) {
|
|
213
|
+
if (c && !t.endsWith(".json"))
|
|
214
|
+
return a.error(`${l}必须是json文件,请检查文件后缀名`), process.exit(1);
|
|
215
|
+
const r = f.readFileSync(
|
|
216
|
+
k.resolve(e, t),
|
|
217
|
+
"utf-8"
|
|
218
|
+
);
|
|
219
|
+
let u = r;
|
|
220
|
+
return i && t.endsWith(".md") && (u = r.replace(
|
|
221
|
+
/^\s*```[a-zA-Z0-9]+\s*[\r\n]+([\s\S]+?)```\s*$/,
|
|
222
|
+
"$1"
|
|
223
|
+
)), c ? JSON.parse(u) : u;
|
|
224
|
+
} else
|
|
225
|
+
return s ? (a.info(`${l} 为空,将使用${o}作为数据`), c ? JSON.parse(s) : s) : (a.error(`${l}与${o}不能同时为空`), process.exit(1));
|
|
226
|
+
}, P = (e, t) => {
|
|
227
|
+
if (!t)
|
|
228
|
+
return a.error(`${e}模式下output不能为空`), process.exit(1);
|
|
229
|
+
}, w = (e, t) => {
|
|
230
|
+
if (t && e === t)
|
|
231
|
+
return a.error("output与input不能相同"), process.exit(1);
|
|
232
|
+
}, ne = (e, t) => {
|
|
233
|
+
if (!t)
|
|
234
|
+
return a.error(`${e}模式下input不能为空`), process.exit(1);
|
|
235
|
+
}, M = async (e, {
|
|
236
|
+
rootDir: t,
|
|
237
|
+
rollbackDelFileAgree: s = !1
|
|
238
|
+
}) => {
|
|
239
|
+
const {
|
|
240
|
+
env: c,
|
|
241
|
+
input: l,
|
|
242
|
+
inputData: o,
|
|
243
|
+
output: i,
|
|
244
|
+
mode: r,
|
|
245
|
+
rollback: u,
|
|
246
|
+
rollbackDelNullFile: g,
|
|
247
|
+
dealMarkdown: D,
|
|
248
|
+
envData: E
|
|
249
|
+
} = e;
|
|
250
|
+
if (u)
|
|
251
|
+
switch (r) {
|
|
252
|
+
case b.REPLACE:
|
|
253
|
+
case b.RETURN: {
|
|
254
|
+
a.error(`${r}模式不支持回滚`);
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
a.stage(`开始处理模板
|
|
259
|
+
mode: ${r}
|
|
260
|
+
rollback: ${u}
|
|
261
|
+
`);
|
|
262
|
+
const m = x({
|
|
263
|
+
rootDir: t,
|
|
264
|
+
filePath: l,
|
|
265
|
+
dataInit: o,
|
|
266
|
+
limitJson: !1,
|
|
267
|
+
filePathKey: "input",
|
|
268
|
+
dataInitKey: "inputData",
|
|
269
|
+
dealMarkdown: D
|
|
270
|
+
}), d = O(m), C = typeof E == "function" ? E() : E, p = d(C);
|
|
271
|
+
switch (r) {
|
|
272
|
+
case b.OVERWRITE: {
|
|
273
|
+
P(r, i), w(i, l);
|
|
274
|
+
const n = k.resolve(t, i);
|
|
275
|
+
if (f.mkdirSync(k.dirname(n), { recursive: !0 }), f.existsSync(n)) {
|
|
276
|
+
if (u)
|
|
277
|
+
if (s || (await R({
|
|
278
|
+
type: "confirm",
|
|
279
|
+
name: "remove",
|
|
280
|
+
message: `${r}模式下回滚将删除${n},是否继续?`
|
|
281
|
+
})).remove) {
|
|
282
|
+
f.rmSync(n, { force: !0 }), a.success(`${r}模式下${n}已删除`);
|
|
283
|
+
return;
|
|
284
|
+
} else {
|
|
285
|
+
a.warn(`${r}模式下${n}回滚取消`);
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
a.info(`output:${n} 已存在,将覆盖`);
|
|
289
|
+
} else {
|
|
290
|
+
if (u) {
|
|
291
|
+
a.warn(`${r}模式下${n}不存在,无需回滚`);
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
a.stage(`output:${n} 不存在,将创建`);
|
|
295
|
+
}
|
|
296
|
+
f.writeFileSync(n, p, "utf-8"), a.success(`模板处理完成,输出到 ${n}`);
|
|
297
|
+
break;
|
|
298
|
+
}
|
|
299
|
+
case b.APPEND: {
|
|
300
|
+
P(r, i), w(i, l);
|
|
301
|
+
const n = k.resolve(t, i);
|
|
302
|
+
if (f.mkdirSync(k.dirname(n), { recursive: !0 }), f.existsSync(n)) {
|
|
303
|
+
const $ = f.readFileSync(n, "utf-8");
|
|
304
|
+
if (u) {
|
|
305
|
+
const L = $.replace(p, "");
|
|
306
|
+
L || !g ? f.writeFileSync(n, L, "utf-8") : (a.stage(`${r}模式下 文件为空 删除`), f.unlinkSync(n)), a.success(`${r}模式下${n}回滚完成`);
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
const y = $ + p;
|
|
310
|
+
f.writeFileSync(n, y, "utf-8"), a.success(`模板处理完成,追加到 ${n}`);
|
|
311
|
+
} else {
|
|
312
|
+
if (u) {
|
|
313
|
+
a.warn(`${r}模式下${n}不存在,无需回滚`);
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
a.stage(`output:${n} 不存在,将创建`), f.writeFileSync(n, p, "utf-8"), a.success(`模板处理完成,输出到 ${n}`);
|
|
317
|
+
}
|
|
318
|
+
break;
|
|
319
|
+
}
|
|
320
|
+
case b.REPLACE: {
|
|
321
|
+
if (i && a.warn(`output ${i} 将被忽略`), ne(r, l), c && c === l)
|
|
322
|
+
return a.error("env 与 input 不能相同"), process.exit(1);
|
|
323
|
+
const n = k.resolve(t, l);
|
|
324
|
+
let $ = n;
|
|
325
|
+
const y = O(n)(C);
|
|
326
|
+
y !== n && (a.success(`检测输入文件名也需要替换
|
|
327
|
+
${n} => ${y}`), f.rmSync(n), $ = y), f.mkdirSync(k.dirname($), { recursive: !0 }), f.writeFileSync($, p, "utf-8"), a.success(`模板处理完成,输出到 ${$}`);
|
|
328
|
+
break;
|
|
329
|
+
}
|
|
330
|
+
case b.RETURN:
|
|
331
|
+
return a.success("模板处理完成,返回结果(函数调用才会拿到返回值)"), p;
|
|
332
|
+
default:
|
|
333
|
+
return a.error(`mode ${r} 不支持`), process.exit(1);
|
|
334
|
+
}
|
|
335
|
+
return p;
|
|
336
|
+
}, ae = async ({
|
|
337
|
+
rootDir: e,
|
|
338
|
+
configPath: t = I,
|
|
339
|
+
itemDefaultRollback: s = !1,
|
|
340
|
+
extraEnvData: c = {}
|
|
341
|
+
}, l) => {
|
|
342
|
+
let o;
|
|
343
|
+
if (l ? o = l : o = await G({
|
|
344
|
+
rootDir: e,
|
|
345
|
+
configPath: t
|
|
346
|
+
}), !o)
|
|
347
|
+
return a.error("读取配置文件失败"), process.exit(1);
|
|
348
|
+
const {
|
|
349
|
+
list: i = [],
|
|
350
|
+
globalEnvData: r = {},
|
|
351
|
+
collectEnvDataForm: u = []
|
|
352
|
+
} = o, g = {};
|
|
353
|
+
for (const m of u) {
|
|
354
|
+
let d, C, p;
|
|
355
|
+
typeof m == "string" ? (d = m, C = m, p = void 0) : (d = m.key, C = m.label, p = m.initial), g[d] = (await R({
|
|
356
|
+
type: "text",
|
|
357
|
+
name: d,
|
|
358
|
+
message: `请输入${C}`,
|
|
359
|
+
initial: p,
|
|
360
|
+
format: (n) => n.trim(),
|
|
361
|
+
validate: (n) => n.length > 0 || `${C}不能为空`
|
|
362
|
+
}))[d];
|
|
363
|
+
}
|
|
364
|
+
const D = i.map((m) => {
|
|
365
|
+
const { rollback: d = s } = m, {
|
|
366
|
+
envData: C,
|
|
367
|
+
env: p,
|
|
368
|
+
input: n,
|
|
369
|
+
output: $,
|
|
370
|
+
...y
|
|
371
|
+
} = S(m);
|
|
372
|
+
return p && a.warn(`批量处理中 env:${p} 将被忽略, 只读envData`), {
|
|
373
|
+
...y,
|
|
374
|
+
env: p,
|
|
375
|
+
input: n,
|
|
376
|
+
output: $,
|
|
377
|
+
envData: B(
|
|
378
|
+
{},
|
|
379
|
+
c,
|
|
380
|
+
r,
|
|
381
|
+
g,
|
|
382
|
+
C
|
|
383
|
+
),
|
|
384
|
+
rollback: d
|
|
385
|
+
};
|
|
386
|
+
}), E = [];
|
|
387
|
+
for (const m of D) {
|
|
388
|
+
const d = await M(m, {
|
|
389
|
+
rollbackDelFileAgree: !0,
|
|
390
|
+
rootDir: e
|
|
391
|
+
});
|
|
392
|
+
E.push(d);
|
|
393
|
+
}
|
|
394
|
+
return E;
|
|
395
|
+
}, oe = async (e) => {
|
|
396
|
+
const {
|
|
397
|
+
envData: t,
|
|
398
|
+
env: s,
|
|
399
|
+
input: c,
|
|
400
|
+
inputData: l,
|
|
401
|
+
output: o,
|
|
402
|
+
mode: i,
|
|
403
|
+
rollback: r,
|
|
404
|
+
rollbackDelNullFile: u,
|
|
405
|
+
dealMarkdown: g,
|
|
406
|
+
batch: D,
|
|
407
|
+
rootDir: E,
|
|
408
|
+
configPath: m
|
|
409
|
+
} = S(e);
|
|
410
|
+
if (D)
|
|
411
|
+
return a.stage("开始批量处理"), ae({
|
|
412
|
+
// 回滚默认值 基于全局
|
|
413
|
+
itemDefaultRollback: r,
|
|
414
|
+
rootDir: E,
|
|
415
|
+
configPath: m
|
|
416
|
+
});
|
|
417
|
+
a.stage("开始单个处理");
|
|
418
|
+
const d = x({
|
|
419
|
+
rootDir: E,
|
|
420
|
+
filePath: s,
|
|
421
|
+
dataInit: t,
|
|
422
|
+
limitJson: !0,
|
|
423
|
+
filePathKey: "env",
|
|
424
|
+
dataInitKey: "envData",
|
|
425
|
+
dealMarkdown: g
|
|
426
|
+
});
|
|
427
|
+
return M(
|
|
428
|
+
{
|
|
429
|
+
input: c,
|
|
430
|
+
inputData: l,
|
|
431
|
+
output: o,
|
|
432
|
+
mode: i,
|
|
433
|
+
rollback: r,
|
|
434
|
+
rollbackDelNullFile: u,
|
|
435
|
+
dealMarkdown: g,
|
|
436
|
+
envData: d
|
|
437
|
+
},
|
|
438
|
+
{
|
|
439
|
+
rootDir: E
|
|
440
|
+
}
|
|
441
|
+
);
|
|
442
|
+
}, U = async (e, t) => e === h.INIT ? te(t) : e === h.COMPILE ? oe(t) : (a.error(`无效的命令: ${e}`), process.exit(1)), {
|
|
443
|
+
version: re,
|
|
444
|
+
description: ie,
|
|
445
|
+
cliConfig: { moduleName: F }
|
|
446
|
+
} = _, le = {
|
|
447
|
+
command: h.INIT,
|
|
448
|
+
describe: "初始化模板配置文件",
|
|
449
|
+
options: X(),
|
|
450
|
+
handler: T(U)(
|
|
451
|
+
h.INIT
|
|
452
|
+
)
|
|
453
|
+
}, se = {
|
|
454
|
+
command: h.COMPILE,
|
|
455
|
+
describe: "编译模板",
|
|
456
|
+
options: Z(),
|
|
457
|
+
handler: T(U)(
|
|
458
|
+
h.COMPILE
|
|
459
|
+
)
|
|
460
|
+
}, j = {
|
|
461
|
+
describe: ie,
|
|
462
|
+
version: re,
|
|
463
|
+
subcommands: [le, se].map(
|
|
464
|
+
A
|
|
465
|
+
),
|
|
466
|
+
demandCommandCount: 1
|
|
467
|
+
}, H = (e = !1) => {
|
|
468
|
+
const t = e ? F : void 0, s = `$0${e ? ` ${F}` : ""} <command> [options]`;
|
|
469
|
+
return { command: t, usage: s };
|
|
470
|
+
}, de = async () => Y({
|
|
471
|
+
...j,
|
|
472
|
+
...H()
|
|
473
|
+
}), be = () => A({
|
|
474
|
+
...j,
|
|
475
|
+
...H(!0)
|
|
476
|
+
});
|
|
477
|
+
export {
|
|
478
|
+
I as M,
|
|
479
|
+
b as O,
|
|
480
|
+
h as S,
|
|
481
|
+
J as T,
|
|
482
|
+
be as a,
|
|
483
|
+
ae as b,
|
|
484
|
+
oe as c,
|
|
485
|
+
de as d,
|
|
486
|
+
U as h,
|
|
487
|
+
te as i
|
|
488
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@done-coding/cli-template",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.2-alpha.0",
|
|
4
4
|
"description": "预编译命令行工具",
|
|
5
5
|
"private": false,
|
|
6
6
|
"module": "es/index.mjs",
|
|
@@ -17,8 +17,7 @@
|
|
|
17
17
|
"files": [
|
|
18
18
|
"es",
|
|
19
19
|
"lib",
|
|
20
|
-
"types"
|
|
21
|
-
"gif"
|
|
20
|
+
"types"
|
|
22
21
|
],
|
|
23
22
|
"scripts": {
|
|
24
23
|
"clean": "rimraf es lib types",
|
|
@@ -41,11 +40,8 @@
|
|
|
41
40
|
"license": "MIT",
|
|
42
41
|
"sideEffects": false,
|
|
43
42
|
"devDependencies": {
|
|
44
|
-
"@done-coding/cli-inject": "^0.5.
|
|
43
|
+
"@done-coding/cli-inject": "^0.5.1-alpha.0",
|
|
45
44
|
"@types/lodash.assign": "^4.2.9",
|
|
46
|
-
"@types/lodash.curry": "^4.1.8",
|
|
47
|
-
"@types/lodash.get": "^4.4.9",
|
|
48
|
-
"@types/lodash.set": "^4.3.9",
|
|
49
45
|
"@types/lodash.template": "^4.5.3",
|
|
50
46
|
"@types/node": "^18.0.0",
|
|
51
47
|
"@types/yargs": "^17.0.28",
|
|
@@ -58,12 +54,9 @@
|
|
|
58
54
|
"node": ">=18.0.0"
|
|
59
55
|
},
|
|
60
56
|
"dependencies": {
|
|
61
|
-
"@done-coding/cli-utils": "^0.3.
|
|
57
|
+
"@done-coding/cli-utils": "^0.3.1-alpha.0",
|
|
62
58
|
"lodash.assign": "^4.2.0",
|
|
63
|
-
"lodash.curry": "^4.1.1",
|
|
64
|
-
"lodash.get": "^4.4.2",
|
|
65
|
-
"lodash.set": "^4.3.2",
|
|
66
59
|
"lodash.template": "^4.5.0"
|
|
67
60
|
},
|
|
68
|
-
"gitHead": "
|
|
61
|
+
"gitHead": "ec6957606bbae68d7268159b38b9d36dab4a2731"
|
|
69
62
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { handler } from './handler';
|
|
2
|
-
export { initHandler, compileHandler, batchCompileHandler } from './utils';
|
|
2
|
+
export { initHandler, compileHandler, batchCompileHandler, MODULE_DEFAULT_CONFIG_RELATIVE_PATH, } from './utils';
|
|
3
3
|
export { crateAsSubcommand } from "./main";
|
|
4
4
|
export * from './utils/types';
|
|
5
5
|
export * from './utils/config';
|
package/types/utils/compile.d.ts
CHANGED
|
@@ -2,9 +2,11 @@ import type { CompileTemplateConfig } from '.';
|
|
|
2
2
|
import { type CompileOptions } from '.';
|
|
3
3
|
import type { CliHandlerArgv } from "@done-coding/cli-utils";
|
|
4
4
|
/** 批量编译模板 */
|
|
5
|
-
export declare const batchCompileHandler: ({ rootDir, itemDefaultRollback, extraEnvData, }
|
|
6
|
-
/**
|
|
7
|
-
rootDir
|
|
5
|
+
export declare const batchCompileHandler: ({ rootDir, configPath, itemDefaultRollback, extraEnvData, }: {
|
|
6
|
+
/** 项目更目录 */
|
|
7
|
+
rootDir: string;
|
|
8
|
+
/** 配置文件路径 */
|
|
9
|
+
configPath?: string | undefined;
|
|
8
10
|
/** item默认回滚? */
|
|
9
11
|
itemDefaultRollback?: boolean | undefined;
|
|
10
12
|
/** 额外环境变量 */
|
package/types/utils/config.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
+
import type { ReadConfigFileOptions } from "@done-coding/cli-utils";
|
|
1
2
|
/** 获取配置文件路径 */
|
|
2
|
-
export declare const getConfigPath: (rootDir
|
|
3
|
+
export declare const getConfigPath: ({ rootDir, configPath, }: ReadConfigFileOptions) => string | undefined;
|
|
@@ -1,10 +1,21 @@
|
|
|
1
|
+
import { type CliInfo } from "@done-coding/cli-utils";
|
|
1
2
|
import { OutputModeEnum, type CompileOptions } from "./types";
|
|
2
|
-
/**
|
|
3
|
-
export declare const
|
|
4
|
-
/**
|
|
5
|
-
export declare const
|
|
3
|
+
/** 编译默认选项 */
|
|
4
|
+
export declare const defaultCompileOptions: Pick<CompileOptions, "rollback" | "dealMarkdown" | "mode" | "batch" | "rollbackDelNullFile">;
|
|
5
|
+
/** 设置编译默认选项 */
|
|
6
|
+
export declare const completeDefaultCompileOptions: <T extends Pick<CompileOptions, "mode" | "rollback" | "rollbackDelNullFile" | "dealMarkdown" | "batch">>(options: T) => {
|
|
7
|
+
env?: string | undefined;
|
|
8
|
+
envData?: string | undefined;
|
|
9
|
+
input?: string | undefined;
|
|
10
|
+
inputData?: string | undefined;
|
|
11
|
+
output?: string | undefined;
|
|
6
12
|
rollback: boolean | undefined;
|
|
13
|
+
rollbackDelNullFile: boolean | undefined;
|
|
7
14
|
dealMarkdown: boolean | undefined;
|
|
8
15
|
mode: OutputModeEnum;
|
|
9
16
|
batch: boolean | undefined;
|
|
10
|
-
|
|
17
|
+
rootDir: string;
|
|
18
|
+
configPath: string;
|
|
19
|
+
};
|
|
20
|
+
/** 获取编译选项 */
|
|
21
|
+
export declare const getCompileOptions: () => CliInfo["options"];
|
package/types/utils/index.d.ts
CHANGED
package/types/utils/init.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
/// <reference types="yargs" />
|
|
1
2
|
import { type InitOptions } from '.';
|
|
2
3
|
import { type CliHandlerArgv } from "@done-coding/cli-utils";
|
|
3
|
-
export declare const
|
|
4
|
+
export declare const getInitOptions: () => Record<keyof import("@done-coding/cli-utils").ConfigFileCommonOptions, import("yargs").Options>;
|
|
4
5
|
/** 初始化模板 */
|
|
5
6
|
export declare const initHandler: (argv: CliHandlerArgv<InitOptions>) => Promise<void>;
|
package/types/utils/types.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { InitConfigFileOptions, ReadConfigFileOptions } from "@done-coding/cli-utils";
|
|
1
2
|
/** 子命令枚举 */
|
|
2
3
|
export declare enum SubcommandEnum {
|
|
3
4
|
/** 初始化模板 */
|
|
@@ -5,21 +6,7 @@ export declare enum SubcommandEnum {
|
|
|
5
6
|
/** 编译模板 */
|
|
6
7
|
COMPILE = "compile"
|
|
7
8
|
}
|
|
8
|
-
export
|
|
9
|
-
/** 配置文件绝对路径 */
|
|
10
|
-
configPath: string;
|
|
11
|
-
/** 配置文件中编译文件相对目录运行根目录 */
|
|
12
|
-
rootDir: string;
|
|
13
|
-
}
|
|
14
|
-
/** 编辑器类型枚举 */
|
|
15
|
-
export declare enum EditorTypeEnum {
|
|
16
|
-
/** vscode */
|
|
17
|
-
VSCODE = "VsCode",
|
|
18
|
-
/** cursor */
|
|
19
|
-
CURSOR = "Cursor",
|
|
20
|
-
/** 其他编辑器 */
|
|
21
|
-
OTHER = "\u5176\u4ED6"
|
|
22
|
-
}
|
|
9
|
+
export type InitOptions = InitConfigFileOptions;
|
|
23
10
|
/** 绑定的关键变量枚举 */
|
|
24
11
|
export declare enum TemplateBindKeyEnum {
|
|
25
12
|
/** 远程仓库地址 */
|
|
@@ -37,6 +24,10 @@ export declare enum OutputModeEnum {
|
|
|
37
24
|
RETURN = "return"
|
|
38
25
|
}
|
|
39
26
|
export interface CompileOptions {
|
|
27
|
+
/** 项目根目录 */
|
|
28
|
+
rootDir: string;
|
|
29
|
+
/** 配置文件路径 */
|
|
30
|
+
configPath?: string;
|
|
40
31
|
/** 环境数据(json)文件(优先级高于 envData ) */
|
|
41
32
|
env?: string;
|
|
42
33
|
/** 环境变量数据(JSON字符串) */
|
|
@@ -51,13 +42,21 @@ export interface CompileOptions {
|
|
|
51
42
|
mode: OutputModeEnum;
|
|
52
43
|
/** 是否回滚 @default false */
|
|
53
44
|
rollback?: boolean;
|
|
45
|
+
/**
|
|
46
|
+
* 回滚删除空文件
|
|
47
|
+
* ---
|
|
48
|
+
* 只限 OutputModeEnum.APPEND 模式下生效
|
|
49
|
+
*/
|
|
50
|
+
rollbackDelNullFile?: boolean;
|
|
54
51
|
/** (检测是markdown)是否处理(单个)代码块包裹 */
|
|
55
52
|
dealMarkdown?: boolean;
|
|
56
53
|
/** 是否批量处理 */
|
|
57
54
|
batch?: boolean;
|
|
58
55
|
}
|
|
59
|
-
/**
|
|
60
|
-
|
|
56
|
+
/**
|
|
57
|
+
* 编译模板配置选项
|
|
58
|
+
*/
|
|
59
|
+
export type CompileTemplateConfigListItem = Omit<CompileOptions, "envData" | keyof ReadConfigFileOptions> & {
|
|
61
60
|
/** 已经解析为对象的envData */
|
|
62
61
|
envData: Record<string, any>;
|
|
63
62
|
};
|
|
@@ -79,5 +78,5 @@ export interface CompileTemplateConfig {
|
|
|
79
78
|
/** 采集环境变量表单配置 */
|
|
80
79
|
collectEnvDataForm?: (CollectFormItem | string)[];
|
|
81
80
|
/** 配置列表 */
|
|
82
|
-
list?: CompileOptions[];
|
|
81
|
+
list?: Omit<CompileOptions, keyof ReadConfigFileOptions>[];
|
|
83
82
|
}
|
package/es/index-34359db9.js
DELETED
|
@@ -1,521 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import v from "node:path";
|
|
3
|
-
import f, { existsSync as j, mkdirSync as J, writeFileSync as H } from "node:fs";
|
|
4
|
-
import { xPrompts as k, log as n, createSubcommand as L, createMainCommand as W } from "@done-coding/cli-utils";
|
|
5
|
-
import { execSync as O } from "node:child_process";
|
|
6
|
-
import S from "lodash.template";
|
|
7
|
-
import Y from "lodash.assign";
|
|
8
|
-
import T from "lodash.curry";
|
|
9
|
-
const B = "./.done-coding/template.json";
|
|
10
|
-
var h = /* @__PURE__ */ ((e) => (e.INIT = "init", e.COMPILE = "compile", e))(h || {}), R = /* @__PURE__ */ ((e) => (e.VSCODE = "VsCode", e.CURSOR = "Cursor", e.OTHER = "其他", e))(R || {}), K = /* @__PURE__ */ ((e) => (e.REPOSITORY_URL = "REPOSITORY_URL", e))(K || {}), g = /* @__PURE__ */ ((e) => (e.OVERWRITE = "overwrite", e.APPEND = "append", e.REPLACE = "replace", e.RETURN = "return", e))(g || {});
|
|
11
|
-
const C = {
|
|
12
|
-
rollback: !1,
|
|
13
|
-
dealMarkdown: !1,
|
|
14
|
-
mode: g.OVERWRITE,
|
|
15
|
-
batch: !1
|
|
16
|
-
}, A = (e) => {
|
|
17
|
-
const { rollback: r, dealMarkdown: c, mode: s, batch: p, ...u } = e;
|
|
18
|
-
return {
|
|
19
|
-
rollback: r ?? C.rollback,
|
|
20
|
-
dealMarkdown: c ?? C.dealMarkdown,
|
|
21
|
-
mode: s ?? C.mode,
|
|
22
|
-
batch: p ?? C.batch,
|
|
23
|
-
...u
|
|
24
|
-
};
|
|
25
|
-
}, F = {
|
|
26
|
-
version: "0.7.1-alpha.0",
|
|
27
|
-
name: "@done-coding/cli-template",
|
|
28
|
-
description: "预编译命令行工具",
|
|
29
|
-
cliConfig: {
|
|
30
|
-
namespaceDir: ".done-coding",
|
|
31
|
-
moduleName: "template"
|
|
32
|
-
}
|
|
33
|
-
}, G = (e) => {
|
|
34
|
-
const { namespaceDir: r, moduleName: c } = F.cliConfig, s = [r, `${c}.json`];
|
|
35
|
-
e && s.unshift(e);
|
|
36
|
-
const p = v.resolve(...s);
|
|
37
|
-
if (f.existsSync(p))
|
|
38
|
-
return p;
|
|
39
|
-
}, q = {
|
|
40
|
-
globalEnvData: {},
|
|
41
|
-
collectEnvDataForm: [
|
|
42
|
-
{
|
|
43
|
-
key: "name",
|
|
44
|
-
label: "项目名"
|
|
45
|
-
},
|
|
46
|
-
{
|
|
47
|
-
key: "chineseName",
|
|
48
|
-
label: "项目中文名"
|
|
49
|
-
},
|
|
50
|
-
{
|
|
51
|
-
key: "description",
|
|
52
|
-
label: "描述",
|
|
53
|
-
initial: "这是一个描述"
|
|
54
|
-
},
|
|
55
|
-
{
|
|
56
|
-
key: "repositoryUrl",
|
|
57
|
-
label: "仓库地址",
|
|
58
|
-
initial: "",
|
|
59
|
-
bindKey: "REPOSITORY_URL"
|
|
60
|
-
}
|
|
61
|
-
],
|
|
62
|
-
list: [
|
|
63
|
-
{
|
|
64
|
-
input: "./package.json",
|
|
65
|
-
output: "./package.json",
|
|
66
|
-
mode: "replace"
|
|
67
|
-
},
|
|
68
|
-
{
|
|
69
|
-
input: "./README.md",
|
|
70
|
-
output: "./README.md",
|
|
71
|
-
mode: "replace"
|
|
72
|
-
}
|
|
73
|
-
]
|
|
74
|
-
}, w = (e, r, c) => {
|
|
75
|
-
try {
|
|
76
|
-
O(`${e} -v`, { stdio: "ignore" }), O(`${e} ${r}`);
|
|
77
|
-
} catch {
|
|
78
|
-
c();
|
|
79
|
-
}
|
|
80
|
-
}, z = async (e) => {
|
|
81
|
-
const { configPath: r } = e, { useDefaultConfig: c } = await k({
|
|
82
|
-
name: "useDefaultConfig",
|
|
83
|
-
type: "confirm",
|
|
84
|
-
message: "使用默认模板配置"
|
|
85
|
-
});
|
|
86
|
-
let s = {};
|
|
87
|
-
if (c)
|
|
88
|
-
s = q;
|
|
89
|
-
else {
|
|
90
|
-
const { globalEnvCount: b, collectEnvCount: l, compileFileCount: m } = await k([
|
|
91
|
-
{
|
|
92
|
-
name: "globalEnvCount",
|
|
93
|
-
type: "number",
|
|
94
|
-
message: "全局固定变量数量",
|
|
95
|
-
initial: 1,
|
|
96
|
-
format: (a) => Number(a) || 0,
|
|
97
|
-
validate: (a) => a >= 0
|
|
98
|
-
},
|
|
99
|
-
{
|
|
100
|
-
name: "collectEnvCount",
|
|
101
|
-
type: "number",
|
|
102
|
-
message: "采集变量数量",
|
|
103
|
-
initial: 1,
|
|
104
|
-
format: (a) => Number(a) || 0,
|
|
105
|
-
validate: (a) => a >= 0
|
|
106
|
-
},
|
|
107
|
-
{
|
|
108
|
-
name: "compileFileCount",
|
|
109
|
-
type: "number",
|
|
110
|
-
message: "预编译文件数量",
|
|
111
|
-
initial: 1,
|
|
112
|
-
format: (a) => Number(a) || 0,
|
|
113
|
-
validate: (a) => a >= 0
|
|
114
|
-
}
|
|
115
|
-
]);
|
|
116
|
-
b && (s.globalEnvData = Array.from({
|
|
117
|
-
length: b
|
|
118
|
-
}).reduce((a, t, d) => (a[`GLOBAL_${d}`] = "", a), {})), l && (s.collectEnvDataForm = Array.from({
|
|
119
|
-
length: l
|
|
120
|
-
}).map((a, t) => ({
|
|
121
|
-
key: `COLLECT_KEY_${t}`,
|
|
122
|
-
label: `COLLECT_LABEL_${t}`,
|
|
123
|
-
initial: void 0
|
|
124
|
-
}))), m && (s.list = Array.from({
|
|
125
|
-
length: m
|
|
126
|
-
}).map((a, t) => ({
|
|
127
|
-
input: `(相对于命令运行目录)需要编译的模板文件路径${t}`,
|
|
128
|
-
output: `(相对于命令运行目录)编译后输出的文件路径${t}`,
|
|
129
|
-
mode: g.REPLACE
|
|
130
|
-
})));
|
|
131
|
-
}
|
|
132
|
-
const p = await k([
|
|
133
|
-
{
|
|
134
|
-
name: "configPath",
|
|
135
|
-
type: "text",
|
|
136
|
-
message: "配置文件生成路径",
|
|
137
|
-
initial: r
|
|
138
|
-
},
|
|
139
|
-
{
|
|
140
|
-
name: "editorType",
|
|
141
|
-
type: "select",
|
|
142
|
-
message: "编辑器类型",
|
|
143
|
-
choices: [
|
|
144
|
-
R.CURSOR,
|
|
145
|
-
R.VSCODE,
|
|
146
|
-
R.OTHER
|
|
147
|
-
].map((b) => ({
|
|
148
|
-
title: b,
|
|
149
|
-
value: b
|
|
150
|
-
}))
|
|
151
|
-
}
|
|
152
|
-
]), { configPath: u, editorType: o } = p, i = v.resolve(u), E = v.dirname(i);
|
|
153
|
-
j(E) || J(E, {
|
|
154
|
-
recursive: !0
|
|
155
|
-
}), H(i, JSON.stringify(s, null, 2)), c || n.success(`配置文件已生成:${i}
|
|
156
|
-
请具体需要替换
|
|
157
|
-
globalEnvData中的 GLOBAL_\${index}及其对应值
|
|
158
|
-
collectEnvDataForm各项中 COLLECT_KEY_\${index} COLLECT_LABEL_\${index}
|
|
159
|
-
list各项中的 input值 output值
|
|
160
|
-
`);
|
|
161
|
-
const y = (b) => n.info(`
|
|
162
|
-
${b}, 请用编辑器打开 ${i} 进行编辑
|
|
163
|
-
`);
|
|
164
|
-
switch (o) {
|
|
165
|
-
case R.CURSOR: {
|
|
166
|
-
w("cursor", i, () => {
|
|
167
|
-
y("cursor命令未安装");
|
|
168
|
-
});
|
|
169
|
-
break;
|
|
170
|
-
}
|
|
171
|
-
case R.VSCODE: {
|
|
172
|
-
w("code", i, () => {
|
|
173
|
-
y("code命令未安装");
|
|
174
|
-
});
|
|
175
|
-
break;
|
|
176
|
-
}
|
|
177
|
-
default:
|
|
178
|
-
y("其他编辑器");
|
|
179
|
-
}
|
|
180
|
-
}, _ = ({
|
|
181
|
-
filePath: e,
|
|
182
|
-
dataInit: r,
|
|
183
|
-
limitJson: c,
|
|
184
|
-
filePathKey: s,
|
|
185
|
-
dataInitKey: p,
|
|
186
|
-
dealMarkdown: u = !1
|
|
187
|
-
}) => {
|
|
188
|
-
if (e) {
|
|
189
|
-
if (c && !e.endsWith(".json"))
|
|
190
|
-
return n.error(`${s}必须是json文件,请检查文件后缀名`), process.exit(1);
|
|
191
|
-
const o = f.readFileSync(v.resolve(e), "utf-8");
|
|
192
|
-
let i = o;
|
|
193
|
-
return u && e.endsWith(".md") && (i = o.replace(
|
|
194
|
-
/^\s*```[a-zA-Z0-9]+\s*[\r\n]+([\s\S]+?)```\s*$/,
|
|
195
|
-
"$1"
|
|
196
|
-
)), c ? JSON.parse(i) : i;
|
|
197
|
-
} else
|
|
198
|
-
return r ? (n.info(`${s} 为空,将使用${p}作为数据`), c ? JSON.parse(r) : r) : (n.error(`${s}与${p}不能同时为空`), process.exit(1));
|
|
199
|
-
}, I = (e, r) => {
|
|
200
|
-
if (!r)
|
|
201
|
-
return n.error(`${e}模式下output不能为空`), process.exit(1);
|
|
202
|
-
}, P = (e, r) => {
|
|
203
|
-
if (r && e === r)
|
|
204
|
-
return n.error("output与input不能相同"), process.exit(1);
|
|
205
|
-
}, Z = (e, r) => {
|
|
206
|
-
if (!r)
|
|
207
|
-
return n.error(`${e}模式下input不能为空`), process.exit(1);
|
|
208
|
-
}, x = async (e, {
|
|
209
|
-
rollbackDelFileAgree: r = !1
|
|
210
|
-
} = {}) => {
|
|
211
|
-
const {
|
|
212
|
-
env: c,
|
|
213
|
-
input: s,
|
|
214
|
-
inputData: p,
|
|
215
|
-
output: u,
|
|
216
|
-
mode: o,
|
|
217
|
-
rollback: i,
|
|
218
|
-
dealMarkdown: E,
|
|
219
|
-
envData: y
|
|
220
|
-
} = e;
|
|
221
|
-
if (i)
|
|
222
|
-
switch (o) {
|
|
223
|
-
case g.REPLACE:
|
|
224
|
-
case g.RETURN: {
|
|
225
|
-
n.error(`${o}模式不支持回滚`);
|
|
226
|
-
return;
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
n.stage(`开始处理模板
|
|
230
|
-
mode: ${o}
|
|
231
|
-
rollback: ${i}
|
|
232
|
-
`);
|
|
233
|
-
const b = _({
|
|
234
|
-
filePath: s,
|
|
235
|
-
dataInit: p,
|
|
236
|
-
limitJson: !1,
|
|
237
|
-
filePathKey: "input",
|
|
238
|
-
dataInitKey: "inputData",
|
|
239
|
-
dealMarkdown: E
|
|
240
|
-
}), l = S(b), m = typeof y == "function" ? y() : y, a = l(m);
|
|
241
|
-
switch (o) {
|
|
242
|
-
case g.OVERWRITE: {
|
|
243
|
-
I(o, u), P(u, s);
|
|
244
|
-
const t = v.resolve(u);
|
|
245
|
-
if (f.mkdirSync(v.dirname(t), { recursive: !0 }), f.existsSync(t)) {
|
|
246
|
-
if (i)
|
|
247
|
-
if (r || (await k({
|
|
248
|
-
type: "confirm",
|
|
249
|
-
name: "remove",
|
|
250
|
-
message: `${o}模式下回滚将删除${t},是否继续?`
|
|
251
|
-
})).remove) {
|
|
252
|
-
f.rmSync(t, { force: !0 }), n.success(`${o}模式下${t}已删除`);
|
|
253
|
-
return;
|
|
254
|
-
} else {
|
|
255
|
-
n.warn(`${o}模式下${t}回滚取消`);
|
|
256
|
-
return;
|
|
257
|
-
}
|
|
258
|
-
n.info(`output:${t} 已存在,将覆盖`);
|
|
259
|
-
} else {
|
|
260
|
-
if (i) {
|
|
261
|
-
n.warn(`${o}模式下${t}不存在,无需回滚`);
|
|
262
|
-
return;
|
|
263
|
-
}
|
|
264
|
-
n.stage(`output:${t} 不存在,将创建`);
|
|
265
|
-
}
|
|
266
|
-
f.writeFileSync(t, a, "utf-8"), n.success(`模板处理完成,输出到 ${t}`);
|
|
267
|
-
break;
|
|
268
|
-
}
|
|
269
|
-
case g.APPEND: {
|
|
270
|
-
I(o, u), P(u, s);
|
|
271
|
-
const t = v.resolve(u);
|
|
272
|
-
if (f.mkdirSync(v.dirname(t), { recursive: !0 }), f.existsSync(t)) {
|
|
273
|
-
const d = f.readFileSync(t, "utf-8");
|
|
274
|
-
if (i) {
|
|
275
|
-
f.writeFileSync(
|
|
276
|
-
t,
|
|
277
|
-
d.replace(a, ""),
|
|
278
|
-
"utf-8"
|
|
279
|
-
), n.success(`${o}模式下${t}回滚完成`);
|
|
280
|
-
return;
|
|
281
|
-
}
|
|
282
|
-
const $ = d + a;
|
|
283
|
-
f.writeFileSync(t, $, "utf-8"), n.success(`模板处理完成,追加到 ${t}`);
|
|
284
|
-
} else {
|
|
285
|
-
if (i) {
|
|
286
|
-
n.warn(`${o}模式下${t}不存在,无需回滚`);
|
|
287
|
-
return;
|
|
288
|
-
}
|
|
289
|
-
n.stage(`output:${t} 不存在,将创建`), f.writeFileSync(t, a, "utf-8"), n.success(`模板处理完成,输出到 ${t}`);
|
|
290
|
-
}
|
|
291
|
-
break;
|
|
292
|
-
}
|
|
293
|
-
case g.REPLACE: {
|
|
294
|
-
if (u && n.warn(`output ${u} 将被忽略`), Z(o, s), c && c === s)
|
|
295
|
-
return n.error("env 与 input 不能相同"), process.exit(1);
|
|
296
|
-
const t = v.resolve(s);
|
|
297
|
-
let d = t;
|
|
298
|
-
const $ = s.replace(`${process.cwd()}/`, ""), D = S($)(m);
|
|
299
|
-
D !== $ && (n.success(`检测输入文件名也需要替换
|
|
300
|
-
./${$} => ./${D} `), f.rmSync(t), d = v.resolve(`./${D}`)), f.mkdirSync(v.dirname(d), { recursive: !0 }), f.writeFileSync(d, a, "utf-8"), n.success(`模板处理完成,输出到 ${d}`);
|
|
301
|
-
break;
|
|
302
|
-
}
|
|
303
|
-
case g.RETURN:
|
|
304
|
-
return n.success("模板处理完成,返回结果(函数调用才会拿到返回值)"), a;
|
|
305
|
-
default:
|
|
306
|
-
return n.error(`mode ${o} 不支持`), process.exit(1);
|
|
307
|
-
}
|
|
308
|
-
return a;
|
|
309
|
-
}, Q = async ({
|
|
310
|
-
rootDir: e = process.cwd(),
|
|
311
|
-
itemDefaultRollback: r = !1,
|
|
312
|
-
extraEnvData: c = {}
|
|
313
|
-
} = {}, s) => {
|
|
314
|
-
let p;
|
|
315
|
-
if (s)
|
|
316
|
-
p = s;
|
|
317
|
-
else {
|
|
318
|
-
const l = G(e);
|
|
319
|
-
if (!l)
|
|
320
|
-
return n.error(`配置文件${l}不存在`), process.exit(1);
|
|
321
|
-
const m = f.readFileSync(l, "utf-8");
|
|
322
|
-
p = JSON.parse(m);
|
|
323
|
-
}
|
|
324
|
-
const {
|
|
325
|
-
list: u = [],
|
|
326
|
-
globalEnvData: o = {},
|
|
327
|
-
collectEnvDataForm: i = []
|
|
328
|
-
} = p, E = {};
|
|
329
|
-
for (const l of i) {
|
|
330
|
-
let m, a, t;
|
|
331
|
-
typeof l == "string" ? (m = l, a = l, t = void 0) : (m = l.key, a = l.label, t = l.initial), E[m] = (await k({
|
|
332
|
-
type: "text",
|
|
333
|
-
name: m,
|
|
334
|
-
message: `请输入${a}`,
|
|
335
|
-
initial: t,
|
|
336
|
-
format: (d) => d.trim(),
|
|
337
|
-
validate: (d) => d.length > 0 || `${a}不能为空`
|
|
338
|
-
}))[m];
|
|
339
|
-
}
|
|
340
|
-
const y = u.map((l) => {
|
|
341
|
-
const { rollback: m = r } = l, {
|
|
342
|
-
envData: a,
|
|
343
|
-
env: t,
|
|
344
|
-
input: d,
|
|
345
|
-
output: $,
|
|
346
|
-
...D
|
|
347
|
-
} = A(l);
|
|
348
|
-
return t && n.warn(`批量处理中 env:${t} 将被忽略,只读envData`), {
|
|
349
|
-
...D,
|
|
350
|
-
env: t,
|
|
351
|
-
input: e && d ? v.resolve(e, d) : d,
|
|
352
|
-
output: e && $ ? v.resolve(e, $) : $,
|
|
353
|
-
envData: Y(
|
|
354
|
-
{},
|
|
355
|
-
c,
|
|
356
|
-
o,
|
|
357
|
-
E,
|
|
358
|
-
a
|
|
359
|
-
),
|
|
360
|
-
rollback: m
|
|
361
|
-
};
|
|
362
|
-
}), b = [];
|
|
363
|
-
for (const l of y) {
|
|
364
|
-
const m = await x(l, {
|
|
365
|
-
rollbackDelFileAgree: !0
|
|
366
|
-
});
|
|
367
|
-
b.push(m);
|
|
368
|
-
}
|
|
369
|
-
return b;
|
|
370
|
-
}, X = async (e) => {
|
|
371
|
-
const {
|
|
372
|
-
envData: r,
|
|
373
|
-
env: c,
|
|
374
|
-
input: s,
|
|
375
|
-
inputData: p,
|
|
376
|
-
output: u,
|
|
377
|
-
mode: o,
|
|
378
|
-
rollback: i,
|
|
379
|
-
dealMarkdown: E,
|
|
380
|
-
batch: y
|
|
381
|
-
} = A(e);
|
|
382
|
-
if (y)
|
|
383
|
-
return n.stage("开始批量处理"), Q({
|
|
384
|
-
// 回滚默认值 基于全局
|
|
385
|
-
itemDefaultRollback: i
|
|
386
|
-
});
|
|
387
|
-
n.stage("开始单个处理");
|
|
388
|
-
const b = _({
|
|
389
|
-
filePath: c,
|
|
390
|
-
dataInit: r,
|
|
391
|
-
limitJson: !0,
|
|
392
|
-
filePathKey: "env",
|
|
393
|
-
dataInitKey: "envData",
|
|
394
|
-
dealMarkdown: E
|
|
395
|
-
});
|
|
396
|
-
return x({
|
|
397
|
-
input: s,
|
|
398
|
-
inputData: p,
|
|
399
|
-
output: u,
|
|
400
|
-
mode: o,
|
|
401
|
-
rollback: i,
|
|
402
|
-
dealMarkdown: E,
|
|
403
|
-
envData: b
|
|
404
|
-
});
|
|
405
|
-
}, U = async (e, r) => e === h.INIT ? z(r) : e === h.COMPILE ? X(r) : (n.error(`无效的命令: ${e}`), process.exit(1)), {
|
|
406
|
-
version: ee,
|
|
407
|
-
description: te,
|
|
408
|
-
cliConfig: { moduleName: N }
|
|
409
|
-
} = F, ne = () => ({
|
|
410
|
-
configPath: {
|
|
411
|
-
type: "string",
|
|
412
|
-
alias: "c",
|
|
413
|
-
describe: "配置文件路径",
|
|
414
|
-
default: v.join(process.cwd(), B)
|
|
415
|
-
},
|
|
416
|
-
rootDir: {
|
|
417
|
-
type: "string",
|
|
418
|
-
alias: "r",
|
|
419
|
-
describe: "编译阶段相对目录的根目录",
|
|
420
|
-
default: process.cwd()
|
|
421
|
-
}
|
|
422
|
-
}), ae = {
|
|
423
|
-
command: h.INIT,
|
|
424
|
-
describe: "初始化模板配置文件",
|
|
425
|
-
options: ne(),
|
|
426
|
-
handler: T(U)(
|
|
427
|
-
h.INIT
|
|
428
|
-
)
|
|
429
|
-
}, re = () => ({
|
|
430
|
-
env: {
|
|
431
|
-
alias: "e",
|
|
432
|
-
describe: "环境数据文件JSON文件相对路径(优先级高于envData)",
|
|
433
|
-
type: "string"
|
|
434
|
-
},
|
|
435
|
-
envData: {
|
|
436
|
-
alias: "E",
|
|
437
|
-
describe: "环境变量数据(JSON字符串)",
|
|
438
|
-
type: "string"
|
|
439
|
-
},
|
|
440
|
-
input: {
|
|
441
|
-
alias: "i",
|
|
442
|
-
describe: "模板文件相对路径(优先级高于inputTemplate)",
|
|
443
|
-
type: "string"
|
|
444
|
-
},
|
|
445
|
-
inputData: {
|
|
446
|
-
alias: "I",
|
|
447
|
-
describe: "模板数据",
|
|
448
|
-
type: "string"
|
|
449
|
-
},
|
|
450
|
-
mode: {
|
|
451
|
-
alias: "m",
|
|
452
|
-
describe: "输出模式",
|
|
453
|
-
type: "string",
|
|
454
|
-
choices: [
|
|
455
|
-
g.OVERWRITE,
|
|
456
|
-
g.APPEND,
|
|
457
|
-
g.REPLACE,
|
|
458
|
-
g.RETURN
|
|
459
|
-
],
|
|
460
|
-
default: C.mode
|
|
461
|
-
},
|
|
462
|
-
output: {
|
|
463
|
-
alias: "o",
|
|
464
|
-
describe: "输出文件路径",
|
|
465
|
-
type: "string"
|
|
466
|
-
},
|
|
467
|
-
rollback: {
|
|
468
|
-
alias: "r",
|
|
469
|
-
describe: "是否回滚",
|
|
470
|
-
type: "boolean",
|
|
471
|
-
default: C.rollback
|
|
472
|
-
},
|
|
473
|
-
dealMarkdown: {
|
|
474
|
-
alias: "d",
|
|
475
|
-
describe: "(检测是markdown)是否处理(单个)代码块包裹",
|
|
476
|
-
type: "boolean",
|
|
477
|
-
default: C.dealMarkdown
|
|
478
|
-
},
|
|
479
|
-
batch: {
|
|
480
|
-
alias: "b",
|
|
481
|
-
describe: "是否批量处理",
|
|
482
|
-
type: "boolean",
|
|
483
|
-
default: C.batch
|
|
484
|
-
}
|
|
485
|
-
}), se = {
|
|
486
|
-
command: h.COMPILE,
|
|
487
|
-
describe: "编译模板",
|
|
488
|
-
options: re(),
|
|
489
|
-
handler: T(U)(
|
|
490
|
-
h.COMPILE
|
|
491
|
-
)
|
|
492
|
-
}, M = {
|
|
493
|
-
describe: te,
|
|
494
|
-
version: ee,
|
|
495
|
-
subcommands: [ae, se].map(
|
|
496
|
-
(e) => L(e)
|
|
497
|
-
),
|
|
498
|
-
demandCommandCount: 1
|
|
499
|
-
}, V = (e = !1) => {
|
|
500
|
-
const r = e ? N : void 0, c = `$0${e ? ` ${N}` : ""} <command> [options]`;
|
|
501
|
-
return { command: r, usage: c };
|
|
502
|
-
}, de = async () => W({
|
|
503
|
-
...M,
|
|
504
|
-
...V()
|
|
505
|
-
}), fe = () => L({
|
|
506
|
-
...M,
|
|
507
|
-
...V(!0)
|
|
508
|
-
});
|
|
509
|
-
export {
|
|
510
|
-
R as E,
|
|
511
|
-
g as O,
|
|
512
|
-
h as S,
|
|
513
|
-
K as T,
|
|
514
|
-
X as a,
|
|
515
|
-
Q as b,
|
|
516
|
-
de as c,
|
|
517
|
-
fe as d,
|
|
518
|
-
G as g,
|
|
519
|
-
U as h,
|
|
520
|
-
z as i
|
|
521
|
-
};
|
package/types/utils/const.d.ts
DELETED
|
File without changes
|