@done-coding/cli-inject 0.2.2 → 0.3.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 CHANGED
File without changes
package/es/cli.mjs CHANGED
File without changes
package/es/handler.mjs CHANGED
@@ -1,30 +1,38 @@
1
1
  #!/usr/bin/env node
2
2
  import u from "node:path";
3
- import r from "node:fs";
4
- import d from "lodash.get";
5
- import j from "lodash.set";
3
+ import t from "node:fs";
4
+ import S from "lodash.get";
5
+ import v from "lodash.set";
6
6
  import e from "chalk";
7
- const P = async (a) => {
8
- const { sourceJsonFilePath: c, injectKeyPath: g, injectInfoFilePath: l } = a, i = process.cwd();
7
+ import { getKey as J, paramsResolve as P } from "./utils/resolve.mjs";
8
+ const O = async (a) => {
9
+ const { sourceJsonFilePath: c, injectKeyPath: f, injectInfoFilePath: l } = a, i = process.cwd();
9
10
  if (!c.endsWith(".json"))
10
11
  return console.log(e.red("源文件必须是json")), process.exit(1);
11
12
  if (!l.endsWith(".json"))
12
13
  return console.log(e.red("注入文件必须是json")), process.exit(1);
13
- const h = u.resolve(i, c), p = JSON.parse(
14
- r.readFileSync(h, "utf-8")
15
- ), s = g.reduce((n, f) => (j(n, f, d(p, f)), n), {}), o = u.resolve(i, l), t = JSON.stringify(s, null, 2);
16
- if (r.existsSync(o)) {
17
- const n = r.readFileSync(o, "utf-8");
18
- if (t === n)
14
+ const g = u.resolve(i, c), m = t.readFileSync(g, "utf-8"), p = JSON.parse(m), s = f.reduce(
15
+ (n, h) => {
16
+ const { key: d, targetKey: j, paramsList: y } = J(h), F = S(p, d), I = P({
17
+ valueInit: F,
18
+ paramsList: y
19
+ });
20
+ return v(n, j, I), n;
21
+ },
22
+ {}
23
+ ), o = u.resolve(i, l), r = JSON.stringify(s, null, 2);
24
+ if (t.existsSync(o)) {
25
+ const n = t.readFileSync(o, "utf-8");
26
+ if (r === n)
19
27
  return console.log(e.green("注入文件已存在且内容相同,无需重复注入")), s;
20
28
  console.log(e.green("文件内容变化,开始覆盖注入文件"));
21
29
  } else
22
30
  console.log(e.green("开始注入文件"));
23
- return r.writeFileSync(o, t), console.log(
31
+ return t.writeFileSync(o, r), console.log(
24
32
  e.green(`文件注入成功: ${o}`),
25
- e.blue(t)
33
+ e.blue(r)
26
34
  ), s;
27
35
  };
28
36
  export {
29
- P as handler
37
+ O as handler
30
38
  };
package/es/index.mjs CHANGED
File without changes
@@ -1,8 +1,12 @@
1
1
  #!/usr/bin/env node
2
2
  const n = {
3
- version: "0.2.2",
3
+ version: "0.3.0",
4
4
  name: "@done-coding/cli-inject",
5
- description: "信息注入命令行工具"
5
+ description: "信息注入命令行工具",
6
+ cliConfig: {
7
+ namespaceDir: ".done-coding",
8
+ commandName: "inject"
9
+ }
6
10
  };
7
11
  export {
8
12
  n as default
package/es/main.mjs CHANGED
@@ -1,9 +1,9 @@
1
1
  #!/usr/bin/env node
2
2
  import c from "yargs";
3
3
  import { hideBin as l } from "yargs/helpers";
4
- import { handler as n } from "./handler.mjs";
4
+ import { handler as i } from "./handler.mjs";
5
5
  import s from "chalk";
6
- import i from "./injectInfo.json.mjs";
6
+ import n from "./injectInfo.json.mjs";
7
7
  const m = () => ({
8
8
  sourceJsonFilePath: {
9
9
  type: "string",
@@ -23,21 +23,21 @@ const m = () => ({
23
23
  describe: "注入信息文件路径",
24
24
  default: "./src/injectInfo.json"
25
25
  }
26
- }), t = "inject", d = (e, o) => {
26
+ }), t = n.cliConfig.commandName, d = (e, o) => {
27
27
  console.log(e ? s.red(e) : s.red(o.message)), process.exit(1);
28
- }, p = i.description, g = `Usage: $0 ${t} [options]`, f = "Usage: $0 [options]", r = (e, o) => {
29
- const a = m();
30
- return e.strict().usage(o).help("help").version(i.version).alias("v", "version").alias("h", "help").options(a).fail(d).argv;
31
- }, h = (e) => r(e, g), k = {
28
+ }, p = n.description, g = `Usage: $0 ${t} [options]`, f = "Usage: $0 [options]", a = (e, o) => {
29
+ const r = m();
30
+ return e.strict().usage(o).help("help").version(n.version).alias("v", "version").alias("h", "help").options(r).fail(d).argv;
31
+ }, h = (e) => a(e, g), C = {
32
32
  command: t,
33
33
  describe: p,
34
34
  builder: h,
35
- handler: n
36
- }, C = async () => {
37
- const e = c(l(process.argv)), o = await r(e, f);
38
- return n(o);
35
+ handler: i
36
+ }, k = async () => {
37
+ const e = c(l(process.argv)), o = await a(e, f);
38
+ return i(o);
39
39
  };
40
40
  export {
41
- k as command,
42
- C as createCli
41
+ C as command,
42
+ k as createCli
43
43
  };
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env node
2
+ import p from "chalk";
3
+ const l = /(?<!\/):/, y = (e) => {
4
+ const [t, s = t, ...r] = e.split(l);
5
+ return {
6
+ key: t,
7
+ /** 目标key */
8
+ targetKey: s,
9
+ /** 参数列表 */
10
+ paramsList: r
11
+ };
12
+ }, f = ({
13
+ valueInit: e,
14
+ paramsList: t
15
+ }) => {
16
+ const [s, ...r] = t;
17
+ switch (s) {
18
+ case "REG": {
19
+ const [o, n, c] = r, a = new RegExp(o, c ?? void 0);
20
+ return typeof e == "string" ? e.replace(a, n) : (console.log(
21
+ p.yellow(
22
+ `${e}不是字符串类型,无法使用正则表达式进行替换,此处将直接返回原值`
23
+ )
24
+ ), e);
25
+ }
26
+ case "VALUE": {
27
+ const [o] = r;
28
+ return o;
29
+ }
30
+ default:
31
+ return e;
32
+ }
33
+ };
34
+ export {
35
+ y as getKey,
36
+ f as paramsResolve
37
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@done-coding/cli-inject",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "description": "信息注入命令行工具",
5
5
  "private": false,
6
6
  "module": "es/index.mjs",
@@ -61,5 +61,5 @@
61
61
  "prompts": "^2.4.2",
62
62
  "yargs": "^17.7.2"
63
63
  },
64
- "gitHead": "e2dc7c4b0fa74aa83dc6173416aaffe0a0a445ad"
64
+ "gitHead": "0e9dcf532b8abf7608d07f738348538fa9a9864d"
65
65
  }
package/types/cli.d.ts CHANGED
File without changes
@@ -1,3 +1,3 @@
1
- import type { Options } from './utils';
1
+ import { type Options } from "./utils";
2
2
  import type { ArgumentsCamelCase } from "yargs";
3
- export declare const handler: (argv: ArgumentsCamelCase<Options>) => Promise<Record<string, any>>;
3
+ export declare const handler: (argv: ArgumentsCamelCase<Options> | Options) => Promise<Record<string, any>>;
package/types/index.d.ts CHANGED
File without changes
@@ -1,7 +1,11 @@
1
1
  declare const _default: {
2
- "version": "0.2.2",
2
+ "version": "0.3.0",
3
3
  "name": "@done-coding/cli-inject",
4
- "description": "信息注入命令行工具"
4
+ "description": "信息注入命令行工具",
5
+ "cliConfig": {
6
+ "namespaceDir": ".done-coding",
7
+ "commandName": "inject"
8
+ }
5
9
  };
6
10
 
7
11
  export default _default;
package/types/main.d.ts CHANGED
File without changes
@@ -1 +1,2 @@
1
1
  export * from "./types";
2
+ export * from "./resolve";
@@ -0,0 +1,35 @@
1
+ /** 配置类型枚举 */
2
+ export declare enum ConfigTypeEnum {
3
+ /**
4
+ * 正则表达式类型
5
+ * -----
6
+ * @example `${key}:${targetKey}:REG:${pattern}:${replaceValue}:${flags}`
7
+ */
8
+ REG = "REG",
9
+ /**
10
+ * 直接设置值类型
11
+ * -----
12
+ * @example `${key}:${targetKey}:VALUE:${value}`
13
+ */
14
+ VALUE = "VALUE",
15
+ /**
16
+ * 默认类型
17
+ * -----
18
+ * @example `${key}`
19
+ * @example `${key}:${targetKey}`
20
+ */
21
+ DEFAULT = "DEFAULT"
22
+ }
23
+ /** 获取配置key及其参数 */
24
+ export declare const getKey: (configStr: string) => {
25
+ key: string;
26
+ /** 目标key */
27
+ targetKey: string;
28
+ /** 参数列表 */
29
+ paramsList: string[];
30
+ };
31
+ /** 参数解析-获取最终值 */
32
+ export declare const paramsResolve: ({ valueInit, paramsList, }: {
33
+ valueInit: any;
34
+ paramsList: string[];
35
+ }) => any;
File without changes