revojs 0.3.7 → 0.3.8

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/dist/bin/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { createCommandTree, runCommands } from "../commands/index.js";
2
+ import { createCommandTree, formatError, runCommands } from "../commands/index.js";
3
3
  import { installBase64 } from "./base64.js";
4
4
  import { createRunnableDevEnvironment, createServer, isRunnableDevEnvironment, loadEnv } from "vite";
5
5
  import { parseArgs } from "util";
@@ -63,7 +63,7 @@ try {
63
63
  });
64
64
  } catch (error) {
65
65
  console.error(error);
66
- process.exitCode = 1;
66
+ process.exitCode = formatError(error).exitCode;
67
67
  } finally {
68
68
  await server.close();
69
69
  process.exit();
@@ -12,9 +12,11 @@ export interface CommandArgument extends ParseArgsOptionDescriptor {
12
12
  description?: string;
13
13
  }
14
14
  export type CommandArguments = ParseArgsOptionsConfig & Record<string, CommandArgument>;
15
+ export type CommandFlagValues = Record<string, string | boolean | Array<string | boolean> | undefined>;
15
16
  export interface CommandContext<T extends ParseArgsOptionsConfig = ParseArgsOptionsConfig> {
16
17
  input: Array<string>;
17
18
  arguments: CommandValues<T>;
19
+ flags: CommandFlagValues;
18
20
  environment?: RunnableDevEnvironment;
19
21
  }
20
22
  export interface Command<T extends ParseArgsOptionsConfig = ParseArgsOptionsConfig> {
@@ -42,6 +44,10 @@ export interface CommandMatch {
42
44
  path: Array<string>;
43
45
  command?: Command<CommandArguments>;
44
46
  }
47
+ export interface CommandSelectOption<T> {
48
+ label: string;
49
+ value: T;
50
+ }
45
51
  export interface CommandHelpOptions {
46
52
  name: string;
47
53
  description?: string;
@@ -58,13 +64,18 @@ export interface CommandRunOptions {
58
64
  sections?: Record<string, Array<CommandHelpEntry>>;
59
65
  run: (command: Command<CommandArguments>, context: CommandContext<CommandArguments>) => Awaitable<void>;
60
66
  }
67
+ export declare class CommandError extends Error {
68
+ exitCode: number;
69
+ constructor(message: string, exitCode?: number, options?: ErrorOptions);
70
+ }
61
71
  export declare function defineCommand<const T extends ParseArgsOptionsConfig>(command: Command<T>): Command<T>;
62
72
  export declare function createCommandTree(commands: CommandLoaders): Promise<CommandTree>;
63
73
  export declare function findCommand(tree: CommandTree, positionals: Array<string>): CommandMatch;
64
74
  export declare function formatSection(title: string, entries: Array<CommandHelpEntry>): string[];
65
75
  export declare function formatFlag(flag: CommandFlag): string;
66
76
  export declare function formatHelp(options: CommandHelpOptions): string;
67
- export declare function parseCommand(command: Command<CommandArguments>, args: Array<string>, path: Array<string>): {
77
+ export declare function toCommandArguments(flags?: Array<CommandFlag>): CommandArguments;
78
+ export declare function parseCommand(command: Command<CommandArguments>, args: Array<string>, path: Array<string>, flags?: Array<CommandFlag>): {
68
79
  values: {
69
80
  [longOption: string]: string | boolean | undefined;
70
81
  } & {
@@ -72,4 +83,14 @@ export declare function parseCommand(command: Command<CommandArguments>, args: A
72
83
  };
73
84
  positionals: string[];
74
85
  };
75
- export declare function runCommands(options: CommandRunOptions): Promise<0 | 1>;
86
+ export declare function runCommands(options: CommandRunOptions): Promise<0 | 2>;
87
+ export declare function isInteractive(): boolean;
88
+ export declare function prompt(question: string, fallback?: string): Promise<string>;
89
+ export declare function confirm(question: string): Promise<boolean>;
90
+ export declare function select<T>(question: string, choices: Array<CommandSelectOption<T>>): Promise<T>;
91
+ export declare function formatTable(header: Array<string>, rows: Array<Array<string>>): string;
92
+ export declare function formatError(error: unknown, json?: boolean): {
93
+ exitCode: number;
94
+ text: string;
95
+ };
96
+ export declare const USAGE_EXIT_CODE = 2;
@@ -1,5 +1,14 @@
1
+ import { createInterface } from "node:readline/promises";
1
2
  import { parseArgs } from "node:util";
2
3
  //#region src/commands/index.ts
4
+ var CommandError = class extends Error {
5
+ exitCode;
6
+ constructor(message, exitCode = 1, options) {
7
+ super(message, options);
8
+ this.name = "CommandError";
9
+ this.exitCode = exitCode;
10
+ }
11
+ };
3
12
  function defineCommand(command) {
4
13
  return command;
5
14
  }
@@ -75,7 +84,19 @@ function formatHelp(options) {
75
84
  }))), ...formatSection("Commands", commands));
76
85
  return lines.join("\n");
77
86
  }
