dc-inject 0.5.8-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/README.md +7 -0
- package/es/cli.mjs +7 -0
- package/es/helpers.mjs +30 -0
- package/es/index-124e6e6f.js +150 -0
- package/es/index.mjs +14 -0
- package/es/types-a1ee6691.js +6 -0
- package/package.json +59 -0
- package/types/cli.d.ts +2 -0
- package/types/handler.d.ts +5 -0
- package/types/helpers-assets/done-coding.d.ts +6 -0
- package/types/helpers.d.ts +1 -0
- package/types/index.d.ts +4 -0
- package/types/injectInfo.json.d.ts +11 -0
- package/types/json/default.d.ts +6 -0
- package/types/main.d.ts +9 -0
- package/types/utils/generate.d.ts +15 -0
- package/types/utils/index.d.ts +5 -0
- package/types/utils/init.d.ts +7 -0
- package/types/utils/path.d.ts +4 -0
- package/types/utils/resolve.d.ts +7 -0
- package/types/utils/types.d.ts +87 -0
package/README.md
ADDED
package/es/cli.mjs
ADDED
package/es/helpers.mjs
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { I as e } from "./types-a1ee6691.js";
|
|
3
|
+
const o = {
|
|
4
|
+
sourceFilePath: "./package.json",
|
|
5
|
+
keyConfigMap: {
|
|
6
|
+
name: {
|
|
7
|
+
type: e.READ
|
|
8
|
+
},
|
|
9
|
+
version: {
|
|
10
|
+
type: e.READ
|
|
11
|
+
},
|
|
12
|
+
description: {
|
|
13
|
+
type: e.READ
|
|
14
|
+
},
|
|
15
|
+
"cliConfig.namespaceDir": {
|
|
16
|
+
type: e.FIXED,
|
|
17
|
+
value: ".done-coding"
|
|
18
|
+
},
|
|
19
|
+
"cliConfig.moduleName": {
|
|
20
|
+
type: e.REG,
|
|
21
|
+
sourceKey: "name",
|
|
22
|
+
pattern: /@done-coding\/cli-([a-zA-Z0-9]+(-[a-zA-Z0-9]+)*)/.source,
|
|
23
|
+
replaceValue: "$1"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
injectFilePath: "./src/injectInfo.json"
|
|
27
|
+
};
|
|
28
|
+
export {
|
|
29
|
+
o as doneCodingCliConfig
|
|
30
|
+
};
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { I as c, S as a } from "./types-a1ee6691.js";
|
|
3
|
+
import { log as n, _get as I, getConfigFileCommonOptions as h, initHandlerCommon as _, _set as b, readConfigFile as k, _curry as A, createSubcommand as N, createMainCommand as w } from "@done-coding/cli-utils";
|
|
4
|
+
import F from "node:path";
|
|
5
|
+
import m from "node:fs";
|
|
6
|
+
const G = ({
|
|
7
|
+
sourceJson: e,
|
|
8
|
+
targetKey: o,
|
|
9
|
+
keyConfig: t
|
|
10
|
+
}) => {
|
|
11
|
+
let s;
|
|
12
|
+
typeof t == "string" ? s = {
|
|
13
|
+
type: c.FIXED,
|
|
14
|
+
value: t
|
|
15
|
+
} : s = t;
|
|
16
|
+
const { type: l = c.READ } = s;
|
|
17
|
+
switch (l) {
|
|
18
|
+
case c.REG: {
|
|
19
|
+
const { sourceKey: r, pattern: d, replaceValue: p, flags: g } = s, E = new RegExp(d, g ?? void 0), i = I(e, r);
|
|
20
|
+
return typeof i == "string" ? i.replace(E, p) : (n.warn(
|
|
21
|
+
`${i}不是字符串类型,无法使用正则表达式进行替换,此处将直接返回原值`
|
|
22
|
+
), i);
|
|
23
|
+
}
|
|
24
|
+
case c.FIXED: {
|
|
25
|
+
const { value: r } = s;
|
|
26
|
+
return r;
|
|
27
|
+
}
|
|
28
|
+
case c.READ: {
|
|
29
|
+
const { sourceKey: r = o } = s;
|
|
30
|
+
return I(e, r);
|
|
31
|
+
}
|
|
32
|
+
default: {
|
|
33
|
+
n.warn(`未知的配置类型${l}`);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}, D = {
|
|
38
|
+
sourceFilePath: "./package.json",
|
|
39
|
+
keyConfigMap: {
|
|
40
|
+
name: {
|
|
41
|
+
type: c.READ
|
|
42
|
+
},
|
|
43
|
+
version: {
|
|
44
|
+
type: c.READ
|
|
45
|
+
},
|
|
46
|
+
description: {
|
|
47
|
+
type: c.READ
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
injectFilePath: "./src/injectInfo.json"
|
|
51
|
+
}, R = {
|
|
52
|
+
name: "@done-coding/cli-inject",
|
|
53
|
+
version: "0.5.8-alpha.0",
|
|
54
|
+
description: "信息(JSON)注入命令行工具",
|
|
55
|
+
cliConfig: {
|
|
56
|
+
namespaceDir: ".done-coding",
|
|
57
|
+
moduleName: "inject"
|
|
58
|
+
}
|
|
59
|
+
}, {
|
|
60
|
+
cliConfig: { namespaceDir: M, moduleName: x }
|
|
61
|
+
} = R, H = `./${M}/${x}`, T = `${H}.json`, L = () => h({
|
|
62
|
+
configPathDefault: T
|
|
63
|
+
}), J = async (e) => _(D, e, {
|
|
64
|
+
onFileGenerated: () => {
|
|
65
|
+
n.info("文件生成成功");
|
|
66
|
+
}
|
|
67
|
+
}), U = () => ({
|
|
68
|
+
...h({
|
|
69
|
+
configPathDefault: T
|
|
70
|
+
})
|
|
71
|
+
}), V = async ({
|
|
72
|
+
rootDir: e = process.cwd(),
|
|
73
|
+
config: o = D,
|
|
74
|
+
keyConfigMap: t = {}
|
|
75
|
+
} = {}) => {
|
|
76
|
+
const {
|
|
77
|
+
sourceFilePath: s,
|
|
78
|
+
keyConfigMap: l,
|
|
79
|
+
injectFilePath: r
|
|
80
|
+
} = o, d = {
|
|
81
|
+
...l,
|
|
82
|
+
...t
|
|
83
|
+
};
|
|
84
|
+
if (!s.endsWith(".json"))
|
|
85
|
+
return n.error("源文件必须是json"), process.exit(1);
|
|
86
|
+
if (!r.endsWith(".json"))
|
|
87
|
+
return n.error("注入文件必须是json"), process.exit(1);
|
|
88
|
+
const p = F.resolve(e, s), g = m.readFileSync(p, "utf-8"), E = JSON.parse(g), i = Object.entries(d).reduce(
|
|
89
|
+
(f, [C, $]) => {
|
|
90
|
+
const P = G({ sourceJson: E, targetKey: C, keyConfig: $ });
|
|
91
|
+
return b(f, C, P), f;
|
|
92
|
+
},
|
|
93
|
+
{}
|
|
94
|
+
), u = F.resolve(e, r), y = JSON.stringify(i, null, 2);
|
|
95
|
+
if (m.existsSync(u)) {
|
|
96
|
+
const f = m.readFileSync(u, "utf-8");
|
|
97
|
+
if (y === f)
|
|
98
|
+
return n.skip("注入文件已存在且内容相同,无需重复注入"), i;
|
|
99
|
+
n.stage("文件内容变化,开始覆盖注入文件");
|
|
100
|
+
} else
|
|
101
|
+
n.stage("开始注入文件");
|
|
102
|
+
m.writeFileSync(u, y), n.success(`文件注入成功: ${u}`), n.info(y);
|
|
103
|
+
}, K = async (e) => {
|
|
104
|
+
const o = await k(e);
|
|
105
|
+
if (!o)
|
|
106
|
+
return n.error("配置文件为空"), process.exit(1);
|
|
107
|
+
const { rootDir: t } = e;
|
|
108
|
+
await V({ rootDir: t, config: o });
|
|
109
|
+
}, v = async (e, o) => e === a.INIT ? J(o) : e === a.GENERATE ? K(o) : (n.error(`无效的命令: ${e}`), process.exit(1)), {
|
|
110
|
+
version: W,
|
|
111
|
+
description: X,
|
|
112
|
+
cliConfig: { moduleName: j }
|
|
113
|
+
} = R, q = {
|
|
114
|
+
command: a.INIT,
|
|
115
|
+
describe: "初始化提取配置文件",
|
|
116
|
+
options: L(),
|
|
117
|
+
handler: A(v)(
|
|
118
|
+
a.INIT
|
|
119
|
+
)
|
|
120
|
+
}, z = {
|
|
121
|
+
command: a.GENERATE,
|
|
122
|
+
describe: "提取信息",
|
|
123
|
+
options: U(),
|
|
124
|
+
handler: A(v)(
|
|
125
|
+
a.GENERATE
|
|
126
|
+
)
|
|
127
|
+
}, O = {
|
|
128
|
+
describe: X,
|
|
129
|
+
version: W,
|
|
130
|
+
subcommands: [q, z].map(
|
|
131
|
+
N
|
|
132
|
+
),
|
|
133
|
+
demandCommandCount: 1
|
|
134
|
+
}, S = (e = !1) => {
|
|
135
|
+
const o = e ? j : void 0, t = `$0${e ? ` ${j}` : ""} <command> [options]`;
|
|
136
|
+
return { command: o, usage: t };
|
|
137
|
+
}, ee = async () => w({
|
|
138
|
+
...O,
|
|
139
|
+
...S()
|
|
140
|
+
}), ne = () => N({
|
|
141
|
+
...O,
|
|
142
|
+
...S(!0)
|
|
143
|
+
});
|
|
144
|
+
export {
|
|
145
|
+
ne as a,
|
|
146
|
+
ee as c,
|
|
147
|
+
V as g,
|
|
148
|
+
v as h,
|
|
149
|
+
G as k
|
|
150
|
+
};
|
package/es/index.mjs
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { a as r, g as s, h as t, k as i } from "./index-124e6e6f.js";
|
|
3
|
+
import { I as u, S as c } from "./types-a1ee6691.js";
|
|
4
|
+
import "@done-coding/cli-utils";
|
|
5
|
+
import "node:path";
|
|
6
|
+
import "node:fs";
|
|
7
|
+
export {
|
|
8
|
+
u as InjectTypeEnum,
|
|
9
|
+
c as SubcommandEnum,
|
|
10
|
+
r as crateAsSubcommand,
|
|
11
|
+
s as generateFile,
|
|
12
|
+
t as handler,
|
|
13
|
+
i as keyConfigResolve
|
|
14
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "dc-inject",
|
|
3
|
+
"version": "0.5.8-alpha.0",
|
|
4
|
+
"description": "信息(JSON)注入命令行工具",
|
|
5
|
+
"private": false,
|
|
6
|
+
"module": "es/index.mjs",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"types": "types/index.d.ts",
|
|
9
|
+
"bin": "es/cli.mjs",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": "./es/index.mjs"
|
|
13
|
+
},
|
|
14
|
+
"./helpers": {
|
|
15
|
+
"import": "./es/helpers.mjs",
|
|
16
|
+
"types": "./types/helpers.d.ts"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"typesVersions": {
|
|
20
|
+
"*": {
|
|
21
|
+
"helpers": [
|
|
22
|
+
"./types/helpers.d.ts"
|
|
23
|
+
]
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"es",
|
|
28
|
+
"lib",
|
|
29
|
+
"types"
|
|
30
|
+
],
|
|
31
|
+
"scripts": {},
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "https://gitee.com/justsosu/done-coding-cli.git",
|
|
35
|
+
"directory": "packages/inject"
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public",
|
|
39
|
+
"registry": "https://registry.npmjs.org/"
|
|
40
|
+
},
|
|
41
|
+
"author": "JustSoSu",
|
|
42
|
+
"license": "MIT",
|
|
43
|
+
"sideEffects": false,
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@types/node": "^18.0.0",
|
|
46
|
+
"@types/yargs": "^17.0.28",
|
|
47
|
+
"rimraf": "^6.0.1",
|
|
48
|
+
"typescript": "^5.2.2",
|
|
49
|
+
"vite": "^4.4.11",
|
|
50
|
+
"vite-plugin-dts": "^3.6.0"
|
|
51
|
+
},
|
|
52
|
+
"engines": {
|
|
53
|
+
"node": ">=18.0.0"
|
|
54
|
+
},
|
|
55
|
+
"dependencies": {
|
|
56
|
+
"@done-coding/cli-utils": "^0.6.0-alpha.0"
|
|
57
|
+
},
|
|
58
|
+
"gitHead": "5879161383e32e7bed351d71d3ef607d81dc021a"
|
|
59
|
+
}
|
package/types/cli.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
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>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './helpers-assets/done-coding';
|
package/types/index.d.ts
ADDED
package/types/main.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
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,15 @@
|
|
|
1
|
+
/** 考虑本包会使用当前文件源码 避免不识别@ 此处用相对路径 */
|
|
2
|
+
/** 考虑本包会使用当前文件源码 避免不识别@ 此处用相对路径 */
|
|
3
|
+
/** 考虑本包会使用当前文件源码 避免不识别@ 此处用相对路径 */
|
|
4
|
+
import { type CliHandlerArgv, type CliInfo } from "@done-coding/cli-utils";
|
|
5
|
+
import type { InjectConfig, GenerateOptions } from "./types";
|
|
6
|
+
/** 获取生成命令选项 */
|
|
7
|
+
export declare const getGenerateOptions: () => CliInfo["options"];
|
|
8
|
+
/** 生成注入信息文件 */
|
|
9
|
+
export declare const generateFile: ({ rootDir, config, keyConfigMap: extractKeyConfigMap, }?: {
|
|
10
|
+
rootDir?: string | undefined;
|
|
11
|
+
config?: InjectConfig | undefined;
|
|
12
|
+
keyConfigMap?: Record<string, import("./types").InjectKeyConfig> | undefined;
|
|
13
|
+
}) => Promise<{} | undefined>;
|
|
14
|
+
/** 提取文件命令处理器 */
|
|
15
|
+
export declare const generateHandler: (argv: CliHandlerArgv<GenerateOptions>) => Promise<undefined>;
|
|
@@ -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,87 @@
|
|
|
1
|
+
import type { InitConfigFileOptions, ReadConfigFileOptions } from "@done-coding/cli-utils";
|
|
2
|
+
/** 子命令枚举 */
|
|
3
|
+
export declare enum SubcommandEnum {
|
|
4
|
+
/** 初始化提取配置文件 */
|
|
5
|
+
INIT = "init",
|
|
6
|
+
/** 生成文件 */
|
|
7
|
+
GENERATE = "generate"
|
|
8
|
+
}
|
|
9
|
+
/** 配置类型枚举 */
|
|
10
|
+
export declare enum InjectTypeEnum {
|
|
11
|
+
/**
|
|
12
|
+
* 正则表达式 类型
|
|
13
|
+
*/
|
|
14
|
+
REG = "reg",
|
|
15
|
+
/**
|
|
16
|
+
* 固定值 类型
|
|
17
|
+
*/
|
|
18
|
+
FIXED = "fixed",
|
|
19
|
+
/**
|
|
20
|
+
* 读取 类型
|
|
21
|
+
*/
|
|
22
|
+
READ = "read"
|
|
23
|
+
}
|
|
24
|
+
/** 初始化选项 */
|
|
25
|
+
export type InitOptions = InitConfigFileOptions;
|
|
26
|
+
/** 注入配置基础 */
|
|
27
|
+
export interface InjectKeyConfigBase<T extends InjectTypeEnum> {
|
|
28
|
+
/**
|
|
29
|
+
* 提取类型
|
|
30
|
+
*/
|
|
31
|
+
type: T;
|
|
32
|
+
}
|
|
33
|
+
/** 注入配置-正则类型 */
|
|
34
|
+
export interface InjectKeyConfigReg extends InjectKeyConfigBase<InjectTypeEnum.REG> {
|
|
35
|
+
/**
|
|
36
|
+
* 正则表达式字符串
|
|
37
|
+
* ----
|
|
38
|
+
* 不带flags
|
|
39
|
+
*/
|
|
40
|
+
pattern: string;
|
|
41
|
+
/** 正则匹配的 flags */
|
|
42
|
+
flags?: string;
|
|
43
|
+
/**
|
|
44
|
+
* 替换值
|
|
45
|
+
* ---
|
|
46
|
+
* replace 第二个参数
|
|
47
|
+
*/
|
|
48
|
+
replaceValue: string;
|
|
49
|
+
/** 源key */
|
|
50
|
+
sourceKey: string;
|
|
51
|
+
}
|
|
52
|
+
/** 注入配置-固定值类型 */
|
|
53
|
+
export interface InjectKeyConfigFixed extends InjectKeyConfigBase<InjectTypeEnum.FIXED> {
|
|
54
|
+
/** 值 */
|
|
55
|
+
value: string;
|
|
56
|
+
}
|
|
57
|
+
/** 注入配置-读取类型 */
|
|
58
|
+
export interface InjectKeyConfigRead extends InjectKeyConfigBase<InjectTypeEnum.READ> {
|
|
59
|
+
/** 源key */
|
|
60
|
+
sourceKey?: string;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* 注入配置
|
|
64
|
+
* ---
|
|
65
|
+
* 为 string 时 解析为 InjectKeyConfigFixed, 其中InjectKeyConfigFixed.value = string
|
|
66
|
+
*/
|
|
67
|
+
export type InjectKeyConfig = InjectKeyConfigReg | (InjectKeyConfigFixed | string) | InjectKeyConfigRead;
|
|
68
|
+
/** @deprecated */
|
|
69
|
+
export interface Options {
|
|
70
|
+
/** json文件相对路径 */
|
|
71
|
+
sourceJsonFilePath: string;
|
|
72
|
+
/** 注入的key路径 */
|
|
73
|
+
injectKeyPath: string[];
|
|
74
|
+
/** 注入信息文件路径 */
|
|
75
|
+
injectInfoFilePath: string;
|
|
76
|
+
}
|
|
77
|
+
/** 注入配置 */
|
|
78
|
+
export interface InjectConfig {
|
|
79
|
+
/** json文件相对路径 */
|
|
80
|
+
sourceFilePath: string;
|
|
81
|
+
/** 注入的key路径 */
|
|
82
|
+
keyConfigMap: Record<string, InjectKeyConfig>;
|
|
83
|
+
/** 注入信息文件路径 */
|
|
84
|
+
injectFilePath: string;
|
|
85
|
+
}
|
|
86
|
+
/** 生成选项 */
|
|
87
|
+
export type GenerateOptions = ReadConfigFileOptions;
|