@done-coding/cli-template 0.6.1-alpha.0 → 0.7.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 CHANGED
@@ -1,12 +1,8 @@
1
1
  #!/usr/bin/env node
2
- import { c as r } from "./index-9892305e.js";
2
+ import { c as m } from "./index-6bc0fa40.js";
3
3
  import "node:path";
4
4
  import "node:fs";
5
- import "chalk";
6
5
  import "lodash.template";
7
6
  import "lodash.assign";
8
- import "prompts";
9
7
  import "@done-coding/cli-utils";
10
- import "yargs";
11
- import "yargs/helpers";
12
- r();
8
+ m();
@@ -0,0 +1,343 @@
1
+ #!/usr/bin/env node
2
+ import $ from "node:path";
3
+ import l from "node:fs";
4
+ import E from "lodash.template";
5
+ import A from "lodash.assign";
6
+ import { log as n, xPrompts as N, createMainCommand as T, createSubcommand as M } from "@done-coding/cli-utils";
7
+ var v = /* @__PURE__ */ ((e) => (e.OVERWRITE = "overwrite", e.APPEND = "append", e.REPLACE = "replace", e.RETURN = "return", e))(v || {});
8
+ const y = {
9
+ rollback: !1,
10
+ dealMarkdown: !1,
11
+ mode: v.OVERWRITE,
12
+ batch: !1
13
+ }, P = (e) => {
14
+ const { rollback: r, dealMarkdown: i, mode: a, batch: u, ...c } = e;
15
+ return {
16
+ rollback: r ?? y.rollback,
17
+ dealMarkdown: i ?? y.dealMarkdown,
18
+ mode: a ?? y.mode,
19
+ batch: u ?? y.batch,
20
+ ...c
21
+ };
22
+ }, C = {
23
+ version: "0.7.0-alpha.0",
24
+ name: "@done-coding/cli-template",
25
+ description: "预编译命令行工具",
26
+ cliConfig: {
27
+ namespaceDir: ".done-coding",
28
+ moduleName: "template"
29
+ }
30
+ }, W = (e) => {
31
+ const { namespaceDir: r, moduleName: i } = C.cliConfig, a = [r, `${i}.json`];
32
+ e && a.unshift(e);
33
+ const u = $.resolve(...a);
34
+ if (l.existsSync(u))
35
+ return u;
36
+ }, I = ({
37
+ filePath: e,
38
+ dataInit: r,
39
+ limitJson: i,
40
+ filePathKey: a,
41
+ dataInitKey: u,
42
+ dealMarkdown: c = !1
43
+ }) => {
44
+ if (e) {
45
+ if (i && !e.endsWith(".json"))
46
+ return n.error(`${a}必须是json文件,请检查文件后缀名`), process.exit(1);
47
+ const s = l.readFileSync($.resolve(e), "utf-8");
48
+ let p = s;
49
+ return c && e.endsWith(".md") && (p = s.replace(
50
+ /^\s*```[a-zA-Z0-9]+\s*[\r\n]+([\s\S]+?)```\s*$/,
51
+ "$1"
52
+ )), i ? JSON.parse(p) : p;
53
+ } else
54
+ return r ? (n.info(`${a} 为空,将使用${u}作为数据`), i ? JSON.parse(r) : r) : (n.error(`${a}与${u}不能同时为空`), process.exit(1));
55
+ }, D = (e, r) => {
56
+ if (!r)
57
+ return n.error(`${e}模式下output不能为空`), process.exit(1);
58
+ }, S = (e, r) => {
59
+ if (r && e === r)
60
+ return n.error("output与input不能相同"), process.exit(1);
61
+ }, J = (e, r) => {
62
+ if (!r)
63
+ return n.error(`${e}模式下input不能为空`), process.exit(1);
64
+ }, x = async (e, {
65
+ rollbackDelFileAgree: r = !1
66
+ } = {}) => {
67
+ const {
68
+ env: i,
69
+ input: a,
70
+ inputData: u,
71
+ output: c,
72
+ mode: s,
73
+ rollback: p,
74
+ dealMarkdown: g,
75
+ envData: h
76
+ } = e;
77
+ if (p)
78
+ switch (s) {
79
+ case v.REPLACE:
80
+ case v.RETURN: {
81
+ n.error(`${s}模式不支持回滚`);
82
+ return;
83
+ }
84
+ }
85
+ n.stage(`开始处理模板
86
+ mode: ${s}
87
+ rollback: ${p}
88
+ `);
89
+ const k = I({
90
+ filePath: a,
91
+ dataInit: u,
92
+ limitJson: !1,
93
+ filePathKey: "input",
94
+ dataInitKey: "inputData",
95
+ dealMarkdown: g
96
+ }), o = E(k), f = typeof h == "function" ? h() : h, m = o(f);
97
+ switch (s) {
98
+ case v.OVERWRITE: {
99
+ D(s, c), S(c, a);
100
+ const t = $.resolve(c);
101
+ if (l.mkdirSync($.dirname(t), { recursive: !0 }), l.existsSync(t)) {
102
+ if (p)
103
+ if (r || (await N({
104
+ type: "confirm",
105
+ name: "remove",
106
+ message: `${s}模式下回滚将删除${t},是否继续?`
107
+ })).remove) {
108
+ l.rmSync(t, { force: !0 }), n.success(`${s}模式下${t}已删除`);
109
+ return;
110
+ } else {
111
+ n.warn(`${s}模式下${t}回滚取消`);
112
+ return;
113
+ }
114
+ n.info(`output:${t} 已存在,将覆盖`);
115
+ } else {
116
+ if (p) {
117
+ n.warn(`${s}模式下${t}不存在,无需回滚`);
118
+ return;
119
+ }
120
+ n.stage(`output:${t} 不存在,将创建`);
121
+ }
122
+ l.writeFileSync(t, m, "utf-8"), n.success(`模板处理完成,输出到 ${t}`);
123
+ break;
124
+ }
125
+ case v.APPEND: {
126
+ D(s, c), S(c, a);
127
+ const t = $.resolve(c);
128
+ if (l.mkdirSync($.dirname(t), { recursive: !0 }), l.existsSync(t)) {
129
+ const d = l.readFileSync(t, "utf-8");
130
+ if (p) {
131
+ l.writeFileSync(
132
+ t,
133
+ d.replace(m, ""),
134
+ "utf-8"
135
+ ), n.success(`${s}模式下${t}回滚完成`);
136
+ return;
137
+ }
138
+ const b = d + m;
139
+ l.writeFileSync(t, b, "utf-8"), n.success(`模板处理完成,追加到 ${t}`);
140
+ } else {
141
+ if (p) {
142
+ n.warn(`${s}模式下${t}不存在,无需回滚`);
143
+ return;
144
+ }
145
+ n.stage(`output:${t} 不存在,将创建`), l.writeFileSync(t, m, "utf-8"), n.success(`模板处理完成,输出到 ${t}`);
146
+ }
147
+ break;
148
+ }
149
+ case v.REPLACE: {
150
+ if (c && n.warn(`output ${c} 将被忽略`), J(s, a), i && i === a)
151
+ return n.error("env 与 input 不能相同"), process.exit(1);
152
+ const t = $.resolve(a);
153
+ let d = t;
154
+ const b = a.replace(`${process.cwd()}/`, ""), w = E(b)(f);
155
+ w !== b && (n.success(`检测输入文件名也需要替换
156
+ ./${b} => ./${w} `), l.rmSync(t), d = $.resolve(`./${w}`)), l.mkdirSync($.dirname(d), { recursive: !0 }), l.writeFileSync(d, m, "utf-8"), n.success(`模板处理完成,输出到 ${d}`);
157
+ break;
158
+ }
159
+ case v.RETURN:
160
+ return n.success("模板处理完成,返回结果(函数调用才会拿到返回值)"), m;
161
+ default:
162
+ return n.error(`mode ${s} 不支持`), process.exit(1);
163
+ }
164
+ return m;
165
+ }, U = async ({
166
+ rootDir: e = process.cwd(),
167
+ itemDefaultRollback: r = !1,
168
+ extraEnvData: i = {}
169
+ } = {}, a) => {
170
+ let u;
171
+ if (a)
172
+ u = a;
173
+ else {
174
+ const o = W(e);
175
+ if (!o)
176
+ return n.error(`配置文件${o}不存在`), process.exit(1);
177
+ const f = l.readFileSync(o, "utf-8");
178
+ u = JSON.parse(f);
179
+ }
180
+ const {
181
+ list: c,
182
+ globalEnvData: s = {},
183
+ collectEnvDataForm: p = []
184
+ } = u, g = {};
185
+ for (const o of p) {
186
+ let f, m, t;
187
+ typeof o == "string" ? (f = o, m = o, t = void 0) : (f = o.key, m = o.label, t = o.initial), g[f] = (await N({
188
+ type: "text",
189
+ name: f,
190
+ message: `请输入${m}`,
191
+ initial: t,
192
+ format: (d) => d.trim(),
193
+ validate: (d) => d.length > 0 || `${m}不能为空`
194
+ }))[f];
195
+ }
196
+ const h = c.map((o) => {
197
+ const { rollback: f = r } = o, {
198
+ envData: m,
199
+ env: t,
200
+ input: d,
201
+ output: b,
202
+ ...w
203
+ } = P(o);
204
+ return t && n.warn(`批量处理中 env:${t} 将被忽略,只读envData`), {
205
+ ...w,
206
+ env: t,
207
+ input: e && d ? $.resolve(e, d) : d,
208
+ output: e && b ? $.resolve(e, b) : b,
209
+ envData: A(
210
+ {},
211
+ i,
212
+ s,
213
+ g,
214
+ m
215
+ ),
216
+ rollback: f
217
+ };
218
+ }), k = [];
219
+ for (const o of h) {
220
+ const f = await x(o, {
221
+ rollbackDelFileAgree: !0
222
+ });
223
+ k.push(f);
224
+ }
225
+ return k;
226
+ }, j = async (e) => {
227
+ const {
228
+ envData: r,
229
+ env: i,
230
+ input: a,
231
+ inputData: u,
232
+ output: c,
233
+ mode: s,
234
+ rollback: p,
235
+ dealMarkdown: g,
236
+ batch: h
237
+ } = P(e);
238
+ if (h)
239
+ return n.stage("开始批量处理"), U({
240
+ // 回滚默认值 基于全局
241
+ itemDefaultRollback: p
242
+ });
243
+ n.stage("开始单个处理");
244
+ const k = I({
245
+ filePath: i,
246
+ dataInit: r,
247
+ limitJson: !0,
248
+ filePathKey: "env",
249
+ dataInitKey: "envData",
250
+ dealMarkdown: g
251
+ });
252
+ return x({
253
+ input: a,
254
+ inputData: u,
255
+ output: c,
256
+ mode: s,
257
+ rollback: p,
258
+ dealMarkdown: g,
259
+ envData: k
260
+ });
261
+ }, {
262
+ version: L,
263
+ description: V,
264
+ cliConfig: { moduleName: R }
265
+ } = C, _ = () => ({
266
+ env: {
267
+ alias: "e",
268
+ describe: "环境数据文件JSON文件相对路径(优先级高于envData)",
269
+ type: "string"
270
+ },
271
+ envData: {
272
+ alias: "E",
273
+ describe: "环境变量数据(JSON字符串)",
274
+ type: "string"
275
+ },
276
+ input: {
277
+ alias: "i",
278
+ describe: "模板文件相对路径(优先级高于inputTemplate)",
279
+ type: "string"
280
+ },
281
+ inputData: {
282
+ alias: "I",
283
+ describe: "模板数据",
284
+ type: "string"
285
+ },
286
+ mode: {
287
+ alias: "m",
288
+ describe: "输出模式",
289
+ type: "string",
290
+ choices: [
291
+ v.OVERWRITE,
292
+ v.APPEND,
293
+ v.REPLACE,
294
+ v.RETURN
295
+ ],
296
+ default: y.mode
297
+ },
298
+ output: {
299
+ alias: "o",
300
+ describe: "输出文件路径",
301
+ type: "string"
302
+ },
303
+ rollback: {
304
+ alias: "r",
305
+ describe: "是否回滚",
306
+ type: "boolean",
307
+ default: y.rollback
308
+ },
309
+ dealMarkdown: {
310
+ alias: "d",
311
+ describe: "(检测是markdown)是否处理(单个)代码块包裹",
312
+ type: "boolean",
313
+ default: y.dealMarkdown
314
+ },
315
+ batch: {
316
+ alias: "b",
317
+ describe: "是否批量处理",
318
+ type: "boolean",
319
+ default: y.batch
320
+ }
321
+ }), F = {
322
+ describe: V,
323
+ version: L,
324
+ options: _(),
325
+ handler: j
326
+ }, O = (e = !1) => {
327
+ const r = e ? R : void 0, i = `$0${e ? ` ${R}` : ""} [options]`;
328
+ return { command: r, usage: i };
329
+ }, B = async () => T({
330
+ ...F,
331
+ ...O()
332
+ }), G = () => M({
333
+ ...F,
334
+ ...O(!0)
335
+ });
336
+ export {
337
+ v as O,
338
+ G as a,
339
+ U as b,
340
+ B as c,
341
+ W as g,
342
+ j as h
343
+ };
package/es/index.mjs CHANGED
@@ -1,18 +1,14 @@
1
1
  #!/usr/bin/env node
