@done-coding/cli-component 0.4.5 → 0.4.7

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-2b31a5e8.js";
2
+ import { c as m } from "./index-800149be.js";
3
3
  import "node:path";
4
4
  import "node:fs";
5
5
  import "@done-coding/cli-utils";
@@ -0,0 +1,266 @@
1
+ #!/usr/bin/env node
2
+ import p from "node:path";
3
+ import m from "node:fs";
4
+ import { log as l, json5 as L, chalk as g, xPrompts as w, createSubcommand as O, getRootScriptName as V, createMainCommand as J } from "@done-coding/cli-utils";
5
+ import x from "lodash.upperfirst";
6
+ import v from "lodash.camelcase";
7
+ import S from "lodash.kebabcase";
8
+ import T from "lodash.lowerfirst";
9
+ import f from "lodash.template";
10
+ import { OutputModeEnum as E, compileHandler as C } from "@done-coding/cli-template";
11
+ var u = /* @__PURE__ */ ((e) => (e.ADD = "add", e.REMOVE = "remove", e.LIST = "list", e))(u || {});
12
+ const _ = (e, t) => {
13
+ if (!/^[a-zA-Z]+[a-zA-Z0-9-]*$/.test(e))
14
+ return l.error("组件名只能包含字母、数字、中划线"), process.exit(1);
15
+ const { nameExcludes: n } = t;
16
+ return n.includes(e) ? (l.error(`组件名: ${e}是保留名称。
17
+ 保留名称: ${n.join(",")}`), process.exit(1)) : !0;
18
+ }, D = {
19
+ name: "@done-coding/cli-component",
20
+ version: "0.4.7",
21
+ description: "组件命令行工具",
22
+ bin: {
23
+ "dc-component": "es/cli.mjs"
24
+ },
25
+ cliConfig: {
26
+ namespaceDir: ".done-coding",
27
+ moduleName: "component"
28
+ }
29
+ }, { namespaceDir: K, moduleName: z } = D.cliConfig, N = () => p.resolve(K, z), I = () => ({
30
+ execDir: process.cwd(),
31
+ templateDir: N()
32
+ }), b = (e) => {
33
+ const { series: t, name: n } = e, o = x(v(n)), c = T(o), a = S(o), s = t ? x(v(t)) : "", i = s ? `${s}${o}` : "", d = S(i);
34
+ return {
35
+ series: s,
36
+ name: o,
37
+ nameLowerFirst: c,
38
+ nameKebab: a,
39
+ fullName: i,
40
+ fullNameKebab: d,
41
+ cls: d
42
+ };
43
+ }, Z = (e) => ({
44
+ $: "$",
45
+ ...I(),
46
+ ...b(e)
47
+ }), $ = () => {
48
+ const e = p.resolve(N(), "index.json");
49
+ if (!m.existsSync(e))
50
+ return l.error(`模块入口文件不存在: ${e}`), process.exit(1);
51
+ const n = JSON.parse(m.readFileSync(e, "utf-8")).config;
52
+ if (!n)
53
+ return l.error(`配置文件相对路径不存在: ${n}`), process.exit(1);
54
+ const o = p.resolve(
55
+ p.dirname(e),
56
+ n
57
+ );
58
+ if (!m.existsSync(o))
59
+ return l.error(`配置文件不存在: ${o}`), process.exit(1);
60
+ const c = L.parse(m.readFileSync(o, "utf-8")), a = I();
61
+ return c.componentDir = f(c.componentDir)(a), m.existsSync(c.componentDir) || m.mkdirSync(c.componentDir, {
62
+ recursive: !0
63
+ }), c;
64
+ }, k = async ({
65
+ name: e,
66
+ config: t,
67
+ command: n
68
+ }) => {
69
+ if (![u.ADD, u.REMOVE].includes(n))
70
+ return l.error(`不支持组件${n}操作`), process.exit(1);
71
+ const o = Z({
72
+ ...t,
73
+ name: e
74
+ }), c = process.cwd(), a = JSON.stringify(o);
75
+ for (const { entry: s, index: i } of t.list) {
76
+ const d = {
77
+ envData: a,
78
+ rollback: n === u.REMOVE,
79
+ /** 回滚时可以删除空文件 */
80
+ rollbackDelNullFile: !0,
81
+ rollbackDelAskAsYes: !0,
82
+ dealMarkdown: !0,
83
+ batch: !1,
84
+ rootDir: c
85
+ };
86
+ if (s) {
87
+ const r = s;
88
+ r != null && r.input && (r.input = f(r.input)(o)), r != null && r.output && (r.output = f(r.output)(o));
89
+ const h = {
90
+ ...s,
91
+ mode: E.APPEND,
92
+ ...d
93
+ };
94
+ await C(h);
95
+ }
96
+ if (i) {
97
+ const r = i;
98
+ r != null && r.input && (r.input = f(r.input)(o)), r != null && r.output && (r.output = f(r.output)(o));
99
+ const h = {
100
+ ...i,
101
+ mode: E.OVERWRITE,
102
+ ...d
103
+ };
104
+ await C(h);
105
+ }
106
+ }
107
+ }, H = () => ({
108
+ outputJson: {
109
+ alias: "o",
110
+ describe: "是否输出组件名列表json",
111
+ type: "boolean",
112
+ default: !1
113
+ },
114
+ outputPath: {
115
+ alias: "p",
116
+ describe: "输入路径",
117
+ type: "string"
118
+ }
119
+ }), y = (e) => {
120
+ const { componentDir: t, nameExcludes: n } = e;
121
+ return m.statSync(t).isDirectory() ? m.readdirSync(t).map((s) => {
122
+ const i = p.join(t, s);
123
+ return m.statSync(i).isDirectory() ? (console.log("filePath:", i, p.basename(i)), p.basename(i)) : "";
124
+ }).filter((s) => !(!s || n.includes(s))) : (l.error("组件源码路径不是目录"), process.exit(1));
125
+ }, R = async ({
126
+ outputJson: e,
127
+ outputPath: t
128
+ }) => {
129
+ l.stage("展示列表");
130
+ const n = $(), o = y(n), c = t || n.nameListJsonOutputPath, a = o.map((s) => {
131
+ const { name: i, fullName: d } = b({
132
+ ...n,
133
+ name: s
134
+ });
135
+ return {
136
+ name: i,
137
+ nameKebab: s,
138
+ fullName: d
139
+ };
140
+ });
141
+ if (console.table(
142
+ a.map(({ name: s, fullName: i, nameKebab: d }) => ({
143
+ [g.green("名称")]: s,
144
+ [g.green("带系列名称")]: i,
145
+ [g.green("绝对路径")]: p.resolve(n.componentDir, d)
146
+ }))
147
+ ), e && c) {
148
+ const s = p.resolve(c), i = p.dirname(s);
149
+ m.existsSync(i) || m.mkdirSync(i, { recursive: !0 }), console.log(g.blue(`输出组件名列表到${s}`)), m.writeFileSync(s, JSON.stringify(a, null, 2));
150
+ }
151
+ }, U = {
152
+ command: u.LIST,
153
+ describe: "展示组件列表",
154
+ options: H(),
155
+ handler: R
156
+ }, W = () => ({
157
+ name: {
158
+ describe: "组件名称",
159
+ type: "string"
160
+ }
161
+ }), A = async ({
162
+ name: e
163
+ }) => {
164
+ l.stage("添加组件");
165
+ let t;
166
+ e ? t = e : t = (await w({
167
+ type: "text",
168
+ name: "name",
169
+ message: "请输入组件名"
170
+ })).name;
171
+ const n = $();
172
+ _(t, n);
173
+ const { series: o } = n, c = await y(n);
174
+ for (let a of c)
175
+ if (b({
176
+ series: o,
177
+ name: t
178
+ }).nameKebab === a)
179
+ return l.error(`组件${a}已存在, 不能再次创建${t}组件`), process.exit(1);
180
+ return k({
181
+ name: t,
182
+ config: n,
183
+ command: u.ADD
184
+ });
185
+ }, Y = {
186
+ command: `${u.ADD} <name>`,
187
+ describe: "新增一个组件",
188
+ positionals: W(),
189
+ handler: A
190
+ }, q = () => ({
191
+ name: {
192
+ describe: "组件名称",
193
+ type: "string"
194
+ }
195
+ }), M = async ({
196
+ name: e
197
+ }) => {
198
+ l.stage("移除组件");
199
+ const t = $(), n = await y(t);
200
+ if (n.length === 0)
201
+ return l.error("组件列表为空"), process.exit(1);
202
+ let o;
203
+ e ? o = e : o = (await w({
204
+ type: "select",
205
+ name: "name",
206
+ message: "请选择要移除的组件",
207
+ choices: n.map((a) => ({ title: a, value: a }))
208
+ })).name;
209
+ const { series: c } = t;
210
+ for (let a of n)
211
+ if (b({
212
+ series: c,
213
+ name: o
214
+ }).nameKebab === a) {
215
+ await k({
216
+ name: o,
217
+ config: t,
218
+ command: u.REMOVE
219
+ }), m.rmdirSync(p.resolve(t.componentDir, a));
220
+ return;
221
+ }
222
+ return l.error(`组件${o}不存在`), process.exit(1);
223
+ }, B = {
224
+ command: `${u.REMOVE} [name]`,
225
+ describe: "删除一个组件",
226
+ positionals: q(),
227
+ handler: M
228
+ }, ce = async (e, t) => {
229
+ switch (e) {
230
+ case u.ADD:
231
+ return A(t);
232
+ case u.REMOVE:
233
+ return M(t);
234
+ case u.LIST:
235
+ return R();
236
+ default:
237
+ throw new Error(`不支持的命令 ${e}`);
238
+ }
239
+ }, { version: G, description: Q } = D, j = {
240
+ describe: Q,
241
+ version: G,
242
+ subcommands: [
243
+ Y,
244
+ B,
245
+ U
246
+ ].map(O),
247
+ demandCommandCount: 1,
248
+ rootScriptName: V({ packageJson: D })
249
+ }, {
250
+ cliConfig: { moduleName: P }
251
+ } = D, F = (e = !1) => {
252
+ const t = e ? P : void 0, n = `$0${e ? ` ${P}` : ""} <command> [options]`;
253
+ return { command: t, usage: n };
254
+ }, me = async () => J({
255
+ ...j,
256
+ ...F()
257
+ }), le = () => O({
258
+ ...j,
259
+ ...F(!0)
260
+ });
261
+ export {
262
+ u as S,
263
+ le as a,
264
+ me as c,
265
+ ce as h
266
+ };
package/es/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { S as u, a as b, h as c } from "./index-2b31a5e8.js";
2
+ import { S as u, a as b, h as c } from "./index-800149be.js";
3
3
  import "node:path";
