done-coding-extract 0.1.8 → 0.1.10-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,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
- import { c as m } from "./index-2f3f403a.js";
2
+ import { c as m } from "./index-ad21e5e4.js";
3
+ import "./index-a1ee6691-8c73eb4d.js";
3
4
  import "@done-coding/cli-utils";
4
- import "./types-a1ee6691-9629699d.js";
5
5
  import "node:path";
6
6
  import "node:fs";
7
7
  import "@done-coding/cli-template";
package/es/helpers.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { E as e, a as n } from "./types-a1ee6691-9629699d.js";
2
+ import { E as e, a as n } from "./index-a1ee6691-8c73eb4d.js";
3
3
  import "@done-coding/cli-utils";
4
4
  import "node:path";
5
5
  import "node:fs";
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env node
2
+ var r = /* @__PURE__ */ ((e) => (e.INIT = "init", e.GENERATE = "generate", e))(r || {}), E = /* @__PURE__ */ ((e) => (e.REG = "reg", e.JSON_INJECT = "json-inject", e.FIXED = "fixed", e))(E || {}), a = /* @__PURE__ */ ((e) => (e.RESULT = "result", e.TEMPLATE = "template", e))(a || {}), i = /* @__PURE__ */ ((e) => (e.INIT = "init", e.GENERATE = "generate", e))(i || {}), t = /* @__PURE__ */ ((e) => (e.REG = "reg", e.FIXED = "fixed", e.READ = "read", e))(t || {});
3
+ export {
4
+ E,
5
+ a as G,
6
+ r as S,
7
+ t as a,
8
+ i as e
9
+ };
@@ -0,0 +1,333 @@
1
+ #!/usr/bin/env node
2
+ import { a as r, e as T, E as u, S as h, G as $ } from "./index-a1ee6691-8c73eb4d.js";
3
+ import { log as s, _get as b, createSubcommand as N, getConfigFileCommonOptions as D, initHandlerCommon as v, readConfigFile as F, _set as L, createMainCommand as V } from "@done-coding/cli-utils";
4
+ import j from "node:path";
5
+ import y from "node:fs";
6
+ import { OutputModeEnum as U, batchCompileHandler as W } from "@done-coding/cli-template";
7
+ const O = {
8
+ name: "@done-coding/cli-extract",
9
+ version: "0.1.10-alpha.0",
10
+ description: "信息提取命令行工具",
11
+ cliConfig: {
12
+ namespaceDir: ".done-coding",
13
+ moduleName: "extract"
14
+ }
15
+ }, {
16
+ cliConfig: { namespaceDir: H, moduleName: X }
17
+ } = O, K = `./${H}/${X}`, J = `${K}.json5`, C = ({
18
+ sourceJson: e,
19
+ targetKey: n,
20
+ keyConfig: o
21
+ }) => {
22
+ let t;
23
+ typeof o == "string" ? t = {
24
+ type: r.FIXED,
25
+ value: o
26
+ } : t = o;
27
+ const { type: a = r.READ } = t;
28
+ switch (a) {
29
+ case r.REG: {
30
+ const { sourceKey: c, pattern: m, replaceValue: l, flags: f } = t, d = new RegExp(m, f ?? void 0), i = b(e, c);
31
+ return typeof i == "string" ? i.replace(d, l) : (s.warn(
32
+ `${i}不是字符串类型,无法使用正则表达式进行替换,此处将直接返回原值`
33
+ ), i);
34
+ }
35
+ case r.FIXED: {
36
+ const { value: c } = t;
37
+ return c;
38
+ }
39
+ case r.READ: {
40
+ const { sourceKey: c = n } = t;
41
+ return b(e, c);
42
+ }
43
+ default: {
44
+ s.warn(`未知的配置类型${a}`);
45
+ return;
46
+ }
47
+ }
48
+ }, S = {
49
+ name: "@done-coding/cli-inject",
50
+ version: "0.5.10-alpha.0",
51
+ description: "信息(JSON)注入命令行工具",
52
+ cliConfig: {
53
+ namespaceDir: ".done-coding",
54
+ moduleName: "inject"
55
+ }
56
+ }, {
57
+ cliConfig: { namespaceDir: q, moduleName: z }
58
+ } = S, B = `./${q}/${z}`, A = `${B}.json`, Q = {
59
+ sourceFilePath: "./package.json",
60
+ keyConfigMap: {
61
+ name: {
62
+ type: r.READ
63
+ },
64
+ version: {
65
+ type: r.READ
66
+ },
67
+ description: {
68
+ type: r.READ
69
+ }
70
+ },
71
+ injectFilePath: "./src/injectInfo.json"
72
+ }, x = Q, Y = () => D({
73
+ configPathDefault: A
74
+ }), Z = async (e) => v(x, e, {
75
+ onFileGenerated: () => {
76
+ s.info("文件生成成功");
77
+ }
78
+ }), ee = {
79
+ command: T.INIT,
80
+ describe: "初始化配置文件",
81
+ options: Y(),
82
+ handler: Z
83
+ }, ne = () => ({
84
+ ...D({
85
+ configPathDefault: A
86
+ })
87
+ }), te = async ({
88
+ rootDir: e = process.cwd(),
89
+ config: n = x,
90
+ keyConfigMap: o = {}
91
+ } = {}) => {
92
+ const {
93
+ sourceFilePath: t,
94
+ keyConfigMap: a,
95
+ injectFilePath: c
96
+ } = n, m = {
97
+ ...a,
98
+ ...o
99
+ };
100
+ if (!t.endsWith(".json"))
101
+ return s.error("源文件必须是json"), process.exit(1);
102
+ if (!c.endsWith(".json"))
103
+ return s.error("注入文件必须是json"), process.exit(1);
104
+ const l = j.resolve(e, t), f = y.readFileSync(l, "utf-8"), d = JSON.parse(f), i = Object.entries(m).reduce(
105
+ (g, [R, P]) => {
106
+ const M = C({ sourceJson: d, targetKey: R, keyConfig: P });
107
+ return L(g, R, M), g;
108
+ },
109
+ {}
110
+ ), p = j.resolve(e, c), E = JSON.stringify(i, null, 2);
111
+ if (y.existsSync(p)) {
112
+ const g = y.readFileSync(p, "utf-8");
113
+ if (E === g)
114
+ return s.skip("注入文件已存在且内容相同,无需重复注入"), i;
115
+ s.stage("文件内容变化,开始覆盖注入文件");
116
+ } else
117
+ s.stage("开始注入文件");
118
+ y.writeFileSync(p, E), s.success(`文件注入成功: ${p}`), s.info(E);
119
+ }, oe = async (e) => {
120
+ const n = await F(e);
121
+ if (!n)
122
+ return s.error("配置文件为空"), process.exit(1);
123
+ const { rootDir: o } = e;
124
+ await te({ rootDir: o, config: n });
125
+ }, se = {
126
+ command: T.GENERATE,
127
+ describe: "生成文件",
128
+ options: ne(),
129
+ handler: oe
130
+ }, { version: re, description: ce } = S;
131
+ [ee, se].map(
132
+ N
133
+ );
134
+ const ie = ({
135
+ input: e,
136
+ rootDir: n
137
+ }) => {
138
+ const o = j.resolve(n, e), t = y.readFileSync(o, "utf-8");
139
+ return e.endsWith(".json") || e.endsWith(".json5") ? JSON.parse(t) : t;
140
+ }, ae = (e) => typeof e == "object" && !!e, ue = ({
141
+ content: e,
142
+ targetKey: n,
143
+ keyConfig: o
144
+ }) => {
145
+ const { type: t, ...a } = o;
146
+ switch (t) {
147
+ case u.REG: {
148
+ if (typeof e != "string")
149
+ throw new Error(`${t} 类型的keyConfig需要content为字符串`);
150
+ return C({
151
+ sourceJson: {
152
+ [n]: e
153
+ },
154
+ targetKey: n,
155
+ keyConfig: {
156
+ ...a,
157
+ type: r.REG,
158
+ sourceKey: n
159
+ }
160
+ });
161
+ }
162
+ case u.JSON_INJECT: {
163
+ if (!ae(e))
164
+ throw new Error(`${t} 类型的keyConfig需要content为json`);
165
+ return C({
166
+ sourceJson: e,
167
+ targetKey: n,
168
+ keyConfig: o.inject
169
+ });
170
+ }
171
+ case u.FIXED:
172
+ return C({
173
+ sourceJson: {},
174
+ targetKey: n,
175
+ keyConfig: {
176
+ ...a,
177
+ type: r.FIXED
178
+ }
179
+ });
180
+ default:
181
+ throw new Error(`不支持的类型${t}`);
182
+ }
183
+ }, de = {
184
+ name: "${name}",
185
+ version: "${version}",
186
+ description: "${description}"
187
+ }, pe = {
188
+ extractInput: {
189
+ "./package.json": {
190
+ name: {
191
+ type: u.JSON_INJECT,
192
+ inject: {
193
+ type: r.READ
194
+ }
195
+ },
196
+ description: {
197
+ type: u.JSON_INJECT,
198
+ inject: {
199
+ type: r.READ
200
+ }
201
+ },
202
+ version: {
203
+ type: u.JSON_INJECT,
204
+ inject: {
205
+ type: r.READ
206
+ }
207
+ }
208
+ },
209
+ "./README.md": {
210
+ docDescription: {
211
+ type: u.REG,
212
+ pattern: /#\s*([^`])+```([^`]+)```/.source,
213
+ replaceValue: ""
214
+ },
215
+ usage: {
216
+ type: u.REG,
217
+ pattern: /##\s*使用\s*```([^`]+)```/.source,
218
+ replaceValue: "$1"
219
+ }
220
+ }
221
+ },
222
+ extractOutput: {
223
+ list: [
224
+ {
225
+ mode: U.OVERWRITE,
226
+ inputData: JSON.stringify(de, null, 2),
227
+ output: "./src/extractInfo.json"
228
+ }
229
+ ]
230
+ }
231
+ }, me = () => D({
232
+ configPathDefault: J
233
+ }), G = async (e) => v(pe, e, {
234
+ onFileGenerated: () => {
235
+ s.info("文件生成成功");
236
+ }
237
+ }), le = {
238
+ command: h.INIT,
239
+ describe: "初始化配置文件",
240
+ options: me(),
241
+ handler: G
242
+ }, fe = () => ({
243
+ ...D({
244
+ configPathDefault: J
245
+ }),
246
+ mode: {
247
+ type: "string",
248
+ alias: "m",
249
+ choices: Object.values($),
250
+ default: $.RESULT,
251
+ describe: "生成模式"
252
+ }
253
+ }), Ee = async ({
254
+ rootDir: e = process.cwd(),
255
+ config: n
256
+ }) => {
257
+ const { extractInput: o, extractOutput: t } = n, a = Object.entries(o).reduce(
258
+ (c, [m, l]) => {
259
+ const f = ie({
260
+ rootDir: e,
261
+ input: m
262
+ });
263
+ return Object.entries(l).reduce(
264
+ (d, [i, p]) => {
265
+ const E = ue({
266
+ content: f,
267
+ targetKey: i,
268
+ keyConfig: p
269
+ });
270
+ return d[i] = E, d;
271
+ },
272
+ c
273
+ );
274
+ },
275
+ {}
276
+ );
277
+ await W(
278
+ {
279
+ rootDir: e,
280
+ extraEnvData: a
281
+ },
282
+ t
283
+ );
284
+ }, I = async (e) => {
285
+ const n = await F(e);
286
+ if (!n)
287
+ return s.error("配置文件为空"), process.exit(1);
288
+ const { rootDir: o } = e;
289
+ await Ee({ rootDir: o, config: n });
290
+ }, ye = {
291
+ command: h.GENERATE,
292
+ describe: "生成文件",
293
+ options: fe(),
294
+ handler: I
295
+ }, Oe = async (e, n) => {
296
+ switch (e) {
297
+ case h.INIT:
298
+ return G(n);
299
+ case h.GENERATE:
300
+ return I(n);
301
+ default:
302
+ return I(n);
303
+ }
304
+ }, { version: ge, description: Ce } = O, k = {
305
+ describe: Ce,
306
+ version: ge,
307
+ subcommands: [le, ye].map(
308
+ N
309
+ ),
310
+ demandCommandCount: 1
311
+ }, {
312
+ cliConfig: { moduleName: w }
313
+ } = O, _ = (e = !1) => {
314
+ const n = e ? w : void 0, o = `$0${e ? ` ${w}` : ""} <command> [options]`;
315
+ return { command: n, usage: o };
316
+ }, Re = async () => V({
317
+ ...k,
318
+ ..._()
319
+ }), $e = () => N({
320
+ ...k,
321
+ ..._(!0)
322
+ });
323
+ export {
324
+ $e as a,
325
+ le as b,
326
+ Re as c,
327
+ I as d,
328
+ ye as e,
329
+ Oe as f,
330
+ Ee as g,
331
+ G as h,
332
+ k as i
333
+ };
package/es/index.mjs CHANGED
@@ -1,14 +1,20 @@
1
1
  #!/usr/bin/env node
