@done-coding/cli-inject 0.5.11 → 0.5.13-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,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { c as m } from "./index-f6928e73.js";
2
+ import { c as m } from "./index-c4ce9afd.js";
3
3
  import "./index-a1ee6691.js";
4
4
  import "@done-coding/cli-utils";
5
5
  import "node:path";
package/es/helpers.mjs CHANGED
@@ -12,6 +12,9 @@ const o = {
12
12
  description: {
13
13
  type: e.READ
14
14
  },
15
+ bin: {
16
+ type: e.READ
17
+ },
15
18
  "cliConfig.namespaceDir": {
16
19
  type: e.FIXED,
17
20
  value: ".done-coding"
@@ -0,0 +1,165 @@
1
+ #!/usr/bin/env node
2
+ import { I as c, S as E } from "./index-a1ee6691.js";
3
+ import { log as o, _get as I, initHandlerCommon as k, getConfigFileCommonOptions as A, _set as w, readConfigFile as T, createSubcommand as N, getRootScriptName as _, createMainCommand as M } from "@done-coding/cli-utils";
4
+ import D from "node:path";
5
+ import u from "node:fs";
6
+ const x = ({
7
+ sourceJson: e,
8
+ targetKey: n,
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: a = c.READ } = s;
17
+ switch (a) {
18
+ case c.REG: {
19
+ const { sourceKey: r, pattern: f, replaceValue: p, flags: g } = s, y = new RegExp(f, g ?? void 0), i = I(e, r);
20
+ return typeof i == "string" ? i.replace(y, p) : (o.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 = n } = s;
30
+ return I(e, r);
31
+ }
32
+ default: {
33
+ o.warn(`未知的配置类型${a}`);
34
+ return;
35
+ }
36
+ }
37
+ }, d = {
38
+ name: "@done-coding/cli-inject",
39
+ version: "0.5.13-alpha.0",
40
+ description: "信息(JSON)注入命令行工具",
41
+ bin: {
42
+ "dc-inject": "es/cli.mjs"
43
+ },
44
+ cliConfig: {
45
+ namespaceDir: ".done-coding",
46
+ moduleName: "inject"
47
+ }
48
+ }, {
49
+ cliConfig: { namespaceDir: G, moduleName: J }
50
+ } = d, L = `./${G}/${J}`, R = `${L}.json`, U = {
51
+ sourceFilePath: "./package.json",
52
+ keyConfigMap: {
53
+ name: {
54
+ type: c.READ
55
+ },
56
+ version: {
57
+ type: c.READ
58
+ },
59
+ description: {
60
+ type: c.READ
61
+ },
62
+ bin: {
63
+ type: c.READ
64
+ }
65
+ },
66
+ injectFilePath: "./src/injectInfo.json"
67
+ }, j = U, V = () => A({
68
+ configPathDefault: R
69
+ }), S = async (e) => k(j, e, {
70
+ onFileGenerated: () => {
71
+ o.info("文件生成成功");
72
+ }
73
+ }), H = {
74
+ command: E.INIT,
75
+ describe: "初始化配置文件",
76
+ options: V(),
77
+ handler: S
78
+ }, K = () => ({
79
+ ...A({
80
+ configPathDefault: R
81
+ })
82
+ }), W = async ({
83
+ rootDir: e = process.cwd(),
84
+ config: n = j,
85
+ keyConfigMap: t = {}
86
+ } = {}) => {
87
+ const {
88
+ sourceFilePath: s,
89
+ keyConfigMap: a,
90
+ injectFilePath: r
91
+ } = n, f = {
92
+ ...a,
93
+ ...t
94
+ };
95
+ if (!s.endsWith(".json"))
96
+ return o.error("源文件必须是json"), process.exit(1);
97
+ if (!r.endsWith(".json"))
98
+ return o.error("注入文件必须是json"), process.exit(1);
99
+ const p = D.resolve(e, s), g = u.readFileSync(p, "utf-8"), y = JSON.parse(g), i = Object.entries(f).reduce(
100
+ (m, [F, v]) => {
101
+ const P = x({ sourceJson: y, targetKey: F, keyConfig: v });
102
+ return w(m, F, P), m;
103
+ },
104
+ {}
105
+ ), l = D.resolve(e, r), C = JSON.stringify(i, null, 2);
106
+ if (u.existsSync(l)) {
107
+ const m = u.readFileSync(l, "utf-8");
108
+ if (C === m)
109
+ return o.skip("注入文件已存在且内容相同,无需重复注入"), i;
110
+ o.stage("文件内容变化,开始覆盖注入文件");
111
+ } else
112
+ o.stage("开始注入文件");
113
+ u.writeFileSync(l, C), o.success(`文件注入成功: ${l}`), o.info(C);
114
+ }, h = async (e) => {
115
+ const n = await T(e, () => (o.info("配置文件为空,使用默认配置"), j));
116
+ if (!n)
117
+ return o.error("配置文件为空"), process.exit(1);
118
+ const { rootDir: t } = e;
119
+ await W({ rootDir: t, config: n });
120
+ }, X = {
121
+ command: "$0",
122
+ describe: "生成文件",
123
+ options: K(),
124
+ handler: h
125
+ }, ee = async (e, n) => {
126
+ switch (e) {
127
+ case E.INIT:
128
+ return S(n);
129
+ case E.GENERATE:
130
+ return h(n);
131
+ default:
132
+ return h(n);
133
+ }
134
+ }, { version: q, description: z } = d, b = {
135
+ describe: z,
136
+ version: q,
137
+ subcommands: [H, X].map(
138
+ N
139
+ ),
140
+ demandCommandCount: 1,
141
+ rootScriptName: _({ packageJson: d })
142
+ }, {
143
+ cliConfig: { moduleName: $ }
144
+ } = d, O = (e = !1) => {
145
+ const n = e ? $ : void 0, t = `$0${e ? ` ${$}` : ""} <command> [options]`;
146
+ return { command: n, usage: t };
147
+ }, ne = async () => M({
148
+ ...b,
149
+ ...O()
150
+ }), oe = () => N({
151
+ ...b,
152
+ ...O(!0)
153
+ });
154
+ export {
155
+ oe as a,
156
+ H as b,
157
+ ne as c,
158
+ h as d,
159
+ X as e,
160
+ ee as f,
161
+ W as g,
162
+ S as h,
163
+ b as i,
164
+ x as k
165
+ };
package/es/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { i as r, a as i, e as s, g as t, d, f as l, b as f, h as p, k as C } from "./index-f6928e73.js";
2
+ import { i as r, a as i, e as s, g as t, d, f as l, b as f, h as p, k as C } from "./index-c4ce9afd.js";
3
3
  import { I as g, S as u } from "./index-a1ee6691.js";
4
4
  import "@done-coding/cli-utils";
5
5
  import "node:path";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@done-coding/cli-inject",
3
- "version": "0.5.11",
3
+ "version": "0.5.13-alpha.0",
4
4
  "description": "信息(JSON)注入命令行工具",
5
5
  "private": false,
6
6
  "module": "es/index.mjs",
@@ -63,7 +63,7 @@
63
63
  "node": ">=18.0.0"
64
64
  },
65
65
  "dependencies": {
66
- "@done-coding/cli-utils": "^0.7.2"
66
+ "@done-coding/cli-utils": "^0.7.4-alpha.0"
67
67
  },
68
- "gitHead": "ad322da7f5d0897ee91a399aff9a14b111d62825"
68
+ "gitHead": "a1acffae69586b12aec17d0ad76d6f75ad1d0ca0"
69
69
  }
