All files / infrastructure/scripts/src/shared/domain cli.ts

100% Statements 30/30
96.15% Branches 25/26
100% Functions 9/9
100% Lines 25/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83                                                          30x 43x 43x     6x 38x 38x 22x 28x     6x         6x 6x   6x 3x         3x             6x 4x 4x     6x 6x   6x 10x 8x   6x 10x   10x          
import { relative } from "node:path";
import { CancelledError, ExpectedError } from "../errors.ts";
 
export interface Args {
    flags: ReadonlySet<string>;
    positionals: string[];
}
 
interface ErrorReport {
    cancelled: boolean;
    message: string;
}
 
interface MatchedDispatch<T> {
    matched: true;
    command: T;
    args: {
        flags: ReadonlySet<string>;
        positionalsWithoutCommandName: string[];
    };
}
 
interface UnmatchedDispatch {
    matched: false;
    usage: string;
}
 
type Dispatch<T> = MatchedDispatch<T> | UnmatchedDispatch;
 
export const parse = (argv: string[]): Args => ({
    flags: new Set(argv.filter((arg) => arg.startsWith("--")).map((arg) => arg.slice(2))),
    positionals: argv.filter((arg) => !arg.startsWith("--")),
});
 
export const optionValue = (flags: ReadonlySet<string>, name: string): string | undefined => {
    const prefix = `${name}=`;
    for (const flag of flags)
        if (flag.startsWith(prefix)) return flag.slice(prefix.length) || undefined;
    return undefined;
};
 
export const dispatch = <T>(
    commands: Record<string, T>,
    args: Args,
    commandInvocation: string,
): Dispatch<T> => {
    const [name, ...positionalsWithoutCommandName] = args.positionals;
    const command = name === undefined ? undefined : commands[name];
 
    if (!command)
        return {
            matched: false,
            usage: `Usage: ${commandInvocation} <${Object.keys(commands).join("|")}>`,
        };
 
    return {
        matched: true,
        command,
        args: { flags: args.flags, positionalsWithoutCommandName },
    };
};
 
export const invocation = (entry: string, cwd: string): string => {
    const path = relative(cwd, entry);
    return `node ${path && !path.startsWith("..") ? path : entry}`;
};
 
export const detailOf = (error: unknown): string | undefined =>
    (error as { stderr?: Buffer | string }).stderr?.toString().trim() || undefined;
 
export const report = (error: unknown): ErrorReport => {
    if (error instanceof CancelledError) return { cancelled: true, message: error.message };
    if (error instanceof ExpectedError) return { cancelled: false, message: error.message };
 
    const stack = error instanceof Error ? (error.stack ?? error.message) : String(error);
    const detail = detailOf(error);
    // Node already appends stderr to an execFileSync error's message, and so to its stack.
    return {
        cancelled: false,
        message: detail && !stack.includes(detail) ? `${stack}\n${detail}` : stack,
    };
};