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