Skip to content

Commit

Permalink
⚡ Improve result type
Browse files Browse the repository at this point in the history
  • Loading branch information
siguici committed Mar 27, 2024
1 parent 333794d commit e9f18ba
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ export type Handler = (path: Path) => Command;
export type Resolver = Command | Handler;
export type Cwd = string;
export type Env = Record<string, string | undefined>;
export type Result = ShellPromise & { command: string; args: string };
export type Result = ShellPromise;

export async function run(
path: Path,
resolver: Resolver,
cwd?: Cwd,
env?: Env,
): Promise<Result> {
): Promise<ShellPromise> {
resolver = typeof resolver === "function" ? resolver(path) : resolver;
resolver = Array.isArray(resolver) ? resolver : resolver.split(" ");

const command = which(resolver[0]);

if (!command) {
throw new Error(`Command (${command}) not found`);
throw new Error(`Command (${resolver.join(" ")}) not found`);
}

const args = resolver.slice(1).join(" ");
Expand All @@ -37,7 +37,7 @@ export async function run(

const result = await $`${command} ${args} ${path}`.quiet();

return { ...result, command, args };
return result;
}

export async function load(
Expand All @@ -46,15 +46,10 @@ export async function load(
cwd?: Cwd,
env?: Env,
): Promise<object> {
const { command, exitCode, stdout, stderr } = await run(
path,
resolver,
cwd,
env,
);
const { exitCode, stdout, stderr } = await run(path, resolver, cwd, env);

if (exitCode !== 0) {
throw new Error(`Failed to run ${command}: ${stderr.toString()}`);
throw new Error(`Failed to load ${path}: ${stderr.toString()}`);
}

return parse(stdout.toString());
Expand Down

0 comments on commit e9f18ba

Please sign in to comment.