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