Skip to content

Commit

Permalink
Improved async logic handling, async tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sukairo-02 committed Jun 27, 2024
1 parent de8a21d commit 7418c97
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 50 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@drizzle-team/brocli",
"type": "module",
"author": "Drizzle Team",
"version": "0.2.2",
"version": "0.2.3",
"description": "Typed CLI command runner",
"license": "Apache-2.0",
"sideEffects": false,
Expand All @@ -26,6 +26,7 @@
"devDependencies": {
"@arethetypeswrong/cli": "^0.15.3",
"@originjs/vite-plugin-commonjs": "^1.0.3",
"@types/clone": "^2.1.4",
"@types/node": "^20.12.13",
"dprint": "^0.46.2",
"tsup": "^8.1.0",
Expand All @@ -51,5 +52,8 @@
"types": "./index.d.ts",
"default": "./index.js"
}
},
"dependencies": {
"clone": "^2.1.2"
}
}
18 changes: 18 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 23 additions & 11 deletions src/command-core.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import clone from 'clone';
import { BroCliError } from './brocli-error';
import {
type GenericBuilderInternals,
Expand Down Expand Up @@ -106,11 +107,19 @@ const invalidStringSyntax = (matchedName: string) => {
};

const enumViolation = (matchedName: string, data: string | undefined, values: [string, ...string[]]) => {
return new Error(``);
return new Error(
`Invalid value: value for the argument '${matchedName}' must be either one of the following: ${
values.join(', ')
}; Received: ${data}`,
);
};

const enumViolationPos = (matchedName: string, data: string | undefined, values: [string, ...string[]]) => {
return new Error(``);
return new Error(
`Invalid value: value for the argument '${matchedName}' must be either one of the following: ${
values.join(', ')
}; Received: ${data}`,
);
};

const invalidNumberSyntax = (matchedName: string) => {
Expand Down Expand Up @@ -149,11 +158,13 @@ const generatePrefix = (name: string) => name.startsWith('-') ? name : name.leng
const validateOptions = <TOptionConfig extends Record<string, GenericBuilderInternals>>(
config: TOptionConfig,
): ProcessedOptions<TOptionConfig> => {
const cloned = clone(config);

const entries: [string, GenericBuilderInternalsFields][] = [];

const storedNames: Record<string, [string, ...string[]]> = {};

const cfgEntries = Object.entries(config);
const cfgEntries = Object.entries(cloned);

for (const [key, value] of cfgEntries) {
const cfg = value._.config;
Expand Down Expand Up @@ -468,9 +479,10 @@ const helpCommand = (commands: Command[], helpHandler: Function | string) =>
});

const validateCommands = (commands: Command[]) => {
const cloned = clone(commands);
const storedNames: Record<string, [string, ...string[]]> = {};

for (const cmd of commands) {
for (const cmd of cloned) {
const storageVals = Object.values(storedNames);

for (const storage of storageVals) {
Expand Down Expand Up @@ -502,13 +514,13 @@ const validateCommands = (commands: Command[]) => {
: [cmd.name];
}

return commands;
return cloned;
};

/**
* Separated for testing purposes
*/
export const rawCli = (commands: Command[], config?: BroCliConfig) => {
export const rawCli = async (commands: Command[], config?: BroCliConfig) => {
let options: Record<string, OutputType> | undefined;
let cmd: RawCommand<any>;

Expand All @@ -520,7 +532,7 @@ export const rawCli = (commands: Command[], config?: BroCliConfig) => {
const cmds = [...rawCmds, helpCommand(rawCmds, helpHandler)];

let args = argSource.slice(2, argSource.length);
if (!args.length) return executeOrLog(helpHandler);
if (!args.length) return await executeOrLog(helpHandler);

const helpIndex = args.findIndex((arg) => arg === '--help' || arg === '-h');
if (helpIndex !== -1 && (helpIndex > 0 ? args[helpIndex - 1]?.startsWith('-') ? false : true : true)) {
Expand All @@ -539,7 +551,7 @@ export const rawCli = (commands: Command[], config?: BroCliConfig) => {
command = command ?? getCommand(cmds, args).command;
}

return command ? executeOrLog(command.help) : executeOrLog(helpHandler);
return command ? await executeOrLog(command.help) : await executeOrLog(helpHandler);
}

const versionIndex = args.findIndex((arg) => arg === '--version' || arg === '-v');
Expand All @@ -554,7 +566,7 @@ export const rawCli = (commands: Command[], config?: BroCliConfig) => {
options = parseOptions(command, args);
cmd = command;

cmd.handler(options);
await cmd.handler(options);
return undefined;
};

Expand All @@ -565,9 +577,9 @@ export const rawCli = (commands: Command[], config?: BroCliConfig) => {
*
* @param argSource - source of cli arguments, optionally passed as a parameter for testing purposes and compatibility with custom environments
*/
export const runCli = (commands: Command[], config?: BroCliConfig) => {
export const runCli = async (commands: Command[], config?: BroCliConfig) => {
try {
rawCli(commands, config);
await rawCli(commands, config);
} catch (e) {
if (e instanceof BroCliError) throw e;

Expand Down
Loading

0 comments on commit 7418c97

Please sign in to comment.