4
4
  import "node:fs";
5
5
  import "@done-coding/cli-utils";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@done-coding/cli-component",
3
- "version": "0.4.5",
3
+ "version": "0.4.7",
4
4
  "description": "组件命令行工具",
5
5
  "private": false,
6
6
  "module": "es/index.mjs",
@@ -41,7 +41,7 @@
41
41
  "license": "MIT",
42
42
  "sideEffects": false,
43
43
  "devDependencies": {
44
- "@done-coding/cli-inject": "0.5.17",
44
+ "@done-coding/cli-inject": "0.5.18",
45
45
  "@types/lodash.camelcase": "^4.3.8",
46
46
  "@types/lodash.kebabcase": "^4.1.8",
47
47
  "@types/lodash.lowerfirst": "^4.3.8",
@@ -58,13 +58,13 @@
58
58
  "node": ">=18.0.0"
59
59
  },
60
60
  "dependencies": {
61
- "@done-coding/cli-template": "0.8.4",
62
- "@done-coding/cli-utils": "0.7.7",
61
+ "@done-coding/cli-template": "0.8.5",
62
+ "@done-coding/cli-utils": "0.7.8",
63
63
  "lodash.camelcase": "^4.3.0",
64
64
  "lodash.kebabcase": "^4.1.1",
65
65
  "lodash.lowerfirst": "^4.3.1",
66
66
  "lodash.template": "^4.5.0",
67
67
  "lodash.upperfirst": "^4.3.1"
68
68
  },
