All files / src/cli cli.ts

0% Statements 0/106
0% Branches 0/1
0% Functions 0/1
0% Lines 0/106

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 116 117 118 119 120 121 122                                                                                                                                                                                                                                                   
import module from 'node:module';
import { styleText } from 'node:util';
import pkg from '../../package.json' with { type: 'json' };
 
import { COMMAND, Esmx, type EsmxOptions } from '../core';
import { resolveImportPath } from '../utils/resolve-path';
 
async function getSrcOptions(): Promise<EsmxOptions> {
    return import(resolveImportPath(process.cwd(), './src/entry.node.ts')).then(
        (m) => m.default
    );
}
 
async function getDistOptions(): Promise<EsmxOptions> {
    try {
        const m = await import(
            resolveImportPath(process.cwd(), './dist/node/src/entry.node.mjs')
        );
        return m.default;
    } catch (e) {
        console.error(
            styleText(
                'red',
                'Failed to load dist entry: dist/node/src/entry.node.mjs'
            )
        );
        console.error(styleText('yellow', 'Run `esmx build` and try again.'));
        process.exit(17);
    }
}
 
export async function cli(command: string) {
    console.log(`🔥 ${styleText('yellow', 'Esmx')} v${pkg.version}
    `);
    if (
        command !== COMMAND.dev &&
        typeof process.env.NODE_ENV === 'undefined'
    ) {
        process.env.NODE_ENV = 'production';
    }
    let esmx: Esmx | null;
    let opts: EsmxOptions | null = null;
    switch (command) {
        case COMMAND.dev:
            opts = await getSrcOptions();
            esmx = new Esmx(opts);
            exit(await esmx.init(COMMAND.dev));
 
            esmx = null;
            opts = null;
            break;
        case COMMAND.start:
            opts = await getDistOptions();
            esmx = new Esmx(opts);
            exit(await esmx.init(COMMAND.start));
 
            esmx = null;
            opts = null;
            break;
        case COMMAND.build:
            opts = await getSrcOptions();
            esmx = new Esmx(opts);
            exit(await esmx.init(COMMAND.build));
            exit(await esmx.destroy());
 
            if (typeof opts.postBuild === 'function') {
                esmx = new Esmx({
                    ...opts,
                    server: undefined
                });
                exit(await esmx.init(COMMAND.start));
                exit(await esmx.postBuild());
            }
 
            esmx = null;
            opts = null;
            break;
        case COMMAND.preview:
            opts = await getSrcOptions();
 
            esmx = new Esmx(opts);
            exit(await esmx.init(COMMAND.build));
            exit(await esmx.destroy());
 
            esmx = new Esmx(opts);
            exit(await esmx.init(COMMAND.start));
            exit(await esmx.postBuild());
 
            esmx = null;
            opts = null;
            break;
        default:
            await import(resolveImportPath(process.cwd(), command));
            break;
    }
}
 
function exit(ok: boolean) {
    if (!ok) {
        process.exit(17);
    }
}
 
module.register(import.meta.url, {
    parentURL: import.meta.url
});
 
export function resolve(
    specifier: string,
    context: Record<string, any>,
    nextResolve: Function
) {
    if (
        context?.parentURL.endsWith('.ts') &&
        specifier.startsWith('.') &&
        !specifier.endsWith('.ts')
    ) {
        return nextResolve(specifier + '.ts', context);
    }
    return nextResolve(specifier, context);
}