@done-coding/cli-template 0.5.1 → 0.6.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 -1
- package/es/{index-dde3ea37.js → index-07bb6534.js} +93 -90
- package/es/index.mjs +3 -2
- package/package.json +3 -2
- package/types/index.d.ts +1 -0
- package/types/injectInfo.json.d.ts +1 -1
package/es/cli.mjs
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { c as r } from "./index-
|
|
2
|
+
import { c as r } from "./index-07bb6534.js";
|
|
3
3
|
import "node:path";
|
|
4
4
|
import "node:fs";
|
|
5
5
|
import "chalk";
|
|
6
6
|
import "lodash.template";
|
|
7
7
|
import "lodash.assign";
|
|
8
8
|
import "prompts";
|
|
9
|
+
import "@done-coding/node-tools";
|
|
9
10
|
import "yargs";
|
|
10
11
|
import "yargs/helpers";
|
|
11
12
|
r();
|
|
@@ -1,57 +1,58 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import v from "node:path";
|
|
3
|
-
import
|
|
3
|
+
import c from "node:fs";
|
|
4
4
|
import o from "chalk";
|
|
5
|
-
import
|
|
6
|
-
import
|
|
5
|
+
import E from "lodash.template";
|
|
6
|
+
import A from "lodash.assign";
|
|
7
7
|
import N from "prompts";
|
|
8
|
-
import
|
|
9
|
-
import
|
|
8
|
+
import { onPromptFormStateForSigint as P } from "@done-coding/node-tools";
|
|
9
|
+
import U from "yargs";
|
|
10
|
+
import { hideBin as W } from "yargs/helpers";
|
|
10
11
|
var g = /* @__PURE__ */ ((e) => (e.OVERWRITE = "overwrite", e.APPEND = "append", e.REPLACE = "replace", e.RETURN = "return", e))(g || {});
|
|
11
12
|
const $ = {
|
|
12
13
|
rollback: !1,
|
|
13
14
|
dealMarkdown: !1,
|
|
14
15
|
mode: g.OVERWRITE,
|
|
15
16
|
batch: !1
|
|
16
|
-
},
|
|
17
|
-
const { rollback: n, dealMarkdown: a, mode: s, batch: u, ...
|
|
17
|
+
}, C = (e) => {
|
|
18
|
+
const { rollback: n, dealMarkdown: a, mode: s, batch: u, ...i } = e;
|
|
18
19
|
return {
|
|
19
20
|
rollback: n ?? $.rollback,
|
|
20
21
|
dealMarkdown: a ?? $.dealMarkdown,
|
|
21
22
|
mode: s ?? $.mode,
|
|
22
23
|
batch: u ?? $.batch,
|
|
23
|
-
...
|
|
24
|
+
...i
|
|
24
25
|
};
|
|
25
|
-
},
|
|
26
|
-
version: "0.
|
|
26
|
+
}, S = {
|
|
27
|
+
version: "0.6.0",
|
|
27
28
|
name: "@done-coding/cli-template",
|
|
28
29
|
description: "预编译命令行工具",
|
|
29
30
|
cliConfig: {
|
|
30
31
|
namespaceDir: ".done-coding",
|
|
31
32
|
moduleName: "template"
|
|
32
33
|
}
|
|
33
|
-
},
|
|
34
|
-
const { namespaceDir: n, moduleName: a } =
|
|
34
|
+
}, J = (e) => {
|
|
35
|
+
const { namespaceDir: n, moduleName: a } = S.cliConfig, s = [n, `${a}.json`];
|
|
35
36
|
e && s.unshift(e);
|
|
36
37
|
const u = v.resolve(...s);
|
|
37
|
-
if (
|
|
38
|
+
if (c.existsSync(u))
|
|
38
39
|
return u;
|
|
39
|
-
},
|
|
40
|
+
}, I = ({
|
|
40
41
|
filePath: e,
|
|
41
42
|
dataInit: n,
|
|
42
43
|
limitJson: a,
|
|
43
44
|
filePathKey: s,
|
|
44
45
|
dataInitKey: u,
|
|
45
|
-
dealMarkdown:
|
|
46
|
+
dealMarkdown: i = !1
|
|
46
47
|
}) => {
|
|
47
48
|
if (e) {
|
|
48
49
|
if (a && !e.endsWith(".json"))
|
|
49
50
|
return console.log(
|
|
50
51
|
o.red(`${s}必须是json文件,请检查文件后缀名`)
|
|
51
52
|
), process.exit(1);
|
|
52
|
-
const r =
|
|
53
|
+
const r = c.readFileSync(v.resolve(e), "utf-8");
|
|
53
54
|
let p = r;
|
|
54
|
-
return
|
|
55
|
+
return i && e.endsWith(".md") && (p = r.replace(
|
|
55
56
|
/^\s*```[a-zA-Z0-9]+\s*[\r\n]+([\s\S]+?)```\s*$/,
|
|
56
57
|
"$1"
|
|
57
58
|
)), a ? JSON.parse(p) : p;
|
|
@@ -59,23 +60,23 @@ const $ = {
|
|
|
59
60
|
return n ? (console.log(
|
|
60
61
|
o.green(`${s} 为空,将使用${u}作为数据`)
|
|
61
62
|
), a ? JSON.parse(n) : n) : (console.log(o.red(`${s}与${u}不能同时为空`)), process.exit(1));
|
|
62
|
-
},
|
|
63
|
+
}, D = (e, n) => {
|
|
63
64
|
if (!n)
|
|
64
65
|
return console.log(o.red(`${e}模式下output不能为空`)), process.exit(1);
|
|
65
|
-
},
|
|
66
|
+
}, R = (e, n) => {
|
|
66
67
|
if (n && e === n)
|
|
67
68
|
return console.log(o.red("output与input不能相同")), process.exit(1);
|
|
68
|
-
},
|
|
69
|
+
}, M = (e, n) => {
|
|
69
70
|
if (!n)
|
|
70
71
|
return console.log(o.red(`${e}模式下input不能为空`)), process.exit(1);
|
|
71
|
-
},
|
|
72
|
+
}, x = async (e, {
|
|
72
73
|
rollbackDelFileAgree: n = !1
|
|
73
74
|
} = {}) => {
|
|
74
75
|
const {
|
|
75
76
|
env: a,
|
|
76
77
|
input: s,
|
|
77
78
|
inputData: u,
|
|
78
|
-
output:
|
|
79
|
+
output: i,
|
|
79
80
|
mode: r,
|
|
80
81
|
rollback: p,
|
|
81
82
|
dealMarkdown: y,
|
|
@@ -95,26 +96,27 @@ mode: ${r}
|
|
|
95
96
|
rollback: ${p}
|
|
96
97
|
`)
|
|
97
98
|
);
|
|
98
|
-
const k =
|
|
99
|
+
const k = I({
|
|
99
100
|
filePath: s,
|
|
100
101
|
dataInit: u,
|
|
101
102
|
limitJson: !1,
|
|
102
103
|
filePathKey: "input",
|
|
103
104
|
dataInitKey: "inputData",
|
|
104
105
|
dealMarkdown: y
|
|
105
|
-
}), l =
|
|
106
|
+
}), l = E(k), m = typeof h == "function" ? h() : h, f = l(m);
|
|
106
107
|
switch (r) {
|
|
107
108
|
case g.OVERWRITE: {
|
|
108
|
-
|
|
109
|
-
const t = v.resolve(
|
|
110
|
-
if (
|
|
109
|
+
D(r, i), R(i, s);
|
|
110
|
+
const t = v.resolve(i);
|
|
111
|
+
if (c.mkdirSync(v.dirname(t), { recursive: !0 }), c.existsSync(t)) {
|
|
111
112
|
if (p)
|
|
112
113
|
if (n || (await N({
|
|
113
114
|
type: "confirm",
|
|
114
115
|
name: "remove",
|
|
115
|
-
message: `${r}模式下回滚将删除${t}
|
|
116
|
+
message: `${r}模式下回滚将删除${t},是否继续?`,
|
|
117
|
+
onState: P
|
|
116
118
|
})).remove) {
|
|
117
|
-
|
|
119
|
+
c.rmSync(t, { force: !0 }), console.log(o.green(`${r}模式下${t}已删除`));
|
|
118
120
|
return;
|
|
119
121
|
} else {
|
|
120
122
|
console.log(o.yellow(`${r}模式下${t}回滚取消`));
|
|
@@ -130,24 +132,24 @@ rollback: ${p}
|
|
|
130
132
|
}
|
|
131
133
|
console.log(o.blue(`output:${t} 不存在,将创建`));
|
|
132
134
|
}
|
|
133
|
-
|
|
135
|
+
c.writeFileSync(t, f, "utf-8"), console.log(o.green(`模板处理完成,输出到 ${t}`));
|
|
134
136
|
break;
|
|
135
137
|
}
|
|
136
138
|
case g.APPEND: {
|
|
137
|
-
|
|
138
|
-
const t = v.resolve(
|
|
139
|
-
if (
|
|
140
|
-
const d =
|
|
139
|
+
D(r, i), R(i, s);
|
|
140
|
+
const t = v.resolve(i);
|
|
141
|
+
if (c.mkdirSync(v.dirname(t), { recursive: !0 }), c.existsSync(t)) {
|
|
142
|
+
const d = c.readFileSync(t, "utf-8");
|
|
141
143
|
if (p) {
|
|
142
|
-
|
|
144
|
+
c.writeFileSync(
|
|
143
145
|
t,
|
|
144
|
-
d.replace(
|
|
146
|
+
d.replace(f, ""),
|
|
145
147
|
"utf-8"
|
|
146
148
|
), console.log(o.green(`${r}模式下${t}回滚完成`));
|
|
147
149
|
return;
|
|
148
150
|
}
|
|
149
|
-
const b = d +
|
|
150
|
-
|
|
151
|
+
const b = d + f;
|
|
152
|
+
c.writeFileSync(t, b, "utf-8"), console.log(o.green(`模板处理完成,追加到 ${t}`));
|
|
151
153
|
} else {
|
|
152
154
|
if (p) {
|
|
153
155
|
console.log(
|
|
@@ -155,31 +157,31 @@ rollback: ${p}
|
|
|
155
157
|
);
|
|
156
158
|
return;
|
|
157
159
|
}
|
|
158
|
-
console.log(o.blue(`output:${t} 不存在,将创建`)),
|
|
160
|
+
console.log(o.blue(`output:${t} 不存在,将创建`)), c.writeFileSync(t, f, "utf-8"), console.log(o.green(`模板处理完成,输出到 ${t}`));
|
|
159
161
|
}
|
|
160
162
|
break;
|
|
161
163
|
}
|
|
162
164
|
case g.REPLACE: {
|
|
163
|
-
if (
|
|
165
|
+
if (i && console.log(o.yellow(`output ${i} 将被忽略`)), M(r, s), a && a === s)
|
|
164
166
|
return console.log(o.red("env 与 input 不能相同")), process.exit(1);
|
|
165
167
|
const t = v.resolve(s);
|
|
166
168
|
let d = t;
|
|
167
|
-
const b = s.replace(`${process.cwd()}/`, ""), w =
|
|
169
|
+
const b = s.replace(`${process.cwd()}/`, ""), w = E(b)(m);
|
|
168
170
|
w !== b && (console.log(
|
|
169
171
|
o.green(`检测输入文件名也需要替换
|
|
170
172
|
./${b} => ./${w} `)
|
|
171
|
-
),
|
|
173
|
+
), c.rmSync(t), d = v.resolve(`./${w}`)), c.mkdirSync(v.dirname(d), { recursive: !0 }), c.writeFileSync(d, f, "utf-8"), console.log(o.green(`模板处理完成,输出到 ${d}`));
|
|
172
174
|
break;
|
|
173
175
|
}
|
|
174
176
|
case g.RETURN:
|
|
175
177
|
return console.log(
|
|
176
178
|
o.green("模板处理完成,返回结果(函数调用才会拿到返回值)")
|
|
177
|
-
),
|
|
179
|
+
), f;
|
|
178
180
|
default:
|
|
179
181
|
return console.log(o.red(`mode ${r} 不支持`)), process.exit(1);
|
|
180
182
|
}
|
|
181
|
-
return
|
|
182
|
-
},
|
|
183
|
+
return f;
|
|
184
|
+
}, j = async ({
|
|
183
185
|
rootDir: e = process.cwd(),
|
|
184
186
|
itemDefaultRollback: n = !1,
|
|
185
187
|
extraEnvData: a = {}
|
|
@@ -188,77 +190,78 @@ rollback: ${p}
|
|
|
188
190
|
if (s)
|
|
189
191
|
u = s;
|
|
190
192
|
else {
|
|
191
|
-
const l =
|
|
193
|
+
const l = J(e);
|
|
192
194
|
if (!l)
|
|
193
195
|
return console.log(o.red(`配置文件${l}不存在`)), process.exit(1);
|
|
194
|
-
const
|
|
195
|
-
u = JSON.parse(
|
|
196
|
+
const m = c.readFileSync(l, "utf-8");
|
|
197
|
+
u = JSON.parse(m);
|
|
196
198
|
}
|
|
197
199
|
const {
|
|
198
|
-
list:
|
|
200
|
+
list: i,
|
|
199
201
|
globalEnvData: r = {},
|
|
200
202
|
collectEnvDataForm: p = []
|
|
201
203
|
} = u, y = {};
|
|
202
204
|
for (const l of p) {
|
|
203
|
-
let
|
|
204
|
-
typeof l == "string" ? (
|
|
205
|
+
let m, f, t;
|
|
206
|
+
typeof l == "string" ? (m = l, f = l, t = void 0) : (m = l.key, f = l.label, t = l.initial), y[m] = (await N({
|
|
205
207
|
type: "text",
|
|
206
|
-
name:
|
|
207
|
-
message: `请输入${
|
|
208
|
+
name: m,
|
|
209
|
+
message: `请输入${f}`,
|
|
208
210
|
initial: t,
|
|
209
|
-
format: (d) =>
|
|
210
|
-
validate: (d) => d
|
|
211
|
-
|
|
211
|
+
format: (d) => d.trim(),
|
|
212
|
+
validate: (d) => d.length > 0 || `${f}不能为空`,
|
|
213
|
+
onState: P
|
|
214
|
+
}))[m];
|
|
212
215
|
}
|
|
213
|
-
const h =
|
|
214
|
-
const { rollback:
|
|
215
|
-
envData:
|
|
216
|
+
const h = i.map((l) => {
|
|
217
|
+
const { rollback: m = n } = l, {
|
|
218
|
+
envData: f,
|
|
216
219
|
env: t,
|
|
217
220
|
input: d,
|
|
218
221
|
output: b,
|
|
219
222
|
...w
|
|
220
|
-
} =
|
|
223
|
+
} = C(l);
|
|
221
224
|
return t && console.log(o.yellow(`批量处理中 env:${t} 将被忽略,只读envData`)), {
|
|
222
225
|
...w,
|
|
223
226
|
env: t,
|
|
224
227
|
input: e && d ? v.resolve(e, d) : d,
|
|
225
228
|
output: e && b ? v.resolve(e, b) : b,
|
|
226
|
-
envData:
|
|
229
|
+
envData: A(
|
|
227
230
|
{},
|
|
228
231
|
a,
|
|
229
232
|
r,
|
|
230
233
|
y,
|
|
231
|
-
|
|
234
|
+
f
|
|
232
235
|
),
|
|
233
|
-
rollback:
|
|
236
|
+
rollback: m
|
|
234
237
|
};
|
|
235
238
|
}), k = [];
|
|
236
239
|
for (const l of h) {
|
|
237
|
-
const
|
|
240
|
+
const m = await x(l, {
|
|
238
241
|
rollbackDelFileAgree: !0
|
|
239
242
|
});
|
|
240
|
-
k.push(
|
|
243
|
+
k.push(m);
|
|
241
244
|
}
|
|
242
245
|
return k;
|
|
243
|
-
},
|
|
246
|
+
}, F = async (e) => {
|
|
244
247
|
const {
|
|
245
248
|
envData: n,
|
|
246
249
|
env: a,
|
|
247
250
|
input: s,
|
|
248
251
|
inputData: u,
|
|
249
|
-
output:
|
|
252
|
+
output: i,
|
|
250
253
|
mode: r,
|
|
251
254
|
rollback: p,
|
|
252
255
|
dealMarkdown: y,
|
|
253
256
|
batch: h
|
|
254
|
-
} =
|
|
257
|
+
} = C(e);
|
|
255
258
|
if (h)
|
|
256
|
-
return console.log(o.blue("开始批量处理")),
|
|
259
|
+
return console.log(o.blue("开始批量处理")), j({
|
|
257
260
|
// 回滚默认值 基于全局
|
|
258
261
|
itemDefaultRollback: p
|
|
259
262
|
});
|
|
260
263
|
console.log(o.blue("开始单个处理"));
|
|
261
|
-
const k =
|
|
264
|
+
const k = I({
|
|
262
265
|
filePath: a,
|
|
263
266
|
dataInit: n,
|
|
264
267
|
limitJson: !0,
|
|
@@ -266,16 +269,16 @@ rollback: ${p}
|
|
|
266
269
|
dataInitKey: "envData",
|
|
267
270
|
dealMarkdown: y
|
|
268
271
|
});
|
|
269
|
-
return
|
|
272
|
+
return x({
|
|
270
273
|
input: s,
|
|
271
274
|
inputData: u,
|
|
272
|
-
output:
|
|
275
|
+
output: i,
|
|
273
276
|
mode: r,
|
|
274
277
|
rollback: p,
|
|
275
278
|
dealMarkdown: y,
|
|
276
279
|
envData: k
|
|
277
280
|
});
|
|
278
|
-
},
|
|
281
|
+
}, L = () => ({
|
|
279
282
|
env: {
|
|
280
283
|
alias: "e",
|
|
281
284
|
describe: "环境数据文件JSON文件相对路径(优先级高于envData)",
|
|
@@ -331,25 +334,25 @@ rollback: ${p}
|
|
|
331
334
|
type: "boolean",
|
|
332
335
|
default: $.batch
|
|
333
336
|
}
|
|
334
|
-
}),
|
|
337
|
+
}), O = S.cliConfig.moduleName, V = (e, n) => {
|
|
335
338
|
console.log(e ? o.red(e) : o.red(n.message)), process.exit(1);
|
|
336
|
-
},
|
|
337
|
-
const a =
|
|
338
|
-
return e.strict().usage(n).help("help").version(
|
|
339
|
-
},
|
|
340
|
-
command:
|
|
341
|
-
describe:
|
|
342
|
-
builder:
|
|
343
|
-
handler:
|
|
344
|
-
},
|
|
345
|
-
const e =
|
|
346
|
-
return
|
|
339
|
+
}, H = S.description, _ = `Usage: $0 ${O} [options]`, q = "Usage: $0 [options]", T = (e, n) => {
|
|
340
|
+
const a = L();
|
|
341
|
+
return e.strict().usage(n).help("help").version(S.version).alias("v", "version").alias("h", "help").options(a).fail(V).argv;
|
|
342
|
+
}, z = (e) => T(e, _), oe = {
|
|
343
|
+
command: O,
|
|
344
|
+
describe: H,
|
|
345
|
+
builder: z,
|
|
346
|
+
handler: F
|
|
347
|
+
}, ne = async () => {
|
|
348
|
+
const e = U(W(process.argv)), n = await T(e, q);
|
|
349
|
+
return F(n);
|
|
347
350
|
};
|
|
348
351
|
export {
|
|
349
352
|
g as O,
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
353
|
+
oe as a,
|
|
354
|
+
j as b,
|
|
355
|
+
ne as c,
|
|
356
|
+
J as g,
|
|
357
|
+
F as h
|
|
355
358
|
};
|
package/es/index.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { O as d, b as h, a as u, g as b, h as
|
|
2
|
+
import { O as d, b as h, a as u, g as b, h as f } from "./index-07bb6534.js";
|
|
3
|
+
export * from "@done-coding/node-tools";
|
|
3
4
|
import "node:path";
|
|
4
5
|
import "node:fs";
|
|
5
6
|
import "chalk";
|
|
@@ -13,5 +14,5 @@ export {
|
|
|
13
14
|
h as batchHandler,
|
|
14
15
|
u as command,
|
|
15
16
|
b as getConfigPath,
|
|
16
|
-
|
|
17
|
+
f as handler
|
|
17
18
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@done-coding/cli-template",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "预编译命令行工具",
|
|
5
5
|
"private": false,
|
|
6
6
|
"module": "es/index.mjs",
|
|
@@ -58,6 +58,7 @@
|
|
|
58
58
|
"node": ">=18.0.0"
|
|
59
59
|
},
|
|
60
60
|
"dependencies": {
|
|
61
|
+
"@done-coding/node-tools": "^0.2.0",
|
|
61
62
|
"chalk": "^5.3.0",
|
|
62
63
|
"lodash.assign": "^4.2.0",
|
|
63
64
|
"lodash.get": "^4.4.2",
|
|
@@ -66,5 +67,5 @@
|
|
|
66
67
|
"prompts": "^2.4.2",
|
|
67
68
|
"yargs": "^17.7.2"
|
|
68
69
|
},
|
|
69
|
-
"gitHead": "
|
|
70
|
+
"gitHead": "0dde53af70143475f5149714152d714802f846f8"
|
|
70
71
|
}
|
package/types/index.d.ts
CHANGED