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