@done-coding/cli-template 0.2.2 → 0.3.1

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/handler.mjs CHANGED
@@ -1,154 +1,216 @@
1
1
  #!/usr/bin/env node
2
- import { OutputModeEnum as p } from "./utils/types.mjs";
3
- import f from "node:path";
4
- import n from "node:fs";
5
- import o from "chalk";
6
- import b from "lodash.template";
7
- import k from "prompts";
8
- const d = ({
9
- filePath: r,
10
- dataInit: l,
11
- json: i,
12
- filePathKey: s,
13
- dataInitKey: g,
14
- dealMarkdown: u = !1
2
+ import { OutputModeEnum as d } from "./utils/types.mjs";
3
+ import { completeDefaultOptions as S } from "./utils/dealOptions.mjs";
4
+ import m from "node:path";
5
+ import s from "node:fs";
6
+ import e from "chalk";
7
+ import x from "lodash.template";
8
+ import N from "lodash.assign";
9
+ import F from "prompts";
10
+ import I from "./injectInfo.json.mjs";
11
+ const b = ({
12
+ filePath: l,
13
+ dataInit: n,
14
+ limitJson: u,
15
+ filePathKey: c,
16
+ dataInitKey: f,
17
+ dealMarkdown: i = !1
15
18
  }) => {
16
- if (r) {
17
- if (i && !r.endsWith(".json"))
19
+ if (l) {
20
+ if (u && !l.endsWith(".json"))
18
21
  return console.log(
19
- o.red(`${s}必须是json文件,请检查文件后缀名`)
22
+ e.red(`${c}必须是json文件,请检查文件后缀名`)
20
23
  ), process.exit(1);
21
- const t = n.readFileSync(f.resolve(r), "utf-8");
22
- let c = t;
23
- return u && r.endsWith(".md") && (c = t.replace(
24
+ const t = s.readFileSync(m.resolve(l), "utf-8");
25
+ let r = t;
26
+ return i && l.endsWith(".md") && (r = t.replace(
24
27
  /^\s*```[a-zA-Z0-9]+\s*[\r\n]+([\s\S]+?)```\s*$/,
25
28
  "$1"
26
- )), i ? JSON.parse(c) : c;
29
+ )), u ? JSON.parse(r) : r;
27
30
  } else
28
- return l ? (console.log(
29
- o.green(`${s} 为空,将使用${g}作为数据`)
30
- ), i ? JSON.parse(l) : l) : (console.log(o.red(`${s}与${g}不能同时为空`)), process.exit(1));
31
- }, y = (r, l) => {
32
- if (!l)
33
- return console.log(o.red(`${r}模式下output不能为空`)), process.exit(1);
34
- }, v = (r, l) => {
35
- if (l && r === l)
36
- return console.log(o.red("output与input不能相同")), process.exit(1);
37
- }, x = (r, l) => {
38
- if (!l)
39
- return console.log(o.red(`${r}模式下input不能为空`)), process.exit(1);
40
- }, O = async (r) => {
31
+ return n ? (console.log(
32
+ e.green(`${c} 为空,将使用${f}作为数据`)
33
+ ), u ? JSON.parse(n) : n) : (console.log(e.red(`${c}与${f}不能同时为空`)), process.exit(1));
34
+ }, y = (l, n) => {
35
+ if (!n)
36
+ return console.log(e.red(`${l}模式下output不能为空`)), process.exit(1);
37
+ }, D = (l, n) => {
38
+ if (n && l === n)
39
+ return console.log(e.red("output与input不能相同")), process.exit(1);
40
+ }, R = (l, n) => {
41
+ if (!n)
42
+ return console.log(e.red(`${l}模式下input不能为空`)), process.exit(1);
43
+ }, w = async (l, {
44
+ rollbackDelFileAgree: n = !1
45
+ } = {}) => {
41
46
  const {
42
- envData: l,
43
- env: i,
44
- input: s,
45
- inputData: g,
46
- output: u,
47
- mode: t = p.OVERWRITE,
48
- rollback: c = !1,
49
- dealMarkdown: m = !1
50
- } = r;
51
- if (c)
47
+ env: u,
48
+ input: c,
49
+ inputData: f,
50
+ output: i,
51
+ mode: t,
52
+ rollback: r,
53
+ dealMarkdown: a,
54
+ envData: p
55
+ } = l;
56
+ if (r)
52
57
  switch (t) {
53
- case p.REPLACE:
54
- case p.RETURN: {
55
- console.log(o.red(`${t}模式不支持回滚`));
58
+ case d.REPLACE:
59
+ case d.RETURN: {
60
+ console.log(e.red(`${t}模式不支持回滚`));
56
61
  return;
57
62
  }
58
63
  }
59
64
  console.log(
60
- o.blue(`开始处理模板
65
+ e.blue(`开始处理模板
61
66
  mode: ${t}
62
- rollback: ${c}
67
+ rollback: ${r}
63
68
  `)
64
69
  );
65
- const S = d({
66
- filePath: i,
67
- dataInit: l,
68
- json: !0,
69
- filePathKey: "env",
70
- dataInitKey: "envData",
71
- dealMarkdown: m
72
- }), w = d({
73
- filePath: s,
74
- dataInit: g,
75
- json: !1,
70
+ const $ = b({
71
+ filePath: c,
72
+ dataInit: f,
73
+ limitJson: !1,
76
74
  filePathKey: "input",
77
75
  dataInitKey: "inputData",
78
- dealMarkdown: m
79
- }), a = b(w)(S);
76
+ dealMarkdown: a
77
+ }), k = x($), E = typeof p == "function" ? p() : p, g = k(E);
80
78
  switch (t) {
81
- case p.OVERWRITE: {
82
- y(t, u), v(u, s);
83
- const e = f.resolve(u);
84
- if (n.mkdirSync(f.dirname(e), { recursive: !0 }), n.existsSync(e)) {
85
- if (c)
86
- if ((await k({
79
+ case d.OVERWRITE: {
80
+ y(t, i), D(i, c);
81
+ const o = m.resolve(i);
82
+ if (s.mkdirSync(m.dirname(o), { recursive: !0 }), s.existsSync(o)) {
83
+ if (r)
84
+ if (n || (await F({
87
85
  type: "confirm",
88
86
  name: "remove",
89
- message: `${t}模式下回滚将删除${e},是否继续?`
87
+ message: `${t}模式下回滚将删除${o},是否继续?`
90
88
  })).remove) {
91
- n.rmSync(e, { force: !0 }), console.log(o.green(`${t}模式下${e}已删除`));
89
+ s.rmSync(o, { force: !0 }), console.log(e.green(`${t}模式下${o}已删除`));
92
90
  return;
93
91
  } else {
94
- console.log(o.yellow(`${t}模式下${e}回滚取消`));
92
+ console.log(e.yellow(`${t}模式下${o}回滚取消`));
95
93
  return;
96
94
  }
97
- console.log(o.blue(`output:${e} 已存在,将覆盖`));
95
+ console.log(e.blue(`output:${o} 已存在,将覆盖`));
98
96
  } else {
99
- if (c) {
97
+ if (r) {
100
98
  console.log(
101
- o.yellow(`${t}模式下${e}不存在,无需回滚`)
99
+ e.yellow(`${t}模式下${o}不存在,无需回滚`)
102
100
  );
103
101
  return;
104
102
  }
105
- console.log(o.blue(`output:${e} 不存在,将创建`));
103
+ console.log(e.blue(`output:${o} 不存在,将创建`));
106
104
  }
107
- n.writeFileSync(e, a, "utf-8"), console.log(o.green(`模板处理完成,输出到 ${e}`));
105
+ s.writeFileSync(o, g, "utf-8"), console.log(e.green(`模板处理完成,输出到 ${o}`));
108
106
  break;
109
107
  }
110
- case p.APPEND: {
111
- y(t, u), v(u, s);
112
- const e = f.resolve(u);
113
- if (n.mkdirSync(f.dirname(e), { recursive: !0 }), n.existsSync(e)) {
114
- const $ = n.readFileSync(e, "utf-8");
115
- if (c) {
116
- n.writeFileSync(
117
- e,
118
- $.replace(a, ""),
108
+ case d.APPEND: {
109
+ y(t, i), D(i, c);
110
+ const o = m.resolve(i);
111
+ if (s.mkdirSync(m.dirname(o), { recursive: !0 }), s.existsSync(o)) {
112
+ const v = s.readFileSync(o, "utf-8");
113
+ if (r) {
114
+ s.writeFileSync(
115
+ o,
116
+ v.replace(g, ""),
119
117
  "utf-8"
120
- ), console.log(o.green(`${t}模式下${e}回滚完成`));
118
+ ), console.log(e.green(`${t}模式下${o}回滚完成`));
121
119
  return;
122
120
  }
123
- const E = $ + a;
124
- n.writeFileSync(e, E, "utf-8"), console.log(o.green(`模板处理完成,追加到 ${e}`));
121
+ const h = v + g;
122
+ s.writeFileSync(o, h, "utf-8"), console.log(e.green(`模板处理完成,追加到 ${o}`));
125
123
  } else {
126
- if (c) {
124
+ if (r) {
127
125
  console.log(
128
- o.yellow(`${t}模式下${e}不存在,无需回滚`)
126
+ e.yellow(`${t}模式下${o}不存在,无需回滚`)
129
127
  );
130
128
  return;
131
129
  }
132
- console.log(o.blue(`output:${e} 不存在,将创建`)), n.writeFileSync(e, a, "utf-8"), console.log(o.green(`模板处理完成,输出到 ${e}`));
130
+ console.log(e.blue(`output:${o} 不存在,将创建`)), s.writeFileSync(o, g, "utf-8"), console.log(e.green(`模板处理完成,输出到 ${o}`));
133
131
  }
134
132
  break;
135
133
  }
136
- case p.REPLACE: {
137
- if (u && console.log(o.yellow(`output ${u} 将被忽略`)), x(t, s), i && i === s)
138
- return console.log(o.red("env 与 input 不能相同")), process.exit(1);
139
- const e = f.resolve(s);
140
- n.mkdirSync(f.dirname(e), { recursive: !0 }), n.writeFileSync(e, a, "utf-8"), console.log(o.green(`模板处理完成,输出到 ${e}`));
134
+ case d.REPLACE: {
135
+ if (i && console.log(e.yellow(`output ${i} 将被忽略`)), R(t, c), u && u === c)
136
+ return console.log(e.red("env 与 input 不能相同")), process.exit(1);
137
+ const o = m.resolve(c);
138
+ s.mkdirSync(m.dirname(o), { recursive: !0 }), s.writeFileSync(o, g, "utf-8"), console.log(e.green(`模板处理完成,输出到 ${o}`));
141
139
  break;
142
140
  }
143
- case p.RETURN:
141
+ case d.RETURN:
144
142
  return console.log(
145
- o.green("模板处理完成,返回结果(函数调用才会拿到返回值)")
146
- ), a;
143
+ e.green("模板处理完成,返回结果(函数调用才会拿到返回值)")
144
+ ), g;
147
145
  default:
148
- return console.log(o.red(`mode ${t} 不支持`)), process.exit(1);
146
+ return console.log(e.red(`mode ${t} 不支持`)), process.exit(1);
149
147
  }
150
- return a;
148
+ return g;
149
+ }, C = async ({
150
+ itemDefaultRollback: l = !1
151
+ } = {}, n) => {
152
+ let u;
153
+ if (n)
154
+ u = n;
155
+ else {
156
+ const { namespaceDir: t, moduleName: r } = I.cliConfig, a = m.resolve(t, `${r}.json`);
157
+ if (!s.existsSync(a))
158
+ return console.log(e.red(`配置文件${a}不存在`)), process.exit(1);
159
+ const p = s.readFileSync(a, "utf-8");
160
+ u = JSON.parse(p);
161
+ }
162
+ const { list: c, globalEnvData: f } = u, i = c.map((t) => {
163
+ const { rollback: r = l } = t, { envData: a, env: p, ...$ } = S(t);
164
+ return p && console.log(e.yellow(`批量处理中 env:${p} 将被忽略,只读envData`)), {
165
+ ...$,
166
+ envData: N({}, f, a),
167
+ rollback: r
168
+ };
169
+ });
170
+ return Promise.all(
171
+ i.map(
172
+ (t) => w(t, {
173
+ rollbackDelFileAgree: !0
174
+ })
175
+ )
176
+ );
177
+ }, U = async (l) => {
178
+ const {
179
+ envData: n,
180
+ env: u,
181
+ input: c,
182
+ inputData: f,
183
+ output: i,
184
+ mode: t,
185
+ rollback: r,
186
+ dealMarkdown: a,
187
+ batch: p
188
+ } = S(l);
189
+ if (p)
190
+ return console.log(e.blue("开始批量处理")), C({
191
+ // 回滚默认值 基于全局
192
+ itemDefaultRollback: r
193
+ });
194
+ console.log(e.blue("开始单个处理"));
195
+ const $ = b({
196
+ filePath: u,
197
+ dataInit: n,
198
+ limitJson: !0,
199
+ filePathKey: "env",
200
+ dataInitKey: "envData",
201
+ dealMarkdown: a
202
+ });
203
+ return w({
204
+ input: c,
205
+ inputData: f,
206
+ output: i,
207
+ mode: t,
208
+ rollback: r,
209
+ dealMarkdown: a,
210
+ envData: $
211
+ });
151
212
  };
152
213
  export {
153
- O as handler
214
+ C as batchHandler,
215
+ U as handler
154
216
  };
@@ -1,11 +1,11 @@
1
1
  #!/usr/bin/env node
2
2
  const e = {
3
- version: "0.2.2",
3
+ version: "0.3.1",
4
4
  name: "@done-coding/cli-template",
5
5
  description: "预编译命令行工具",
6
6
  cliConfig: {
7
7
  namespaceDir: ".done-coding",
8
- commandName: "template"
8
+ moduleName: "template"
9
9
  }
10
10
  };
11
11
  export {
package/es/main.mjs CHANGED
@@ -1,11 +1,12 @@
1
1
  #!/usr/bin/env node
2
- import l from "yargs";
3
- import { hideBin as d } from "yargs/helpers";
4
- import { OutputModeEnum as s } from "./utils/types.mjs";
5
- import { handler as i } from "./handler.mjs";
6
- import t from "chalk";
7
- import a from "./injectInfo.json.mjs";
8
- const m = () => ({
2
+ import d from "yargs";
3
+ import { hideBin as m } from "yargs/helpers";
4
+ import { OutputModeEnum as a } from "./utils/types.mjs";
5
+ import { defaultOptions as t } from "./utils/dealOptions.mjs";
6
+ import { handler as n } from "./handler.mjs";
7
+ import s from "chalk";
8
+ import i from "./injectInfo.json.mjs";
9
+ const p = () => ({
9
10
  env: {
10
11
  alias: "e",
11
12
  describe: "环境数据文件JSON文件相对路径(优先级高于envData)",
@@ -31,12 +32,12 @@ const m = () => ({
31
32
  describe: "输出模式",
32
33
  type: "string",
33
34
  choices: [
34
- s.OVERWRITE,
35
- s.APPEND,
36
- s.REPLACE,
37
- s.RETURN
35
+ a.OVERWRITE,
36
+ a.APPEND,
37
+ a.REPLACE,
38
+ a.RETURN
38
39
  ],
39
- default: s.OVERWRITE
40
+ default: t.mode
40
41
  },
41
42
  output: {
42
43
  alias: "o",
@@ -47,29 +48,35 @@ const m = () => ({
47
48
  alias: "r",
48
49
  describe: "是否回滚",
49
50
  type: "boolean",
50
- default: !1
51
+ default: t.rollback
51
52
  },
52
53
  dealMarkdown: {
53
54
  alias: "d",
54
55
  describe: "(检测是markdown)是否处理(单个)代码块包裹",
55
56
  type: "boolean",
56
- default: !1
57
+ default: t.dealMarkdown
58
+ },
59
+ batch: {
60
+ alias: "b",
61
+ describe: "是否批量处理",
62
+ type: "boolean",
63
+ default: t.batch
57
64
  }
58
- }), n = a.cliConfig.commandName, p = (e, o) => {
59
- console.log(e ? t.red(e) : t.red(o.message)), process.exit(1);
60
- }, g = a.description, u = `Usage: $0 ${n} [options]`, f = "Usage: $0 [options]", r = (e, o) => {
61
- const c = m();
62
- return e.strict().usage(o).help("help").version(a.version).alias("v", "version").alias("h", "help").options(c).fail(p).argv;
63
- }, b = (e) => r(e, u), N = {
64
- command: n,
65
- describe: g,
66
- builder: b,
67
- handler: i
68
- }, O = async () => {
69
- const e = l(d(process.argv)), o = await r(e, f);
70
- return i(o);
65
+ }), r = i.cliConfig.moduleName, u = (e, o) => {
66
+ console.log(e ? s.red(e) : s.red(o.message)), process.exit(1);
67
+ }, b = i.description, g = `Usage: $0 ${r} [options]`, f = "Usage: $0 [options]", c = (e, o) => {
68
+ const l = p();
69
+ return e.strict().usage(o).help("help").version(i.version).alias("v", "version").alias("h", "help").options(l).fail(u).argv;
70
+ }, h = (e) => c(e, g), D = {
71
+ command: r,
72
+ describe: b,
73
+ builder: h,
74
+ handler: n
75
+ }, R = async () => {
76
+ const e = d(m(process.argv)), o = await c(e, f);
77
+ return n(o);
71
78
  };
72
79
  export {
73
- N as command,
74
- O as createCli
80
+ D as command,
81
+ R as createCli
75
82
  };
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env node
2
+ import { OutputModeEnum as n } from "./types.mjs";
3
+ const o = {
4
+ rollback: !1,
5
+ dealMarkdown: !1,
6
+ mode: n.OVERWRITE,
7
+ batch: !1
8
+ }, s = (a) => {
9
+ const { rollback: e, dealMarkdown: l, mode: t, batch: d, ...r } = a;
10
+ return {
11
+ rollback: e ?? o.rollback,
12
+ dealMarkdown: l ?? o.dealMarkdown,
13
+ mode: t ?? o.mode,
14
+ batch: d ?? o.batch,
15
+ ...r
16
+ };
17
+ };
18
+ export {
19
+ s as completeDefaultOptions,
20
+ o as defaultOptions
21
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@done-coding/cli-template",
3
- "version": "0.2.2",
3
+ "version": "0.3.1",
4
4
  "description": "预编译命令行工具",
5
5
  "private": false,
6
6
  "module": "es/index.mjs",
@@ -41,11 +41,12 @@
41
41
  "license": "MIT",
42
42
  "sideEffects": false,
43
43
  "devDependencies": {
44
- "@done-coding/cli-inject": "^0.3.0",
44
+ "@done-coding/cli-inject": "^0.4.0",
45
+ "@types/lodash.assign": "^4.2.9",
45
46
  "@types/lodash.get": "^4.4.9",
46
47
  "@types/lodash.set": "^4.3.9",
47
48
  "@types/lodash.template": "^4.5.3",
48
- "@types/node": "^20.0.0",
49
+ "@types/node": "^18.0.0",
49
50
  "@types/prompts": "^2.4.6",
50
51
  "@types/yargs": "^17.0.28",
51
52
  "rimraf": "^6.0.1",
@@ -54,15 +55,16 @@
54
55
  "vite-plugin-dts": "^3.6.0"
55
56
  },
56
57
  "engines": {
57
- "node": ">=16.0.0"
58
+ "node": ">=18.0.0"
58
59
  },
59
60
  "dependencies": {
60
61
  "chalk": "^5.3.0",
62
+ "lodash.assign": "^4.2.0",
61
63
  "lodash.get": "^4.4.2",
62
64
  "lodash.set": "^4.3.2",
63
65
  "lodash.template": "^4.5.0",
64
66
  "prompts": "^2.4.2",
65
67
  "yargs": "^17.7.2"
66
68
  },
67
- "gitHead": "0e9dcf532b8abf7608d07f738348538fa9a9864d"
69
+ "gitHead": "b3311b79a55322e4689718d59c072388e2866e17"
68
70
  }
@@ -1,3 +1,8 @@
1
+ import type { CompileTemplateConfig } from './utils';
1
2
  import { type Options } from './utils';
2
3
  import type { ArgumentsCamelCase } from "yargs";
3
- export declare const handler: (argv: ArgumentsCamelCase<Options> | Options) => Promise<string | undefined>;
4
+ export declare const batchHandler: ({ itemDefaultRollback, }?: {
5
+ /** item默认回滚? */
6
+ itemDefaultRollback?: boolean | undefined;
7
+ }, paramsConfig?: CompileTemplateConfig) => Promise<(string | undefined)[]>;
8
+ export declare const handler: (argv: ArgumentsCamelCase<Options> | Options) => Promise<string | (string | undefined)[] | undefined>;
@@ -1,10 +1,10 @@
1
1
  declare const _default: {
2
- "version": "0.2.2",
2
+ "version": "0.3.1",
3
3
  "name": "@done-coding/cli-template",
4
4
  "description": "预编译命令行工具",
5
5
  "cliConfig": {
6
6
  "namespaceDir": ".done-coding",
7
- "commandName": "template"
7
+ "moduleName": "template"
8
8
  }
9
9
  };
10
10
 
package/types/main.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import type { CommandModule } from "yargs";
2
2
  import type { Options } from './utils';
3
3
  export declare const command: CommandModule<Options, Options>;
4
- export declare const createCli: () => Promise<string | undefined>;
4
+ export declare const createCli: () => Promise<string | (string | undefined)[] | undefined>;
@@ -0,0 +1,10 @@
1
+ import { OutputModeEnum, type Options } from "./types";
2
+ /** 默认选项 */
3
+ export declare const defaultOptions: Pick<Options, "rollback" | "dealMarkdown" | "mode" | "batch">;
4
+ /** 设置默认选项 */
5
+ export declare const completeDefaultOptions: <T extends Pick<Options, "mode" | "rollback" | "dealMarkdown" | "batch">>(options: T) => {
6
+ rollback: boolean | undefined;
7
+ dealMarkdown: boolean | undefined;
8
+ mode: OutputModeEnum;
9
+ batch: boolean | undefined;
10
+ } & Omit<T, "mode" | "rollback" | "dealMarkdown" | "batch">;
@@ -1 +1,2 @@
1
1
  export * from "./types";
2
+ export * from "./dealOptions";
@@ -26,4 +26,18 @@ export interface Options {
26
26
  rollback?: boolean;
27
27
  /** (检测是markdown)是否处理(单个)代码块包裹 */
28
28
  dealMarkdown?: boolean;
29
+ /** 是否批量处理 */
30
+ batch?: boolean;
31
+ }
32
+ /** 编译模板配置选项 */
33
+ export type CompileTemplateConfigListItem = Omit<Options, "envData"> & {
34
+ /** 已经解析为对象的envData */
35
+ envData: Record<string, any>;
36
+ };
37
+ /** 编译模板配置 */
38
+ export interface CompileTemplateConfig {
39
+ /** 全局环境变量 */
40
+ globalEnvData: Record<string, any>;
41
+ /** 配置列表 */
42
+ list: CompileTemplateConfigListItem[];
29
43
  }