2
- import { a as n, g as p, h as s } from "./index-2f3f403a.js";
3
- import { E as u, S as c } from "./types-a1ee6691-9629699d.js";
2
+ import { i as t, a as i, e as s, g as d, d as l, f as p, b as f, h as u } from "./index-ad21e5e4.js";
3
+ import { E as C, G as E, S as b } from "./index-a1ee6691-8c73eb4d.js";
4
4
  import "@done-coding/cli-utils";
5
5
  import "node:path";
6
6
  import "node:fs";
7
7
  import "@done-coding/cli-template";
8
8
  export {
9
- u as ExtractTypeEnum,
10
- c as SubcommandEnum,
11
- n as crateAsSubcommand,
12
- p as generateFile,
13
- s as handler
9
+ C as ExtractTypeEnum,
10
+ E as GenerateModeEnum,
11
+ b as SubcommandEnum,
12
+ t as commandCliInfo,
13
+ i as crateAsSubcommand,
14
+ s as generateCommandCliInfo,
15
+ d as generateFile,
16
+ l as generateHandler,
17
+ p as handler,
18
+ f as initCommandCliInfo,
19
+ u as initHandler
14
20
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "done-coding-extract",
3
- "version": "0.1.8",
3
+ "version": "0.1.10-alpha.0",
4
4
  "description": "信息提取命令行工具",
5
5
  "private": false,
6
6
  "module": "es/index.mjs",
@@ -42,7 +42,7 @@
42
42
  "license": "MIT",
43
43
  "sideEffects": false,
44
44
  "devDependencies": {
45
- "@done-coding/cli-inject": "^0.5.8",
45
+ "@done-coding/cli-inject": "^0.5.10-alpha.0",
46
46
  "@types/node": "^18.0.0",
47
47
  "@types/yargs": "^17.0.28",
48
48
  "rimraf": "^6.0.1",
@@ -54,9 +54,9 @@
54
54
  "node": ">=18.0.0"
55
55
  },
56
56
  "dependencies": {
57
- "@done-coding/cli-template": "0.7.8",
58
- "@done-coding/cli-utils": "^0.7.0"
57
+ "@done-coding/cli-template": "0.7.10-alpha.0",
58
+ "@done-coding/cli-utils": "^0.7.1-alpha.0"
59
59
  },
60
- "gitHead": "b4fb4f2380a14f72597a1dbfd1bbce22287ef02f",
60
+ "gitHead": "3376f5fbbf8b87c791316e76df06028afd650e08",
61
61
  "scripts": {}
62
62
  }
