@done-coding/cli-utils 0.2.1-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,19 +1,39 @@
1
1
  #!/usr/bin/env node
2
- import i from "node:path";
3
- import g from "node:fs";
4
- import p from "crypto";
5
- import h from "chalk";
6
- const H = (t, c = process.cwd()) => {
7
- const r = i.resolve(c).split(i.sep).map((e, n, o) => n ? i.join(o.slice(0, n).join(i.sep), e) : e);
8
- for (; r.length; ) {
9
- const e = r.pop(), n = i.join(e, t);
10
- if (g.existsSync(n))
11
- return 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))),
12
+ {
13
+ /** 成功 */
14
+ success: (...r) => i("green", ...r),
15
+ /** /步骤 */
16
+ stage: (...r) => i("blue", ...r),
17
+ /** 提示信息 */
18
+ info: (...r) => i("cyan", ...r),
19
+ /** 警告 */
20
+ warn: (...r) => i("yellow", ...r),
21
+ /** 错误 */
22
+ error: (...r) => i("red", ...r),
23
+ /** 跳过 */
24
+ skip: (...r) => i("gray", ...r)
12
25
  }
13
- }, l = "aes-256-cbc", m = 16, f = "hex", d = ":";
14
- function y(t) {
15
- return p.pbkdf2Sync(
16
- 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);
28
+ for (; e.length; ) {
29
+ const o = e.pop(), c = p.join(o, r);
30
+ if (E.existsSync(c))
31
+ return o;
32
+ }
33
+ }, g = "aes-256-cbc", m = 16, u = "hex", d = ":";
34
+ function y(r) {
35
+ return l.pbkdf2Sync(
36
+ r,
17
37
  "done-coding-cli-salt",
18
38
  // 使用固定的盐值
19
39
  1e4,
@@ -23,48 +43,88 @@ function y(t) {
23
43
  "sha256"
24
44
  );
25
45
  }
26
- function A({
27
- text: t,
28
- secretKey: c
46
+ function D({
47
+ text: r,
48
+ secretKey: t
29
49
  }) {
30
50
  try {
31
- const r = y(c), e = p.randomBytes(m), n = p.createCipheriv(l, r, e);
32
- let o = n.update(t);
33
- o = Buffer.concat([o, n.final()]);
34
- const a = e.toString(f), s = o.toString(f);
35
- return `${a}${d}${s}`;
36
- } catch (r) {
37
- return console.error(
38
- `加密失败: ${r instanceof Error ? r.message : String(r)}`
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}`;
56
+ } catch (e) {
57
+ return i.error(
58
+ `加密失败: ${e instanceof Error ? e.message : String(e)}`
39
59
  ), "";
40
60
  }
41
61
  }
42
- function B({
43
- encryptedText: t,
44
- secretKey: c
62
+ function F({
63
+ encryptedText: r,
64
+ secretKey: t
45
65
  }) {
46
66
  try {
47
- if (!t.includes(d))
67
+ if (!r.includes(d))
48
68
  return "";
49
- const r = y(c), [e, n] = t.split(d);
50
- if (e.length !== m * 2)
69
+ const e = y(t), [o, c] = r.split(d);
70
+ if (o.length !== m * 2)
51
71
  return "";
52
- const o = Buffer.from(e, f), a = Buffer.from(n, f), s = p.createDecipheriv(l, r, o);
53
- let u = s.update(a);
54
- return u = Buffer.concat([u, s.final()]), u.toString();
55
- } catch (r) {
56
- return console.error(
57
- `解密失败: ${r instanceof Error ? r.message : String(r)}`
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();
75
+ } catch (e) {
76
+ return i.error(
77
+ `解密失败: ${e instanceof Error ? e.message : String(e)}`
58
78
  ), "";
59
79
  }
60
80
  }
61
- function $(t) {
62
- if (t.aborted)
63
- return console.log(h.red("退出输入")), process.exit(1);
64
- }
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);
86
+ },
87
+ ...e
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
+ };
120
+ };
65
121
  export {
66
- B as decryptAES,
67
- A as encryptAES,
68
- H as lookForParentTarget,
69
- $ as onPromptFormStateForSigint
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
70
130
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@done-coding/cli-utils",
3
- "version": "0.2.1-alpha.0",
3
+ "version": "0.3.0-alpha.0",
4
4
  "description": "cli utils",
5
5
  "private": false,
6
6
  "module": "es/index.mjs",
@@ -39,6 +39,8 @@
39
39
  "sideEffects": false,
40
40
  "devDependencies": {
41
41
  "@types/node": "^18.0.0",
42
+ "@types/prompts": "^2.4.6",
43
+ "@types/yargs": "^17.0.28",
42
44
  "rimraf": "^6.0.1",
43
45
  "typescript": "^5.2.2",
44
46
  "vite": "^4.4.11",
@@ -48,7 +50,9 @@
48
50
  "node": ">=18.0.0"
49
51
  },
50
52
  "dependencies": {
51
- "chalk": "^5.3.0"
53
+ "chalk": "^5.3.0",
54
+ "prompts": "^2.4.2",
55
+ "yargs": "^17.7.2"
52
56
  },
53
- "gitHead": "21eb698f64179882cfec979bd8f19920b047dacb"
57
+ "gitHead": "c5a54bdf08759425701a2024442a923883065189"
54
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
@@ -1,3 +1,5 @@
1
+ export * from "./log";
1
2
  export * from "./look-for";
2
3
  export * from "./aes";
3
4
  export * from "./prompts";
5
+ export * from "./cli";
package/types/log.d.ts ADDED
@@ -0,0 +1,32 @@
1
+ import chalk from "chalk";
2
+ export { chalk };
3
+ /** 日志类型 */
4
+ declare enum LogTypeEnum {
5
+ /** 成功 */
6
+ SUCCESS = "green",
7
+ /** 步骤 */
8
+ STAGE = "blue",
9
+ /** 提示信息 */
10
+ INFO = "cyan",
11
+ /** 警告 */
12
+ WARN = "yellow",
13
+ /** 错误 */
14
+ ERROR = "red",
15
+ /** 跳过 */
16
+ SKIP = "gray"
17
+ }
18
+ /** 日志 */
19
+ export declare const log: ((type: LogTypeEnum, ...messages: string[]) => void) & {
20
+ /** 成功 */
21
+ success: (...messages: string[]) => void;
22
+ /** /步骤 */
23
+ stage: (...messages: string[]) => void;
24
+ /** 提示信息 */
25
+ info: (...messages: string[]) => void;
26
+ /** 警告 */
27
+ warn: (...messages: string[]) => void;
28
+ /** 错误 */
29
+ error: (...messages: string[]) => void;
30
+ /** 跳过 */
31
+ skip: (...messages: string[]) => void;
32
+ };
@@ -1,5 +1,4 @@
1
- /** prompt表单-进程退出信号处理 */
2
- export declare function onPromptFormStateForSigint(params: {
3
- aborted: boolean;
4
- value: any;
5
- }): undefined;
1
+ import prompts from "prompts";
2
+ export type { Choice as PromptChoice, Options as PromptOptions, PromptObject, Answers as PromptAnswers, PrevCaller as PromptPrevCaller, Falsy as PromptFalsy, PromptType, ValueOrFunc as PromptValueOrFunc, InitialReturnValue as PromptInitialReturnValue, } from "prompts";
3
+ /** prompts 拓展 */
4
+ export declare const xPrompts: (questions: prompts.PromptObject<string> | prompts.PromptObject<string>[], options?: prompts.Options | undefined) => Promise<prompts.Answers<string>>;