All files io.ts

91.78% Statements 67/73
75% Branches 3/4
100% Functions 2/2
91.78% Lines 67/73

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 741x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x             5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import {
    isUndefined,
    formatError,
} from './util.js';
 
import type { CopyOptions, PathLike, RmOptions } from 'node:fs';
import type {
    SpawnSyncReturns,
    SpawnSyncOptionsWithStringEncoding,
    SpawnSyncOptionsWithBufferEncoding,
    SpawnSyncOptions
} from 'node:child_process';
import type { DistinctQuestion } from 'inquirer';
 
 
type ValueOf<T> = T[keyof T];
export interface IOPath {
    resolve: (...paths: string[]) => string;
}
export interface IOFs {
    cp: (source: string | URL, destination: string | URL, opts?: CopyOptions) => Promise<void>;
    rename: (oldPath: PathLike, newPath: PathLike) => Promise<void>;
    rm: (path: PathLike, options?: RmOptions) => Promise<void>;
}
type SpawnSyncFunctionString = (
    command: string,
    args?: readonly string[],
    options?: SpawnSyncOptions | SpawnSyncOptionsWithStringEncoding,
) => SpawnSyncReturns<string>;
type SpawnSyncFunctionBuffer = (
    command: string,
    args?: readonly string[],
    options?: SpawnSyncOptions | SpawnSyncOptionsWithBufferEncoding,
) => SpawnSyncReturns<Buffer>;
export interface IOChildProcess {
    spawnSync: SpawnSyncFunctionString | SpawnSyncFunctionBuffer;
}
export interface IOInquirer {
    prompt: <T>(
        question: DistinctQuestion
    ) => Promise<T>;
}
export interface IOMap {
    path: IOPath;
    fs: IOFs;
    child_process: IOChildProcess;
    inquirer: IOInquirer;
}
 
const io = new Map<keyof IOMap, ValueOf<IOMap>>();
 
const getIO = <T extends ValueOf<IOMap>>(
    k: keyof IOMap
): T => {
    const val = io.get(k) as (T | undefined);
    if (isUndefined(val)) {
        // Module load error
        formatError(new Error(
            `IO Error: ${k} module not loaded`
        ));
        process.exit(1);
    }
    return val;
}
 
const setIO = (
    cfg: Record<keyof IOMap, ValueOf<IOMap>>
): void => {
    const keys = Object.keys(cfg) as (keyof IOMap)[];
    keys.forEach(k => io.set(k, cfg[k]));
};
 
export { getIO, setIO };