@done-coding/cli-utils 0.2.2-alpha.0 → 0.3.0-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/index.mjs CHANGED
@@ -1,36 +1,38 @@
1
1
  #!/usr/bin/env node
2
- import h from "chalk";
3
- import { default as O } from "chalk";
4
- import i from "node:path";
5
- import v from "node:fs";
6
- import f from "crypto";
7
- import S from "prompts";
8
- const c = Object.assign(
9
- (r, ...n) => console.log(...n.map((e) => h[r](e))),
2
+ import b from "chalk";
3
+ import { default as Y } from "chalk";
4
+ import p from "node:path";
5
+ import E from "node:fs";
6
+ import l from "crypto";
7
+ import H from "prompts";
8
+ import k from "yargs";
9
+ import { hideBin as $ } from "yargs/helpers";
10
+ const i = Object.assign(
11
+ (r, ...t) => console.log(...t.map((e) => b[r](e))),
10
12
  {
11
13
  /** 成功 */
12
- success: (...r) => c("green", ...r),
14
+ success: (...r) => i("green", ...r),
13
15
  /** /步骤 */
14
- stage: (...r) => c("blue", ...r),
16
+ stage: (...r) => i("blue", ...r),
15
17
  /** 提示信息 */
16
- info: (...r) => c("cyan", ...r),
18
+ info: (...r) => i("cyan", ...r),
17
19
  /** 警告 */
18
- warn: (...r) => c("yellow", ...r),
20
+ warn: (...r) => i("yellow", ...r),
19
21
  /** 错误 */
20
- error: (...r) => c("red", ...r),
22
+ error: (...r) => i("red", ...r),
21
23
  /** 跳过 */
22
- skip: (...r) => c("gray", ...r)
24
+ skip: (...r) => i("gray", ...r)
23
25
  }
24
- ), k = (r, n = process.cwd()) => {
25
- const e = i.resolve(n).split(i.sep).map((t, o, s) => o ? i.join(s.slice(0, o).join(i.sep), t) : t);
26
+ ), P = (r, t = process.cwd()) => {
27
+ const e = p.resolve(t).split(p.sep).map((o, c, s) => c ? p.join(s.slice(0, c).join(p.sep), o) : o);
26
28
  for (; e.length; ) {
27
- const t = e.pop(), o = i.join(t, r);
28
- if (v.existsSync(o))
29
- return t;
29
+ const o = e.pop(), c = p.join(o, r);
30
+ if (E.existsSync(c))
31
+ return o;
30
32
  }
31
- }, m = "aes-256-cbc", y = 16, u = "hex", d = ":";
32
- function g(r) {
33
- return f.pbkdf2Sync(
33
+ }, g = "aes-256-cbc", m = 16, u = "hex", d = ":";
34
+ function y(r) {
35
+ return l.pbkdf2Sync(
34
36
  r,
35
37
  "done-coding-cli-salt",
36
38
  // 使用固定的盐值
@@ -41,55 +43,88 @@ function g(r) {
41
43
  "sha256"
42
44
  );
43
45
  }
44
- function A({
46
+ function D({
45
47
  text: r,
46
- secretKey: n
48
+ secretKey: t
47
49
  }) {
48
50
  try {
49
- const e = g(n), t = f.randomBytes(y), o = f.createCipheriv(m, e, t);
50
- let s = o.update(r);
51
- s = Buffer.concat([s, o.final()]);
52
- const a = t.toString(u), p = s.toString(u);
53
- return `${a}${d}${p}`;
51
+ const e = y(t), o = l.randomBytes(m), c = l.createCipheriv(g, e, o);
52
+ let s = c.update(r);
53
+ s = Buffer.concat([s, c.final()]);
54
+ const f = o.toString(u), n = s.toString(u);
55
+ return `${f}${d}${n}`;
54
56
  } catch (e) {
55
- return c.error(
57
+ return i.error(
56
58
  `加密失败: ${e instanceof Error ? e.message : String(e)}`
57
59
  ), "";
58
60
  }
59
61
  }
60
- function B({
62
+ function F({
61
63
  encryptedText: r,
62
- secretKey: n
64
+ secretKey: t
63
65
  }) {
64
66
  try {
65
67
  if (!r.includes(d))
66
68
  return "";
67
- const e = g(n), [t, o] = r.split(d);
68
- if (t.length !== y * 2)
69
+ const e = y(t), [o, c] = r.split(d);
70
+ if (o.length !== m * 2)
69
71
  return "";
70
- const s = Buffer.from(t, u), a = Buffer.from(o, u), p = f.createDecipheriv(m, e, s);
71
- let l = p.update(a);
72
- return l = Buffer.concat([l, p.final()]), l.toString();
72
+ const s = Buffer.from(o, u), f = Buffer.from(c, u), n = l.createDecipheriv(g, e, s);
73
+ let a = n.update(f);
74
+ return a = Buffer.concat([a, n.final()]), a.toString();
73
75
  } catch (e) {
74
- return c.error(
76
+ return i.error(
75
77
  `解密失败: ${e instanceof Error ? e.message : String(e)}`
76
78
  ), "";
77
79
  }
78
80
  }
79
- const j = (...r) => {
80
- const [n, e = {}] = r;
81
- return S(n, {
82
- onCancel(t) {
83
- return c.error(`退出${t == null ? void 0 : t.name}输入`), process.exit(1);
81
+ const I = (...r) => {
82
+ const [t, e = {}] = r;
83
+ return H(t, {
84
+ onCancel(o) {
85
+ return i.error(`退出${o == null ? void 0 : o.name}输入`), process.exit(1);
84
86
  },
85
87
  ...e
86
88
  });
89
+ }, A = (r, t) => {
90
+ r ? i.error(r) : i.error(t.message), t != null && t.stack && i.error(t.stack), process.exit(1);
91
+ }, B = () => {
92
+ const r = $(process.argv);
93
+ return k(r);
94
+ }, h = (r, {
95
+ usage: t,
96
+ version: e,
97
+ demandCommandCount: o,
98
+ options: c,
99
+ positionals: s,
100
+ subcommands: f
101
+ }) => {
102
+ let n = r.strict();
103
+ t && (n = n.usage(`Usage: ${t}`)), o && (n = n.demandCommand(o));
104
+ const a = "help";
105
+ return n = n.help(a), e ? n = n.version(e).alias("h", a).alias("v", "version") : n = n.alias("h", a), c && (n = n.options(c)), s && (n = Object.entries(s).reduce((v, [S, x]) => v.positional(S, x), n)), f && (n = n.command(f)), n;
106
+ }, K = async ({ handler: r, ...t }) => {
107
+ const e = await h(B(), t).fail(A).argv;
108
+ return r ? r(e) : e;
109
+ }, R = (r) => {
110
+ const { command: t, describe: e, handler: o = () => {
111
+ }, ...c } = r;
112
+ return {
113
+ command: t,
114
+ describe: e,
115
+ builder(s) {
116
+ return h(s, c);
117
+ },
118
+ handler: o
119
+ };
87
120
  };
88
121
  export {
89
- O as chalk,
90
- B as decryptAES,
91
- A as encryptAES,
92
- c as log,
93
- k as lookForParentTarget,
94
- j as xPrompts
122
+ Y as chalk,
123
+ K as createMainCommand,
124
+ R as createSubcommand,
125
+ F as decryptAES,
126
+ D as encryptAES,
127
+ i as log,
128
+ P as lookForParentTarget,
129
+ I as xPrompts
95
130
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@done-coding/cli-utils",
3
- "version": "0.2.2-alpha.0",
3
+ "version": "0.3.0-alpha.0",
4
4
  "description": "cli utils",
5
5
  "private": false,
6
6
  "module": "es/index.mjs",
@@ -54,5 +54,5 @@
54
54
  "prompts": "^2.4.2",
55
55
  "yargs": "^17.7.2"
56
56
  },
57
- "gitHead": "2e59e86ee192e023138e7475f4abbb5e7c42b29e"
57
+ "gitHead": "c5a54bdf08759425701a2024442a923883065189"
58
58
  }
package/types/cli.d.ts ADDED
@@ -0,0 +1,43 @@
1
+ import type { ArgumentsCamelCase, CommandModule, Options as YargsOptions, Argv as YargsArgv } from "yargs";
2
+ import yargs from "yargs";
3
+ export { ArgumentsCamelCase, CommandModule, YargsOptions, YargsArgv };
4
+ /** cli 信息 */
5
+ export interface CliInfo {
6
+ /** 命令 */
7
+ command?: string;
8
+ /** 用法 */
9
+ usage?: string;
10
+ /** 描述信息 */
11
+ describe?: string;
12
+ /** 版本号 */
13
+ version?: string;
14
+ /** 必传命令数 */
15
+ demandCommandCount?: number;
16
+ /** 选项 */
17
+ options?: {
18
+ [key in string]: YargsOptions;
19
+ };
20
+ /** 子命令 */
21
+ subcommands?: CommandModule[];
22
+ /** 位置信息 */
23
+ positionals?: {
24
+ [key in string]: Parameters<typeof yargs.positional>[1];
25
+ };
26
+ /** 处理函数 */
27
+ handler?: CommandModule["handler"];
28
+ }
29
+ /** 子cli信息 */
30
+ export interface SubCliInfo extends CliInfo {
31
+ /** 命令 */
32
+ command: string;
33
+ }
34
+ /** 处理函数参数类型 */
35
+ export type CliHandlerArgv<O> = ArgumentsCamelCase<O> | O;
36
+ /** 创建主命令 */
37
+ export declare const createMainCommand: ({ handler, ...config }: CliInfo) => Promise<void | {
38
+ [x: string]: unknown;
39
+ _: (string | number)[];
40
+ $0: string;
41
+ }>;
42
+ /** 创建子命令模块 */
43
+ export declare const createSubcommand: (cliInfo: SubCliInfo) => CommandModule;
package/types/index.d.ts CHANGED
@@ -2,3 +2,4 @@ export * from "./log";
2
2
  export * from "./look-for";
3
3
  export * from "./aes";
4
4
  export * from "./prompts";
5
+ export * from "./cli";