2
- import { O as d, b as h, a as u, g as b, h as f } from "./index-9892305e.js";
3
- export * from "@done-coding/cli-utils";
2
+ import { O as i, b as p, a as s, g as d, h as u } from "./index-6bc0fa40.js";
4
3
  import "node:path";
5
4
  import "node:fs";
6
- import "chalk";
7
5
  import "lodash.template";
8
6
  import "lodash.assign";
9
- import "prompts";
10
- import "yargs";
11
- import "yargs/helpers";
7
+ import "@done-coding/cli-utils";
12
8
  export {
13
- d as OutputModeEnum,
14
- h as batchHandler,
15
- u as command,
16
- b as getConfigPath,
17
- f as handler
9
+ i as OutputModeEnum,
10
+ p as batchHandler,
11
+ s as crateAsSubcommand,
12
+ d as getConfigPath,
13
+ u as handler
18
14
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@done-coding/cli-template",
3
- "version": "0.6.1-alpha.0",
3
+ "version": "0.7.0-alpha.0",
4
4
  "description": "预编译命令行工具",
5
5
  "private": false,
6
6
  "module": "es/index.mjs",
@@ -41,13 +41,12 @@
41
41
  "license": "MIT",
42
42
  "sideEffects": false,
43
43
  "devDependencies": {
44
- "@done-coding/cli-inject": "^0.4.6-alpha.0",
44
+ "@done-coding/cli-inject": "^0.5.0-alpha.0",
45
45
  "@types/lodash.assign": "^4.2.9",
46
46
  "@types/lodash.get": "^4.4.9",
47
47
  "@types/lodash.set": "^4.3.9",
48
48
  "@types/lodash.template": "^4.5.3",
49
49
  "@types/node": "^18.0.0",
50
- "@types/prompts": "^2.4.6",
51
50
  "@types/yargs": "^17.0.28",
52
51
  "rimraf": "^6.0.1",
53
52
  "typescript": "^5.2.2",
@@ -58,14 +57,11 @@
58
57
  "node": ">=18.0.0"
59
58
  },
60
59
  "dependencies": {
61
- "@done-coding/cli-utils": "^0.2.1-alpha.0",
62
- "chalk": "^5.3.0",
60
+ "@done-coding/cli-utils": "^0.3.0-alpha.0",
63
61
  "lodash.assign": "^4.2.0",
64
62
  "lodash.get": "^4.4.2",
65
63
  "lodash.set": "^4.3.2",
66
- "lodash.template": "^4.5.0",
67
- "prompts": "^2.4.2",
68
- "yargs": "^17.7.2"
64
+ "lodash.template": "^4.5.0"
69
65
  },
70
- "gitHead": "21eb698f64179882cfec979bd8f19920b047dacb"
66
+ "gitHead": "c5a54bdf08759425701a2024442a923883065189"
71
67
  }
