All files util.ts

80.86% Statements 93/115
71.42% Branches 5/7
71.42% Functions 5/7
80.86% Lines 93/115

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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 1161x 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 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  
import { getIO } from './io.js';
 
import { IOFs, IOPath, IOChildProcess } from './io.js';
 
const DEBUG = process.env['DEBUG'] ?? false;
const TEMPLATE_COPY_OPTIONS = {
    recursive: true,
    force: false,
    errorOnExist: true,
    dereference: true
};
const TEMPLATE_COPY_OPTIONS_FORCE = {
    recursive: true,
    force: true,
    errorOnExist: false,
    dereference: true
};
 
const isUndefined = (val: unknown): val is undefined =>
    typeof val === 'undefined';
 
const getTemplateDir = (fileUrl: string, relativePath: string) => {
    const url = new URL(fileUrl);
    const dir = getIO<IOPath>('path').resolve(url.pathname, relativePath);
    return dir;
};
 
// Error types in DefinitelyTyped for @types/node are currently
// a bit of a mess so replacing them wholesale here for now
interface CustomNodeGenericError {
    code: string;
    message: string;
    captureStackTrace: (target: NonNullable<unknown>, constructorOpt?: () => void) => void;
    stackTraceLimit: number;
    stack: string;
 
    cause?: {
        error?: CustomNodeError;
    };
}
export interface CustomNodeSystemError extends CustomNodeGenericError {
    errno: number;
    syscall: string;
 
    address: string;
    dest: string;
    info: Record<string, string>;
    path: string;
    port?: number;
}
 
type CustomNodeError = CustomNodeGenericError | CustomNodeSystemError;
 
const formatError = (err: CustomNodeError | Error): void => {
    // Cast all Errors to CustomNodeError due to DefinitelyTyped typing bugs
    err = err as CustomNodeError;
    const originalError = err.cause?.error ?? err;
    const message = `${err.message}${err.cause?.error ? `: ${originalError.message}` : ''}`;

    console.error(`
        ${message}

        Exiting...
    `, err);
}
 
const ignoreError = (e: unknown) => {
    DEBUG && console.log({ e })
};
 
const runCommand = (cwd = '.', cmd: string, args: string[] = []) => {
    const {
        pid, stdout, stderr, status, signal, error
    } = getIO<IOChildProcess>('child_process').spawnSync(cmd, args, {
      cwd,
      stdio: 'inherit',
      encoding: 'utf8'
    });
    if (error ?? status !== 0) {
        formatError(new Error(error?.message, {
            cause: {
                error,
                stderr,
                signal,
                pid
            }
        }));
    }
    return stdout;
};
 
const npmInstall = (cwd = '.', npm = 'npm') => {
    return runCommand(cwd, npm, ['install']);
};
 
const copyTemplate = async (
    templatePath: string,
    { force = false } = {}
): Promise<void> => {
    const options = force ? TEMPLATE_COPY_OPTIONS_FORCE : TEMPLATE_COPY_OPTIONS;
    const dest = process.cwd();
    const { cp, rename, rm } = getIO<IOFs>('fs');
    await cp(templatePath, dest, options);
    await rename(`${dest}/_.npmrc`, `${dest}/.npmrc`).catch(ignoreError);
    await rename(`${dest}/_.gitignore`, `${dest}/.gitignore`).catch(ignoreError);
    await rm(`${dest}/package-lock.json`).catch(ignoreError);
};
 
export {
    isUndefined,
    getTemplateDir,
    npmInstall,
    formatError,
    copyTemplate
};