78
- function parseCommand(command, args, path) {
87
+ function toCommandArguments(flags = []) {
88
+ const options = {};
89
+ for (const flag of flags) {
90
+ const option = {
91
+ type: flag.type,
92
+ multiple: flag.multiple ?? false
93
+ };
94
+ if (flag.short) option.short = flag.short;
95
+ options[flag.name] = option;
96
+ }
97
+ return options;
98
+ }
99
+ function parseCommand(command, args, path, flags) {
79
100
  const consumable = [...path];
80
101
  return parseArgs({
81
102
  args: args.filter((argument) => {
@@ -87,6 +108,7 @@ function parseCommand(command, args, path) {
87
108
  strict: false,
88
109
  allowPositionals: true,
89
110
  options: {
111
+ ...toCommandArguments(flags),
90
112
  ...command.arguments,
91
113
  help: {
92
114
  type: "boolean",
@@ -101,11 +123,14 @@ async function runCommands(options) {
101
123
  args: options.args,
102
124
  strict: false,
103
125
  allowPositionals: true,
104
- options: { help: {
105
- type: "boolean",
106
- short: "h",
107
- default: false
108
- } }
126
+ options: {
127
+ ...toCommandArguments(options.flags),
128
+ help: {
129
+ type: "boolean",
130
+ short: "h",
131
+ default: false
132
+ }
133
+ }
109
134
  });
110
135
  const helpRequested = parsed.values.help === true || parsed.positionals[0] === "help";
111
136
  const match = findCommand(options.tree, parsed.positionals);
@@ -120,18 +145,88 @@ async function runCommands(options) {
120
145
  };
121
146
  if (!match.command || helpRequested) {
122
147
  help();
123
- return helpRequested || match.command ? 0 : 1;
148
+ return helpRequested || match.command ? 0 : 2;
124
149
  }
125
- const context = parseCommand(match.command, options.args, match.path);
150
+ const context = parseCommand(match.command, options.args, match.path, options.flags);
126
151
  if (context.values.help) {
127
152
  help();
128
153
  return 0;
129
154
  }
155
+ const flags = {};
156
+ for (const flag of options.flags ?? []) flags[flag.name] = context.values[flag.name];
130
157
  await options.run(match.command, {
131
158
  input: context.positionals,
132
- arguments: context.values
159
+ arguments: context.values,
160
+ flags
133
161
  });
134
162
  return 0;
135
163
  }
164
+ function isInteractive() {
165
+ return process.stdin.isTTY === true && process.stderr.isTTY === true;
166
+ }
167
+ async function prompt(question, fallback) {
168
+ if (!isInteractive()) throw new CommandError(`${question} Answer missing, pass it as an argument.`, 2);
169
+ const readline = createInterface({
170
+ input: process.stdin,
171
+ output: process.stderr
172
+ });
173
+ try {
174
+ return (await readline.question(fallback ? `${question} (${fallback}) ` : `${question} `)).trim() || fallback || "";
175
+ } finally {
176
+ readline.close();
177
+ }
178
+ }
179
+ async function confirm(question) {
180
+ const answer = await prompt(`${question} [y/N]`);
181
+ return [
182
+ "y",
183
+ "yes",
184
+ "j",
185
+ "ja"
186
+ ].includes(answer.toLowerCase());
187
+ }
188
+ async function select(question, choices) {
189
+ if (choices.length === 0) throw new CommandError(`${question} No options available.`, 2);
190
+ const lines = [question];
191
+ for (const [index, choice] of choices.entries()) lines.push(` ${index + 1}. ${choice.label}`);
192
+ while (true) {
193
+ const answer = Number(await prompt(`${lines.join("\n")}\n>`, "1"));
194
+ const choice = choices[answer - 1];
195
+ if (Number.isInteger(answer) && choice) return choice.value;
196
+ lines.splice(0, lines.length, `Choose a number from 1 to ${choices.length}.`);
197
+ }
198
+ }
199
+ function formatTable(header, rows) {
200
+ const widths = new Array();
201
+ for (const row of [header, ...rows]) for (const [index, cell] of row.entries()) widths[index] = Math.max(widths[index] ?? 0, cell.length);
202
+ const lines = new Array();
203
+ for (const row of [header, ...rows]) {
204
+ const cells = new Array();
205
+ for (const [index, cell] of row.entries()) if (index === row.length - 1) cells.push(cell);
206
+ else cells.push(cell.padEnd(widths[index] ?? 0));
207
+ lines.push(cells.join(" ").trimEnd());
208
+ }
209
+ return lines.join("\n");
210
+ }
211
+ function formatError(error, json = false) {
212
+ let message = String(error);
213
+ let exitCode = 1;
214
+ if (error instanceof CommandError) {
215
+ message = error.message;
216
+ exitCode = error.exitCode;
217
+ } else if (error instanceof Error) message = error.message;
218
+ if (json) return {
219
+ exitCode,
220
+ text: JSON.stringify({
221
+ error: message,
222
+ exitCode
223
+ })
224
+ };
225
+ return {
226
+ exitCode,
227
+ text: message
228
+ };
229
+ }
230
+ var USAGE_EXIT_CODE = 2;
136
231
  //#endregion
137
- export { createCommandTree, defineCommand, findCommand, formatFlag, formatHelp, formatSection, parseCommand, runCommands };
232
+ export { CommandError, USAGE_EXIT_CODE, confirm, createCommandTree, defineCommand, findCommand, formatError, formatFlag, formatHelp, formatSection, formatTable, isInteractive, parseCommand, prompt, runCommands, select, toCommandArguments };
package/dist/package.js CHANGED
@@ -1,6 +1,6 @@
1
1
  //#region package.json
2
2
  var name = "revojs";
3
- var version = "0.3.7";
3
+ var version = "0.3.8";
4
4
  var repository = {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/tellua/revojs.git"
@@ -40,7 +40,7 @@ var dependencies = {
40
40
  "vite": "^8.3.4"
41
41
  };
42
42
  var devDependencies = {
43
- "@revojs/tsconfig": "0.3.7",
43
+ "@revojs/tsconfig": "0.3.8",
44
44
  "@types/node": "^26.6.4"
45
45
  };
46
46
  var optionalDependencies = { "typescript": "^7.0.2" };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "revojs",
3
- "version": "0.3.7",
3
+ "version": "0.3.8",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -47,7 +47,7 @@
47
47
  "vite": "^8.3.4"
48
48
  },
49
49
  "devDependencies": {
50
- "@revojs/tsconfig": "0.3.7",
50
+ "@revojs/tsconfig": "0.3.8",
51
51
  "@types/node": "^26.6.4"
52
52
  },
53
53
  "optionalDependencies": {