69
- "gitHead": "b600fbc65b532f848da439e6356405ac00d0b643"
69
+ "gitHead": "67d4fc202427c5b2f387b856cfe9507127a5b26c"
70
70
  }
package/types/index.d.ts CHANGED
@@ -23,6 +23,14 @@ export declare interface Config {
23
23
  componentDir: string;
24
24
  /** 配置列表 */
25
25
  list: ConfigListItem[];
26
+ /**
27
+ * 组件名列表json 输出相对路径 (.json)
28
+ * ----
29
+ * 基于运行目录
30
+ * ---
31
+ * 该功能应用场景, 用于生成组件名列表 来用在vite按需加载组件判断
32
+ */
33
+ nameListJsonOutputPath?: string;
26
34
  }
27
35
 
28
36
  /** 列表item */
@@ -38,6 +46,17 @@ export declare const crateAsSubcommand: () => CommandModule<{}, {}>;
38
46
 
39
47
  export declare const handler: (command: SubcommandEnum, argv: CliHandlerArgv<any>) => Promise<void>;
40
48
 
49
+ export declare interface ListOptions {
50
+ /** 输出(组件名列表)json */
51
+ outputJson?: boolean;
52
+ /**
53
+ * 输出路径
54
+ * ---
55
+ * @default './component-name-list.json'
56
+ */
57
+ outputPath?: string;
58
+ }
59
+
41
60
  export declare interface RemoveOptions extends Partial<CommonOptions> {
42
61
  }