package/types/cli.d.ts CHANGED
@@ -1,2 +1 @@
1
- #!/usr/bin/env node
2
- export {};
1
+ export { }
@@ -1 +1,70 @@
1
- export * from './helpers-assets/done-coding';
1
+ import type { CompileTemplateConfig } from '@done-coding/cli-template';
2
+ import type { InjectKeyConfig } from '@done-coding/cli-inject';
3
+ import type { InjectKeyConfigFixed } from '@done-coding/cli-inject';
4
+ import type { InjectKeyConfigReg } from '@done-coding/cli-inject';
5
+
6
+ /** done-coding 系列cli 别名发布信息 */
7
+ export declare const doneCodingPublishAliasInfo: ExtractConfig;
8
+
9
+ /** done-coding 系列cli 注入信息 */
10
+ export declare const doneCodingSeriesCliInjectInfo: ExtractConfig;
11
+
12
+ declare interface ExtractConfig {
13
+ /**
14
+ * 输入配置
15
+ * -- 每个源文件 对应一个配置
16
+ */
17
+ extractInput: Record<string, ExtractInputConfig>;
18
+ /** 输出配置 */
19
+ extractOutput: CompileTemplateConfig;
20
+ }
21
+
22
+ /** 提取输入配置 */
23
+ declare interface ExtractInputConfig {
24
+ [key: string]: ExtractInputKeyConfig;
25
+ }
26
+
27
+ /** 提取配置 */
28
+ declare type ExtractInputKeyConfig = ExtractInputKeyConfigReg | ExtractInputKeyConfigJsonInject | ExtractInputKeyConfigFixed;
29
+
30
+ /** 提取配置基础 */
31
+ declare interface ExtractInputKeyConfigBase<T extends ExtractTypeEnum> {
32
+ /**
33
+ * 提取类型
34
+ */
35
+ type: T;
36
+ }
37
+
38
+ /** 提取配置固定值 */
39
+ declare type ExtractInputKeyConfigFixed = ExtractInputKeyConfigBase<ExtractTypeEnum.FIXED> & Omit<InjectKeyConfigFixed, "type">;
40
+
41
+ /** 提取配置直接读取 */
42
+ declare interface ExtractInputKeyConfigJsonInject extends ExtractInputKeyConfigBase<ExtractTypeEnum.JSON_INJECT> {
43
+ inject: InjectKeyConfig;
44
+ }
45
+
46
+ /** 提取配置正则表达式 */
47
+ declare interface ExtractInputKeyConfigReg extends ExtractInputKeyConfigBase<ExtractTypeEnum.REG>, Omit<InjectKeyConfigReg, "type" | "sourceKey"> {
48
+ }
49
+
50
+ /** 提取方式枚举 */
51
+ declare enum ExtractTypeEnum {
52
+ /**
53
+ * 正则 类型
54
+ * ---
55
+ * 通过正则匹配后 replace 替换
56
+ */
57
+ REG = "reg",
58
+ /**
59
+ * json注入 类型
60
+ * ---
61
+ * 只限于读取json文件 input\output 均为json文件
62
+ * ---
63
+ * 内部直接调用 @done-coding/cli-inject
64
+ */
65
+ JSON_INJECT = "json-inject",
66
+ /** 固定值 类型 */
67
+ FIXED = "fixed"
68
+ }
69
+
70
+ export { }
package/types/index.d.ts CHANGED
@@ -1,4 +1,118 @@
1
- export { handler } from "./handler";
2
- export { generateFile } from "./utils";
3
- export { crateAsSubcommand } from "./main";
4
- export * from './utils/types';
1
+ import { CliHandlerArgv } from '@done-coding/cli-utils';
2
+ import { CliInfo } from '@done-coding/cli-utils';
3
+ import { CommandModule } from 'yargs';
4
+ import type { CompileTemplateConfig } from '@done-coding/cli-template';
5
+ import type { InitConfigFileOptions } from '@done-coding/cli-utils';
6
+ import type { InjectKeyConfig } from '@done-coding/cli-inject';
7
+ import type { InjectKeyConfigFixed } from '@done-coding/cli-inject';
8
+ import type { InjectKeyConfigReg } from '@done-coding/cli-inject';
9
+ import type { ReadConfigFileOptions } from '@done-coding/cli-utils';
10
+ import { SubCliInfo } from '@done-coding/cli-utils';
11
+
12
+ export declare const commandCliInfo: Omit<CliInfo, "usage">;
13
+
14
+ /** 作为子命令创建 */
15
+ export declare const crateAsSubcommand: () => CommandModule<{}, {}>;
16
+
17
+ export declare interface ExtractConfig {
18
+ /**
19
+ * 输入配置
20
+ * -- 每个源文件 对应一个配置
21
+ */
22
+ extractInput: Record<string, ExtractInputConfig>;
23
+ /** 输出配置 */
24
+ extractOutput: CompileTemplateConfig;
25
+ }
26
+
27
+ /** 提取输入配置 */
28
+ export declare interface ExtractInputConfig {
29
+ [key: string]: ExtractInputKeyConfig;
30
+ }
31
+
32
+ /** 提取配置 */
33
+ export declare type ExtractInputKeyConfig = ExtractInputKeyConfigReg | ExtractInputKeyConfigJsonInject | ExtractInputKeyConfigFixed;
34
+
35
+ /** 提取配置基础 */
36
+ export declare interface ExtractInputKeyConfigBase<T extends ExtractTypeEnum> {
37
+ /**
38
+ * 提取类型
39
+ */
40
+ type: T;
41
+ }
42
+
43
+ /** 提取配置固定值 */
44
+ export declare type ExtractInputKeyConfigFixed = ExtractInputKeyConfigBase<ExtractTypeEnum.FIXED> & Omit<InjectKeyConfigFixed, "type">;
45
+
46
+ /** 提取配置直接读取 */
47
+ export declare interface ExtractInputKeyConfigJsonInject extends ExtractInputKeyConfigBase<ExtractTypeEnum.JSON_INJECT> {
48
+ inject: InjectKeyConfig;
49
+ }
50
+
51
+ /** 提取配置正则表达式 */
52
+ export declare interface ExtractInputKeyConfigReg extends ExtractInputKeyConfigBase<ExtractTypeEnum.REG>, Omit<InjectKeyConfigReg, "type" | "sourceKey"> {
53
+ }
54
+
55
+ /** 提取方式枚举 */
56
+ export declare enum ExtractTypeEnum {
57
+ /**
58
+ * 正则 类型
59
+ * ---
60
+ * 通过正则匹配后 replace 替换
61
+ */
62
+ REG = "reg",
63
+ /**
64
+ * json注入 类型
65
+ * ---
66
+ * 只限于读取json文件 input\output 均为json文件
67
+ * ---
68
+ * 内部直接调用 @done-coding/cli-inject
69
+ */
70
+ JSON_INJECT = "json-inject",
71
+ /** 固定值 类型 */
72
+ FIXED = "fixed"
73
+ }
74
+
75
+ export declare const generateCommandCliInfo: SubCliInfo;
76
+
77
+ /** 将提取的信息生成文件 */
78
+ export declare const generateFile: ({ rootDir, config, }: {
79
+ rootDir?: string | undefined;
80
+ config: ExtractConfig;
81
+ }) => Promise<void>;
82
+
83
+ /** 提取文件命令处理器 */
84
+ export declare const generateHandler: (argv: CliHandlerArgv<GenerateOptions>) => Promise<undefined>;
85
+
86
+ /** 生成模式枚举 */
87
+ export declare enum GenerateModeEnum {
88
+ /** 提取出结果 */
89
+ RESULT = "result",
90
+ /** 提取出模板 */
91
+ TEMPLATE = "template"
92
+ }
93
+
94
+ /** 生成选项 */
95
+ export declare interface GenerateOptions extends ReadConfigFileOptions {
96
+ /** 生成模式 */
97
+ mode?: GenerateModeEnum;
98
+ }
99
+
100
+ export declare const handler: (command: SubcommandEnum, argv: CliHandlerArgv<any>) => Promise<void>;
101
+
102
+ export declare const initCommandCliInfo: SubCliInfo;
103
+
104
+ /** 初始化命令处理器 */
105
+ export declare const initHandler: (argv: CliHandlerArgv<InitOptions>) => Promise<void>;
106
+
107
+ /** 初始化选项 */
108
+ export declare type InitOptions = InitConfigFileOptions;
109
+
110
+ /** 子命令枚举 */
111
+ export declare enum SubcommandEnum {
112
+ /** 初始化提取配置文件 */
113
+ INIT = "init",
114
+ /** 生成文件 */
115
+ GENERATE = "generate"
116
+ }
117
+
118
+ export { }
@@ -1,326 +0,0 @@
1
- #!/usr/bin/env node
2
- import { log as r, _get as O, _curry as D, createSubcommand as h, getConfigFileCommonOptions as j, initHandlerCommon as F, readConfigFile as v, _set as H, createMainCommand as V } from "@done-coding/cli-utils";
3
- import { a as s, e as m, E as u, S as l } from "./types-a1ee6691-9629699d.js";
4
- import R from "node:path";
5
- import C from "node:fs";
6
- import { OutputModeEnum as L, batchCompileHandler as W } from "@done-coding/cli-template";
7
- const N = ({
8
- sourceJson: e,
9
- targetKey: n,
10
- keyConfig: o
11
- }) => {
12
- let t;
13
- typeof o == "string" ? t = {
14
- type: s.FIXED,
15
- value: o
16
- } : t = o;
17
- const { type: a = s.READ } = t;
18
- switch (a) {
19
- case s.REG: {
20
- const { sourceKey: c, pattern: f, replaceValue: E, flags: g } = t, d = new RegExp(f, g ?? void 0), i = O(e, c);
21
- return typeof i == "string" ? i.replace(d, E) : (r.warn(
22
- `${i}不是字符串类型,无法使用正则表达式进行替换,此处将直接返回原值`
23
- ), i);
24
- }
25
- case s.FIXED: {
26
- const { value: c } = t;
27
- return c;
28
- }
29
- case s.READ: {
30
- const { sourceKey: c = n } = t;
31
- return O(e, c);
32
- }
33
- default: {
34
- r.warn(`未知的配置类型${a}`);
35
- return;
36
- }
37
- }
38
- }, $ = {
39
- sourceFilePath: "./package.json",
40
- keyConfigMap: {
41
- name: {
42
- type: s.READ
43
- },
44
- version: {
45
- type: s.READ
46
- },
47
- description: {
48
- type: s.READ
49
- }
50
- },
51
- injectFilePath: "./src/injectInfo.json"
52
- }, w = {
53
- name: "@done-coding/cli-inject",
54
- version: "0.5.8",
55
- description: "信息(JSON)注入命令行工具",
56
- cliConfig: {
57
- namespaceDir: ".done-coding",
58
- moduleName: "inject"
59
- }
60
- }, {
61
- cliConfig: { namespaceDir: U, moduleName: X }
62
- } = w, K = `./${U}/${X}`, x = `${K}.json`, q = () => j({
63
- configPathDefault: x
64
- }), z = async (e) => F($, e, {
65
- onFileGenerated: () => {
66
- r.info("文件生成成功");
67
- }
68
- }), B = () => ({
69
- ...j({
70
- configPathDefault: x
71
- })
72
- }), Q = async ({
73
- rootDir: e = process.cwd(),
74
- config: n = $,
75
- keyConfigMap: o = {}
76
- } = {}) => {
77
- const {
78
- sourceFilePath: t,
79
- keyConfigMap: a,
80
- injectFilePath: c
81
- } = n, f = {
82
- ...a,
83
- ...o
84
- };
85
- if (!t.endsWith(".json"))
86
- return r.error("源文件必须是json"), process.exit(1);
87
- if (!c.endsWith(".json"))
88
- return r.error("注入文件必须是json"), process.exit(1);
89
- const E = R.resolve(e, t), g = C.readFileSync(E, "utf-8"), d = JSON.parse(g), i = Object.entries(f).reduce(
90
- (I, [T, P]) => {
91
- const M = N({ sourceJson: d, targetKey: T, keyConfig: P });
92
- return H(I, T, M), I;
93
- },
94
- {}
95
- ), p = R.resolve(e, c), y = JSON.stringify(i, null, 2);
96
- if (C.existsSync(p)) {
97
- const I = C.readFileSync(p, "utf-8");
98
- if (y === I)
99
- return r.skip("注入文件已存在且内容相同,无需重复注入"), i;
100
- r.stage("文件内容变化,开始覆盖注入文件");
101
- } else
102
- r.stage("开始注入文件");
103
- C.writeFileSync(p, y), r.success(`文件注入成功: ${p}`), r.info(y);
104
- }, Y = async (e) => {
105
- const n = await v(e);
106
- if (!n)
107
- return r.error("配置文件为空"), process.exit(1);
108
- const { rootDir: o } = e;
109
- await Q({ rootDir: o, config: n });
110
- }, J = async (e, n) => e === m.INIT ? z(n) : e === m.GENERATE ? Y(n) : (r.error(`无效的命令: ${e}`), process.exit(1)), {
111
- version: Z,
112
- description: ee,
113
- cliConfig: { moduleName: Te }
114
- } = w, ne = {
115
- command: m.INIT,
116
- describe: "初始化提取配置文件",
117
- options: q(),
118
- handler: D(J)(
119
- m.INIT
120
- )
121
- }, te = {
122
- command: m.GENERATE,
123
- describe: "提取信息",
124
- options: B(),
125
- handler: D(J)(
126
- m.GENERATE
127
- )
128
- };
129
- [ne, te].map(
130
- h
131
- );
132
- const oe = {
133
- name: "${name}",
134
- version: "${version}",
135
- description: "${description}"
136
- }, re = {
137
- extractInput: {
138
- "./package.json": {
139
- name: {
140
- type: u.JSON_INJECT,
141
- inject: {
142
- type: s.READ
143
- }
144
- },
145
- description: {
146
- type: u.JSON_INJECT,
147
- inject: {
148
- type: s.READ
149
- }
150
- },
151
- version: {
152
- type: u.JSON_INJECT,
153
- inject: {
154
- type: s.READ
155
- }
156
- }
157
- },
158
- "./README.md": {
159
- docDescription: {
160
- type: u.REG,
161
- pattern: /#\s*([^`])+```([^`]+)```/.source,
162
- replaceValue: ""
163
- },
164
- usage: {
165
- type: u.REG,
166
- pattern: /##\s*使用\s*```([^`]+)```/.source,
167
- replaceValue: "$1"
168
- }
169
- }
170
- },
171
- extractOutput: {
172
- list: [
173
- {
174
- mode: L.OVERWRITE,
175
- inputData: JSON.stringify(oe, null, 2),
176
- output: "./src/extractInfo.json"
177
- }
178
- ]
179
- }
180
- }, S = {
181
- name: "@done-coding/cli-extract",
182
- version: "0.1.8",
183
- description: "信息提取命令行工具",
184
- cliConfig: {
185
- namespaceDir: ".done-coding",
186
- moduleName: "extract"
187
- }
188
- }, {
189
- cliConfig: { namespaceDir: se, moduleName: ce }
190
- } = S, ie = `./${se}/${ce}`, b = `${ie}.json5`, ae = () => j({
191
- configPathDefault: b
192
- }), ue = async (e) => F(re, e, {
193
- onFileGenerated: () => {
194
- r.info("文件生成成功");
195
- }
196
- }), de = ({
197
- input: e,
198
- rootDir: n
199
- }) => {
200
- const o = R.resolve(n, e), t = C.readFileSync(o, "utf-8");
201
- return e.endsWith(".json") || e.endsWith(".json5") ? JSON.parse(t) : t;
202
- }, pe = (e) => typeof e == "object" && !!e, me = ({
203
- content: e,
204
- targetKey: n,
205
- keyConfig: o
206
- }) => {
207
- const { type: t, ...a } = o;
208
- switch (t) {
209
- case u.REG: {
210
- if (typeof e != "string")
211
- throw new Error(`${t} 类型的keyConfig需要content为字符串`);
212
- return N({
213
- sourceJson: {
214
- [n]: e
215
- },
216
- targetKey: n,
217
- keyConfig: {
218
- ...a,
219
- type: s.REG,
220
- sourceKey: n
221
- }
222
- });
223
- }
224
- case u.JSON_INJECT: {
225
- if (!pe(e))
226
- throw new Error(`${t} 类型的keyConfig需要content为json`);
227
- return N({
228
- sourceJson: e,
229
- targetKey: n,
230
- keyConfig: o.inject
231
- });
232
- }
233
- case u.FIXED:
234
- return N({
235
- sourceJson: {},
236
- targetKey: n,
237
- keyConfig: {
238
- ...a,
239
- type: s.FIXED
240
- }
241
- });
242
- default:
243
- throw new Error(`不支持的类型${t}`);
244
- }
245
- }, le = () => ({
246
- ...j({
247
- configPathDefault: b
248
- })
249
- }), fe = async ({
250
- rootDir: e = process.cwd(),
251
- config: n
252
- }) => {
253
- const { extractInput: o, extractOutput: t } = n, a = Object.entries(o).reduce(
254
- (c, [f, E]) => {
255
- const g = de({
256
- rootDir: e,
257
- input: f
258
- });
259
- return Object.entries(E).reduce(
260
- (d, [i, p]) => {
261
- const y = me({
262
- content: g,
263
- targetKey: i,
264
- keyConfig: p
265
- });
266
- return d[i] = y, d;
267
- },
268
- c
269
- );
270
- },
271
- {}
272
- );
273
- await W(
274
- {
275
- rootDir: e,
276
- extraEnvData: a
277
- },
278
- t
279
- );
280
- }, Ee = async (e) => {
281
- const n = await v(e);
282
- if (!n)
283
- return r.error("配置文件为空"), process.exit(1);
284
- const { rootDir: o } = e;
285
- await fe({ rootDir: o, config: n });
286
- }, G = async (e, n) => e === l.INIT ? ue(n) : e === l.GENERATE ? Ee(n) : (r.error(`无效的命令: ${e}`), process.exit(1)), {
287
- version: ge,
288
- description: ye,
289
- cliConfig: { moduleName: A }
290
- } = S, Ce = {
291
- command: l.INIT,
292
- describe: "初始化提取配置文件",
293
- options: ae(),
294
- handler: D(G)(
295
- l.INIT
296
- )
297
- }, Ie = {
298
- command: l.GENERATE,
299
- describe: "提取信息",
300
- options: le(),
301
- handler: D(G)(
302
- l.GENERATE
303
- )
304
- }, _ = {
305
- describe: ye,
306
- version: ge,
307
- subcommands: [Ce, Ie].map(
308
- h
309
- ),
310
- demandCommandCount: 1
311
- }, k = (e = !1) => {
312
- const n = e ? A : void 0, o = `$0${e ? ` ${A}` : ""} <command> [options]`;
313
- return { command: n, usage: o };
314
- }, Oe = async () => V({
315
- ..._,
316
- ...k()
317
- }), Ae = () => h({
318
- ..._,
319
- ...k(!0)
320
- });
321
- export {
322
- Ae as a,
323
- Oe as c,
324
- fe as g,
325
- G as h
326
- };
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env node
2
- var i = /* @__PURE__ */ ((e) => (e.INIT = "init", e.GENERATE = "generate", e))(i || {}), E = /* @__PURE__ */ ((e) => (e.REG = "reg", e.JSON_INJECT = "json-inject", e.FIXED = "fixed", e))(E || {}), n = /* @__PURE__ */ ((e) => (e.INIT = "init", e.GENERATE = "generate", e))(n || {}), r = /* @__PURE__ */ ((e) => (e.REG = "reg", e.FIXED = "fixed", e.READ = "read", e))(r || {});
3
- export {
4
- E,
5
- i as S,
6
- r as a,
7
- n as e
8
- };
@@ -1,5 +0,0 @@
1
- import type { GenerateOptions, 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 | GenerateOptions>) => Promise<void>;
@@ -1,5 +0,0 @@
1
- import { type ExtractConfig } from "../utils";
2
- /** done-coding 系列cli 注入信息 */
3
- export declare const doneCodingSeriesCliInjectInfo: ExtractConfig;
4
- /** done-coding 系列cli 别名发布信息 */
5
- export declare const doneCodingPublishAliasInfo: ExtractConfig;
@@ -1,11 +0,0 @@
1
- declare const _default: {
2
- "name": "@done-coding/cli-extract",
3
- "version": "0.1.8",
4
- "description": "信息提取命令行工具",
5
- "cliConfig": {
6
- "namespaceDir": ".done-coding",
7
- "moduleName": "extract"
8
- }
9
- };
10
-
11
- export default _default;
@@ -1,2 +0,0 @@
1
- import { type ExtractConfig } from '../utils';
2
- export declare const config: ExtractConfig;
package/types/main.d.ts DELETED
@@ -1,9 +0,0 @@
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,11 +0,0 @@
1
- import { type CliHandlerArgv, type CliInfo } from "@done-coding/cli-utils";
2
- import type { ExtractConfig, GenerateOptions } from "./types";
3
- /** 获取生成命令选项 */
4
- export declare const getGenerateOptions: () => CliInfo["options"];
5
- /** 将提取的信息生成文件 */
6
- export declare const generateFile: ({ rootDir, config, }: {
7
- rootDir?: string | undefined;
8
- config: ExtractConfig;
9
- }) => Promise<void>;
10
- /** 提取文件命令处理器 */
11
- export declare const generateHandler: (argv: CliHandlerArgv<GenerateOptions>) => Promise<undefined>;
@@ -1,4 +0,0 @@
1
- export * from "./types";
2
- export * from "./init";
3
- export * from "./generate";
4
- export * from "./path";
@@ -1,7 +0,0 @@
1
- /// <reference types="yargs" />
2
- import type { CliHandlerArgv } from "@done-coding/cli-utils";
3
- import type { InitOptions } from "./types";
4
- /** 获取初始化选项 */
5
- export declare const getInitOptions: () => Record<keyof import("@done-coding/cli-utils").ConfigFileCommonOptions, import("yargs").Options>;
6
- /** 初始化命令处理器 */
7
- export declare const initHandler: (argv: CliHandlerArgv<InitOptions>) => Promise<void>;
@@ -1,4 +0,0 @@
1
- /** 模块配置相对路径 */
2
- export declare const MODULE_CONFIG_RELATIVE_PATH: string;
3
- /** 模块默认配置文件相对路径 */
4
- export declare const MODULE_DEFAULT_CONFIG_RELATIVE_PATH: string;
@@ -1,14 +0,0 @@
1
- import { type ExtractInputKeyConfig } from "./types";
2
- /** 内容解析 */
3
- export declare const contentResolve: ({ input, rootDir, }: {
4
- input: string;
5
- rootDir: string;
6
- }) => string | Record<string, any>;
7
- /** 内容是否为对象 */
8
- export declare const contentIsObj: (content: ReturnType<typeof contentResolve>) => boolean;
9
- /** 配置解析 */
10
- export declare const keyConfigResolve: ({ content, targetKey, keyConfig, }: {
11
- content: ReturnType<typeof contentResolve>;
12
- targetKey: string;
13
- keyConfig: ExtractInputKeyConfig;
14
- }) => any;
@@ -1,64 +0,0 @@
1
- import type { InitConfigFileOptions, ReadConfigFileOptions } from "@done-coding/cli-utils";
2
- import type { CompileTemplateConfig } from "@done-coding/cli-template";
3
- import type { InjectKeyConfig, InjectKeyConfigFixed, InjectKeyConfigReg } from "@done-coding/cli-inject";
4
- /** 子命令枚举 */
5
- export declare enum SubcommandEnum {
6
- /** 初始化提取配置文件 */
7
- INIT = "init",
8
- /** 生成文件 */
9
- GENERATE = "generate"
10
- }
11
- /** 提取方式枚举 */
12
- export declare enum ExtractTypeEnum {
13
- /**
14
- * 正则 类型
15
- * ---
16
- * 通过正则匹配后 replace 替换
17
- */
18
- REG = "reg",
19
- /**
20
- * json注入 类型
21
- * ---
22
- * 只限于读取json文件 input\output 均为json文件
23
- * ---
24
- * 内部直接调用 @done-coding/cli-inject
25
- */
26
- JSON_INJECT = "json-inject",
27
- /** 固定值 类型 */
28
- FIXED = "fixed"
29
- }
30
- /** 初始化选项 */
31
- export type InitOptions = InitConfigFileOptions;
32
- /** 提取配置基础 */
33
- export interface ExtractInputKeyConfigBase<T extends ExtractTypeEnum> {
34
- /**
35
- * 提取类型
36
- */
37
- type: T;
38
- }
39
- /** 提取配置正则表达式 */
40
- export interface ExtractInputKeyConfigReg extends ExtractInputKeyConfigBase<ExtractTypeEnum.REG>, Omit<InjectKeyConfigReg, "type" | "sourceKey"> {
41
- }
42
- /** 提取配置直接读取 */
43
- export interface ExtractInputKeyConfigJsonInject extends ExtractInputKeyConfigBase<ExtractTypeEnum.JSON_INJECT> {
44
- inject: InjectKeyConfig;
45
- }
46
- /** 提取配置固定值 */
47
- export type ExtractInputKeyConfigFixed = ExtractInputKeyConfigBase<ExtractTypeEnum.FIXED> & Omit<InjectKeyConfigFixed, "type">;
48
- /** 提取配置 */
49
- export type ExtractInputKeyConfig = ExtractInputKeyConfigReg | ExtractInputKeyConfigJsonInject | ExtractInputKeyConfigFixed;
50
- /** 提取输入配置 */
51
- export interface ExtractInputConfig {
52
- [key: string]: ExtractInputKeyConfig;
53
- }
54
- export interface ExtractConfig {
55
- /**
56
- * 输入配置
57
- * -- 每个源文件 对应一个配置
58
- */
59
- extractInput: Record<string, ExtractInputConfig>;
60
- /** 输出配置 */
61
- extractOutput: CompileTemplateConfig;
62
- }
63
- /** 生成选项 */
64
- export type GenerateOptions = ReadConfigFileOptions;