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