43
62
 
@@ -1,241 +0,0 @@
1
- #!/usr/bin/env node
2
- import u from "node:path";
3
- import p from "node:fs";
4
- import { log as i, json5 as F, chalk as $, xPrompts as N, createSubcommand as O, getRootScriptName as L, createMainCommand as T } from "@done-coding/cli-utils";
5
- import E from "lodash.upperfirst";
6
- import y from "lodash.camelcase";
7
- import C from "lodash.kebabcase";
8
- import _ from "lodash.lowerfirst";
9
- import f from "lodash.template";
10
- import { OutputModeEnum as v, compileHandler as S } from "@done-coding/cli-template";
11
- var m = /* @__PURE__ */ ((e) => (e.ADD = "add", e.REMOVE = "remove", e.LIST = "list", e))(m || {});
12
- const K = (e, t) => {
13
- if (!/^[a-zA-Z]+[a-zA-Z0-9-]*$/.test(e))
14
- return i.error("组件名只能包含字母、数字、中划线"), process.exit(1);
15
- const { nameExcludes: n } = t;
16
- return n.includes(e) ? (i.error(`组件名: ${e}是保留名称。
17
- 保留名称: ${n.join(",")}`), process.exit(1)) : !0;
18
- }, g = {
19
- name: "@done-coding/cli-component",
20
- version: "0.4.5",
21
- description: "组件命令行工具",
22
- bin: {
23
- "dc-component": "es/cli.mjs"
24
- },
25
- cliConfig: {
26
- namespaceDir: ".done-coding",
27
- moduleName: "component"
28
- }
29
- }, { namespaceDir: J, moduleName: z } = g.cliConfig, P = () => u.resolve(J, z), I = () => ({
30
- execDir: process.cwd(),
31
- templateDir: P()
32
- }), D = (e) => {
33
- const { series: t, name: n } = e, o = E(y(n)), a = _(o), s = C(o), c = t ? E(y(t)) : "", l = c ? `${c}${o}` : "", d = C(l);
34
- return {
35
- series: c,
36
- name: o,
37
- nameLowerFirst: a,
38
- nameKebab: s,
39
- fullName: l,
40
- fullNameKebab: d,
41
- cls: d
42
- };
43
- }, Z = (e) => ({
44
- $: "$",
45
- ...I(),
46
- ...D(e)
47
- }), h = () => {
48
- const e = u.resolve(P(), "index.json");
49
- if (!p.existsSync(e))
50
- return i.error(`模块入口文件不存在: ${e}`), process.exit(1);
51
- const n = JSON.parse(p.readFileSync(e, "utf-8")).config;
52
- if (!n)
53
- return i.error(`配置文件相对路径不存在: ${n}`), process.exit(1);
54
- const o = u.resolve(
55
- u.dirname(e),
56
- n
57
- );
58
- if (!p.existsSync(o))
59
- return i.error(`配置文件不存在: ${o}`), process.exit(1);
60
- const a = F.parse(p.readFileSync(o, "utf-8")), s = I();
61
- return a.componentDir = f(a.componentDir)(s), a;
62
- }, R = async ({
63
- name: e,
64
- config: t,
65
- command: n
66
- }) => {
67
- if (![m.ADD, m.REMOVE].includes(n))
68
- return i.error(`不支持组件${n}操作`), process.exit(1);
69
- const o = Z({
70
- ...t,
71
- name: e
72
- }), a = process.cwd(), s = JSON.stringify(o);
73
- for (const { entry: c, index: l } of t.list) {
74
- const d = {
75
- envData: s,
76
- rollback: n === m.REMOVE,
77
- /** 回滚时可以删除空文件 */
78
- rollbackDelNullFile: !0,
79
- rollbackDelAskAsYes: !0,
80
- dealMarkdown: !0,
81
- batch: !1,
82
- rootDir: a
83
- };
84
- if (c) {
85
- const r = c;
86
- r != null && r.input && (r.input = f(r.input)(o)), r != null && r.output && (r.output = f(r.output)(o));
87
- const b = {
88
- ...c,
89
- mode: v.APPEND,
90
- ...d
91
- };
92
- await S(b);
93
- }
94
- if (l) {
95
- const r = l;
96
- r != null && r.input && (r.input = f(r.input)(o)), r != null && r.output && (r.output = f(r.output)(o));
97
- const b = {
98
- ...l,
99
- mode: v.OVERWRITE,
100
- ...d
101
- };
102
- await S(b);
103
- }
104
- }
105
- }, x = (e) => {
106
- const { componentDir: t, nameExcludes: n } = e;
107
- return p.statSync(t).isDirectory() ? p.readdirSync(t).map((c) => {
108
- const l = u.join(t, c);
109
- return p.statSync(l).isDirectory() ? (console.log("filePath:", l, u.basename(l)), u.basename(l)) : "";
110
- }).filter((c) => !(!c || n.includes(c))) : (i.error("组件源码路径不是目录"), process.exit(1));
111
- }, M = async () => {
112
- i.stage("展示列表");
113
- const e = h(), t = x(e);
114
- console.table(
115
- t.map((n) => {
116
- const { name: o, fullName: a } = D({
117
- ...e,
118
- name: n
119
- });
120
- return {
121
- [$.green("名称")]: o,
122
- [$.green("带系列名称")]: a,
123
- [$.green("绝对路径")]: u.resolve(e.componentDir, n)
124
- };
125
- })
126
- );
127
- }, H = {
128
- command: m.LIST,
129
- describe: "展示组件列表",
130
- handler: M
131
- }, U = () => ({
132
- name: {
133
- describe: "组件名称",
134
- type: "string"
135
- }
136
- }), k = async ({
137
- name: e
138
- }) => {
139
- i.stage("添加组件");
140
- let t;
141
- e ? t = e : t = (await N({
142
- type: "text",
143
- name: "name",
144
- message: "请输入组件名"
145
- })).name;
146
- const n = h();
147
- K(t, n);
148
- const { series: o } = n, a = await x(n);
149
- for (let s of a)
150
- if (D({
151
- series: o,
152
- name: t
153
- }).nameKebab === s)
154
- return i.error(`组件${s}已存在, 不能再次创建${t}组件`), process.exit(1);
155
- return R({
156
- name: t,
157
- config: n,
158
- command: m.ADD
159
- });
160
- }, W = {
161
- command: `${m.ADD} <name>`,
162
- describe: "新增一个组件",
163
- positionals: U(),
164
- handler: k
165
- }, Y = () => ({
166
- name: {
167
- describe: "组件名称",
168
- type: "string"
169
- }
170
- }), A = async ({
171
- name: e
172
- }) => {
173
- i.stage("移除组件");
174
- const t = h(), n = await x(t);
175
- if (n.length === 0)
176
- return i.error("组件列表为空"), process.exit(1);
177
- let o;
178
- e ? o = e : o = (await N({
179
- type: "select",
180
- name: "name",
181
- message: "请选择要移除的组件",
182
- choices: n.map((s) => ({ title: s, value: s }))
183
- })).name;
184
- const { series: a } = t;
185
- for (let s of n)
186
- if (D({
187
- series: a,
188
- name: o
189
- }).nameKebab === s) {
190
- await R({
191
- name: o,
192
- config: t,
193
- command: m.REMOVE
194
- }), p.rmdirSync(u.resolve(t.componentDir, s));
195
- return;
196
- }
197
- return i.error(`组件${o}不存在`), process.exit(1);
198
- }, q = {
199
- command: `${m.REMOVE} [name]`,
200
- describe: "删除一个组件",
201
- positionals: Y(),
202
- handler: A
203
- }, ce = async (e, t) => {
204
- switch (e) {
205
- case m.ADD:
206
- return k(t);
207
- case m.REMOVE:
208
- return A(t);
209
- case m.LIST:
210
- return M();
211
- default:
212
- throw new Error(`不支持的命令 ${e}`);
213
- }
214
- }, { version: B, description: G } = g, V = {
215
- describe: G,
216
- version: B,
217
- subcommands: [
218
- W,
219
- q,
220
- H
221
- ].map(O),
222
- demandCommandCount: 1,
223
- rootScriptName: L({ packageJson: g })
224
- }, {
225
- cliConfig: { moduleName: w }
226
- } = g, j = (e = !1) => {
227
- const t = e ? w : void 0, n = `$0${e ? ` ${w}` : ""} <command> [options]`;
228
- return { command: t, usage: n };
229
- }, ie = async () => T({
230
- ...V,
231
- ...j()
232
- }), me = () => O({
233
- ...V,
234
- ...j(!0)
235
- });
236
- export {
237
- m as S,
238
- me as a,
239
- ie as c,
240
- ce as h
241
- };