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