@@ -1,158 +0,0 @@
1
- #!/usr/bin/env node
2
- import { I as r, S as d } from "./index-a1ee6691.js";
3
- import { log as o, _get as j, initHandlerCommon as b, getConfigFileCommonOptions as D, _set as w, readConfigFile as _, createSubcommand as N, createMainCommand as k } from "@done-coding/cli-utils";
4
- import $ from "node:path";
5
- import m from "node:fs";
6
- const M = ({
7
- sourceJson: e,
8
- targetKey: n,
9
- keyConfig: t
10
- }) => {
11
- let s;
12
- typeof t == "string" ? s = {
13
- type: r.FIXED,
14
- value: t
15
- } : s = t;
16
- const { type: a = r.READ } = s;
17
- switch (a) {
18
- case r.REG: {
19
- const { sourceKey: c, pattern: f, replaceValue: p, flags: g } = s, y = new RegExp(f, g ?? void 0), i = j(e, c);
20
- return typeof i == "string" ? i.replace(y, p) : (o.warn(
21
- `${i}不是字符串类型,无法使用正则表达式进行替换,此处将直接返回原值`
22
- ), i);
23
- }
24
- case r.FIXED: {
25
- const { value: c } = s;
26
- return c;
27
- }
28
- case r.READ: {
29
- const { sourceKey: c = n } = s;
30
- return j(e, c);
31
- }
32
- default: {
33
- o.warn(`未知的配置类型${a}`);
34
- return;
35
- }
36
- }
37
- }, h = {
38
- name: "@done-coding/cli-inject",
39
- version: "0.5.11",
40
- description: "信息(JSON)注入命令行工具",
41
- cliConfig: {
42
- namespaceDir: ".done-coding",
43
- moduleName: "inject"
44
- }
45
- }, {
46
- cliConfig: { namespaceDir: x, moduleName: G }
47
- } = h, L = `./${x}/${G}`, R = `${L}.json`, F = {
48
- sourceFilePath: "./package.json",
49
- keyConfigMap: {
50
- name: {
51
- type: r.READ
52
- },
53
- version: {
54
- type: r.READ
55
- },
56
- description: {
57
- type: r.READ
58
- }
59
- },
60
- injectFilePath: "./src/injectInfo.json"
61
- }, J = () => D({
62
- configPathDefault: R
63
- }), O = async (e) => b(F, e, {
64
- onFileGenerated: () => {
65
- o.info("文件生成成功");
66
- }
67
- }), U = {
68
- command: d.INIT,
69
- describe: "初始化配置文件",
70
- options: J(),
71
- handler: O
72
- }, V = () => ({
73
- ...D({
74
- configPathDefault: R
75
- })
76
- }), H = async ({
77
- rootDir: e = process.cwd(),
78
- config: n = F,
79
- keyConfigMap: t = {}
80
- } = {}) => {
81
- const {
82
- sourceFilePath: s,
83
- keyConfigMap: a,
84
- injectFilePath: c
85
- } = n, f = {
86
- ...a,
87
- ...t
88
- };
89
- if (!s.endsWith(".json"))
90
- return o.error("源文件必须是json"), process.exit(1);
91
- if (!c.endsWith(".json"))
92
- return o.error("注入文件必须是json"), process.exit(1);
93
- const p = $.resolve(e, s), g = m.readFileSync(p, "utf-8"), y = JSON.parse(g), i = Object.entries(f).reduce(
94
- (u, [I, P]) => {
95
- const T = M({ sourceJson: y, targetKey: I, keyConfig: P });
96
- return w(u, I, T), u;
97
- },
98
- {}
99
- ), l = $.resolve(e, c), E = JSON.stringify(i, null, 2);
100
- if (m.existsSync(l)) {
101
- const u = m.readFileSync(l, "utf-8");
102
- if (E === u)
103
- return o.skip("注入文件已存在且内容相同,无需重复注入"), i;
104
- o.stage("文件内容变化,开始覆盖注入文件");
105
- } else
106
- o.stage("开始注入文件");
107
- m.writeFileSync(l, E), o.success(`文件注入成功: ${l}`), o.info(E);
108
- }, C = async (e) => {
109
- const n = await _(e, () => (o.info("配置文件为空,使用默认配置"), F));
110
- if (!n)
111
- return o.error("配置文件为空"), process.exit(1);
112
- const { rootDir: t } = e;
113
- await H({ rootDir: t, config: n });
114
- }, K = {
115
- command: d.GENERATE,
116
- describe: "生成文件",
117
- options: V(),
118
- handler: C
119
- }, Y = async (e, n) => {
120
- switch (e) {
121
- case d.INIT:
122
- return O(n);
123
- case d.GENERATE:
124
- return C(n);
125
- default:
126
- return C(n);
127
- }
128
- }, { version: W, description: X } = h, S = {
129
- describe: X,
130
- version: W,
131
- subcommands: [U, K].map(
132
- N
133
- ),
134
- demandCommandCount: 1
135
- }, {
136
- cliConfig: { moduleName: A }
137
- } = h, v = (e = !1) => {
138
- const n = e ? A : void 0, t = `$0${e ? ` ${A}` : ""} <command> [options]`;
139
- return { command: n, usage: t };
140
- }, Z = async () => k({
141
- ...S,
142
- ...v()
143
- }), ee = () => N({
144
- ...S,
145
- ...v(!0)
146
- });
147
- export {
148
- ee as a,
149
- U as b,
150
- Z as c,
151
- C as d,
152
- K as e,
153
- Y as f,
154
- H as g,
155
- O as h,
156
- S as i,
157
- M as k
158
- };