@@ -1,6 +1,6 @@
1
1
  import type { CompileTemplateConfig } from './utils';
2
2
  import { type Options } from './utils';
3
- import type { ArgumentsCamelCase } from "yargs";
3
+ import type { CliHandlerArgv } from "@done-coding/cli-utils";
4
4
  export declare const batchHandler: ({ rootDir, itemDefaultRollback, extraEnvData, }?: {
5
5
  /** 根目录 */
6
6
  rootDir?: string | undefined;
@@ -9,4 +9,4 @@ export declare const batchHandler: ({ rootDir, itemDefaultRollback, extraEnvData
9
9
  /** 额外环境变量 */
10
10
  extraEnvData?: object | undefined;
11
11
  }, paramsConfig?: CompileTemplateConfig) => Promise<(string | undefined)[]>;
12
- export declare const handler: (argv: ArgumentsCamelCase<Options> | Options) => Promise<string | (string | undefined)[] | undefined>;
12
+ export declare const handler: (argv: CliHandlerArgv<Options>) => Promise<string | (string | undefined)[] | undefined>;
package/types/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  export { handler, batchHandler } from "./handler";
2
- export { command } from "./main";
2
+ export { crateAsSubcommand } from "./main";
3
3
  export * from './utils/types';
4
4
  export * from './utils/config';
5
- export * from "@done-coding/cli-utils";
@@ -1,5 +1,5 @@
1
1
  declare const _default: {
2
- "version": "0.6.1-alpha.0",
2
+ "version": "0.7.0-alpha.0",
3
3
  "name": "@done-coding/cli-template",
4
4
  "description": "预编译命令行工具",
5
5
  "cliConfig": {
package/types/main.d.ts CHANGED
@@ -1,4 +1,9 @@
1
- import type { CommandModule } from "yargs";
2
- import type { Options } from './utils';
3
- export declare const command: CommandModule<Options, Options>;
4
- export declare const createCli: () => Promise<string | (string | undefined)[] | undefined>;
1
+ /// <reference types="yargs" />
2
+ /** 作为主命令创建 */
3
+ export declare const createCommand: () => Promise<void | {
4
+ [x: string]: unknown;
5
+ _: (string | number)[];
6
+ $0: string;
7
+ }>;
8
+ /** 作为子命令创建 */
9
+ export declare const crateAsSubcommand: () => import("yargs").CommandModule<{}, {}>;
@@ -1,358 +0,0 @@
1
- #!/usr/bin/env node
2
- import v from "node:path";
3
- import c from "node:fs";
4
- import o from "chalk";
5
- import E from "lodash.template";
6
- import A from "lodash.assign";
7
- import N from "prompts";
8
- import { onPromptFormStateForSigint as P } from "@done-coding/cli-utils";
9
- import U from "yargs";
10
- import { hideBin as W } from "yargs/helpers";
11
- var g = /* @__PURE__ */ ((e) => (e.OVERWRITE = "overwrite", e.APPEND = "append", e.REPLACE = "replace", e.RETURN = "return", e))(g || {});
12
- const $ = {
13
- rollback: !1,
14
- dealMarkdown: !1,
15
- mode: g.OVERWRITE,
16
- batch: !1
17
- }, C = (e) => {
18
- const { rollback: n, dealMarkdown: a, mode: s, batch: u, ...i } = e;
19
- return {
20
- rollback: n ?? $.rollback,
21
- dealMarkdown: a ?? $.dealMarkdown,
22
- mode: s ?? $.mode,
23
- batch: u ?? $.batch,
24
- ...i
25
- };
26
- }, S = {
27
- version: "0.6.1-alpha.0",
28
- name: "@done-coding/cli-template",
29
- description: "预编译命令行工具",
30
- cliConfig: {
31
- namespaceDir: ".done-coding",
32
- moduleName: "template"
33
- }
34
- }, J = (e) => {
35
- const { namespaceDir: n, moduleName: a } = S.cliConfig, s = [n, `${a}.json`];
36
- e && s.unshift(e);
37
- const u = v.resolve(...s);
38
- if (c.existsSync(u))
39
- return u;
40
- }, I = ({
41
- filePath: e,
42
- dataInit: n,
43
- limitJson: a,
44
- filePathKey: s,
45
- dataInitKey: u,
46
- dealMarkdown: i = !1
47
- }) => {
48
- if (e) {
49
- if (a && !e.endsWith(".json"))
50
- return console.log(
51
- o.red(`${s}必须是json文件,请检查文件后缀名`)
52
- ), process.exit(1);
53
- const l = c.readFileSync(v.resolve(e), "utf-8");
54
- let p = l;
55
- return i && e.endsWith(".md") && (p = l.replace(
56
- /^\s*```[a-zA-Z0-9]+\s*[\r\n]+([\s\S]+?)```\s*$/,
57
- "$1"
58
- )), a ? JSON.parse(p) : p;
59
- } else
60
- return n ? (console.log(
61
- o.green(`${s} 为空,将使用${u}作为数据`)
62
- ), a ? JSON.parse(n) : n) : (console.log(o.red(`${s}与${u}不能同时为空`)), process.exit(1));
63
- }, D = (e, n) => {
64
- if (!n)
65
- return console.log(o.red(`${e}模式下output不能为空`)), process.exit(1);
66
- }, R = (e, n) => {
67
- if (n && e === n)
68
- return console.log(o.red("output与input不能相同")), process.exit(1);
69
- }, M = (e, n) => {
70
- if (!n)
71
- return console.log(o.red(`${e}模式下input不能为空`)), process.exit(1);
72
- }, x = async (e, {
73
- rollbackDelFileAgree: n = !1
74
- } = {}) => {
75
- const {
76
- env: a,
77
- input: s,
78
- inputData: u,
79
- output: i,
80
- mode: l,
81
- rollback: p,
82
- dealMarkdown: y,
83
- envData: h
84
- } = e;
85
- if (p)
86
- switch (l) {
87
- case g.REPLACE:
88
- case g.RETURN: {
89
- console.log(o.red(`${l}模式不支持回滚`));
90
- return;
91
- }
92
- }
93
- console.log(
94
- o.blue(`开始处理模板
95
- mode: ${l}
96
- rollback: ${p}
97
- `)
98
- );
99
- const k = I({
100
- filePath: s,
101
- dataInit: u,
102
- limitJson: !1,
103
- filePathKey: "input",
104
- dataInitKey: "inputData",
105
- dealMarkdown: y
106
- }), r = E(k), m = typeof h == "function" ? h() : h, f = r(m);
107
- switch (l) {
108
- case g.OVERWRITE: {
109
- D(l, i), R(i, s);
110
- const t = v.resolve(i);
111
- if (c.mkdirSync(v.dirname(t), { recursive: !0 }), c.existsSync(t)) {
112
- if (p)
113
- if (n || (await N({
114
- type: "confirm",
115
- name: "remove",
116
- message: `${l}模式下回滚将删除${t},是否继续?`,
117
- onState: P
118
- })).remove) {
119
- c.rmSync(t, { force: !0 }), console.log(o.green(`${l}模式下${t}已删除`));
120
- return;
121
- } else {
122
- console.log(o.yellow(`${l}模式下${t}回滚取消`));
123
- return;
124
- }
125
- console.log(o.blue(`output:${t} 已存在,将覆盖`));
126
- } else {
127
- if (p) {
128
- console.log(
129
- o.yellow(`${l}模式下${t}不存在,无需回滚`)
130
- );
131
- return;
132
- }
133
- console.log(o.blue(`output:${t} 不存在,将创建`));
134
- }
135
- c.writeFileSync(t, f, "utf-8"), console.log(o.green(`模板处理完成,输出到 ${t}`));
136
- break;
137
- }
138
- case g.APPEND: {
139
- D(l, 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");
143
- if (p) {
144
- c.writeFileSync(
145
- t,
146
- d.replace(f, ""),
147
- "utf-8"
148
- ), console.log(o.green(`${l}模式下${t}回滚完成`));
149
- return;
150
- }
151
- const b = d + f;
152
- c.writeFileSync(t, b, "utf-8"), console.log(o.green(`模板处理完成,追加到 ${t}`));
153
- } else {
154
- if (p) {
155
- console.log(
156
- o.yellow(`${l}模式下${t}不存在,无需回滚`)
157
- );
158
- return;
159
- }
160
- console.log(o.blue(`output:${t} 不存在,将创建`)), c.writeFileSync(t, f, "utf-8"), console.log(o.green(`模板处理完成,输出到 ${t}`));
161
- }
162
- break;
163
- }
164
- case g.REPLACE: {
165
- if (i && console.log(o.yellow(`output ${i} 将被忽略`)), M(l, s), a && a === s)
166
- return console.log(o.red("env 与 input 不能相同")), process.exit(1);
167
- const t = v.resolve(s);
168
- let d = t;
169
- const b = s.replace(`${process.cwd()}/`, ""), w = E(b)(m);
170
- w !== b && (console.log(
171
- o.green(`检测输入文件名也需要替换
172
- ./${b} => ./${w} `)
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}`));
174
- break;
175
- }
176
- case g.RETURN:
177
- return console.log(
178
- o.green("模板处理完成,返回结果(函数调用才会拿到返回值)")
179
- ), f;
180
- default:
181
- return console.log(o.red(`mode ${l} 不支持`)), process.exit(1);
182
- }
183
- return f;
184
- }, j = async ({
185
- rootDir: e = process.cwd(),
186
- itemDefaultRollback: n = !1,
187
- extraEnvData: a = {}
188
- } = {}, s) => {
189
- let u;
190
- if (s)
191
- u = s;
192
- else {
193
- const r = J(e);
194
- if (!r)
195
- return console.log(o.red(`配置文件${r}不存在`)), process.exit(1);
196
- const m = c.readFileSync(r, "utf-8");
197
- u = JSON.parse(m);
198
- }
199
- const {
200
- list: i,
201
- globalEnvData: l = {},
202
- collectEnvDataForm: p = []
203
- } = u, y = {};
204
- for (const r of p) {
205
- let m, f, t;
206
- typeof r == "string" ? (m = r, f = r, t = void 0) : (m = r.key, f = r.label, t = r.initial), y[m] = (await N({
207
- type: "text",
208
- name: m,
209
- message: `请输入${f}`,
210
- initial: t,
211
- format: (d) => d.trim(),
212
- validate: (d) => d.length > 0 || `${f}不能为空`,
213
- onState: P
214
- }))[m];
215
- }
216
- const h = i.map((r) => {
217
- const { rollback: m = n } = r, {
218
- envData: f,
219
- env: t,
220
- input: d,
221
- output: b,
222
- ...w
223
- } = C(r);
224
- return t && console.log(o.yellow(`批量处理中 env:${t} 将被忽略,只读envData`)), {
225
- ...w,
226
- env: t,
227
- input: e && d ? v.resolve(e, d) : d,
228
- output: e && b ? v.resolve(e, b) : b,
229
- envData: A(
230
- {},
231
- a,
232
- l,
233
- y,
234
- f
235
- ),
236
- rollback: m
237
- };
238
- }), k = [];
239
- for (const r of h) {
240
- const m = await x(r, {
241
- rollbackDelFileAgree: !0
242
- });
243
- k.push(m);
244
- }
245
- return k;
246
- }, F = async (e) => {
247
- const {
248
- envData: n,
249
- env: a,
250
- input: s,
251
- inputData: u,
252
- output: i,
253
- mode: l,
254
- rollback: p,
255
- dealMarkdown: y,
256
- batch: h
257
- } = C(e);
258
- if (h)
259
- return console.log(o.blue("开始批量处理")), j({
260
- // 回滚默认值 基于全局
261
- itemDefaultRollback: p
262
- });
263
- console.log(o.blue("开始单个处理"));
264
- const k = I({
265
- filePath: a,
266
- dataInit: n,
267
- limitJson: !0,
268
- filePathKey: "env",
269
- dataInitKey: "envData",
270
- dealMarkdown: y
271
- });
272
- return x({
273
- input: s,
274
- inputData: u,
275
- output: i,
276
- mode: l,
277
- rollback: p,
278
- dealMarkdown: y,
279
- envData: k
280
- });
281
- }, L = () => ({
282
- env: {
283
- alias: "e",
284
- describe: "环境数据文件JSON文件相对路径(优先级高于envData)",
285
- type: "string"
286
- },
287
- envData: {
288
- alias: "E",
289
- describe: "环境变量数据(JSON字符串)",
290
- type: "string"
291
- },
292
- input: {
293
- alias: "i",
294
- describe: "模板文件相对路径(优先级高于inputTemplate)",
295
- type: "string"
296
- },
297
- inputData: {
298
- alias: "I",
299
- describe: "模板数据",
300
- type: "string"
301
- },
302
- mode: {
303
- alias: "m",
304
- describe: "输出模式",
305
- type: "string",
306
- choices: [
307
- g.OVERWRITE,
308
- g.APPEND,
309
- g.REPLACE,
310
- g.RETURN
311
- ],
312
- default: $.mode
313
- },
314
- output: {
315
- alias: "o",
316
- describe: "输出文件路径",
317
- type: "string"
318
- },
319
- rollback: {
320
- alias: "r",
321
- describe: "是否回滚",
322
- type: "boolean",
323
- default: $.rollback
324
- },
325
- dealMarkdown: {
326
- alias: "d",
327
- describe: "(检测是markdown)是否处理(单个)代码块包裹",
328
- type: "boolean",
329
- default: $.dealMarkdown
330
- },
331
- batch: {
332
- alias: "b",
333
- describe: "是否批量处理",
334
- type: "boolean",
335
- default: $.batch
336
- }
337
- }), O = S.cliConfig.moduleName, V = (e, n) => {
338
- console.log(e ? o.red(e) : o.red(n.message)), process.exit(1);
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);
350
- };
351
- export {
352
- g as O,
353
- oe as a,
354
- j as b,
355
- ne as c,
356
- J as g,
357
- F as h
358
- };