All files / bin cli.ts

100% Statements 22/22
100% Branches 6/6
100% Functions 4/4
100% Lines 22/22

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 791x       1x     1x 1x   1x 4x 4x 4x     4x   4x 2x 2x 1x   1x         3x 1x   2x       1x   3x     3x     1x 3x                                                                    
import {
    CliArgs,
    parseArgs
} from './args';
import {
    readPackageJson
} from './cliFs';
import {CliEngine} from './cliEngine';
import {WatchingCliEngine} from './watch/watchingCliEngine';
 
export async function cliMain(argv: string[], cliConsole: CliConsole): Promise<void> {
    const version = (await readPackageJson()).version;
    cliConsole.log(`Mooltipage CLI ver. ${ version }`);
    cliConsole.log();
 
    // parse arguments
    const args: CliArgs = parseArgs(argv);
 
    if (args.isHelp) {
        printHelp(cliConsole);
    } else if (args.pages.length === 0) {
        cliConsole.log('No input pages specified.');
    } else {
        await runApp(args, cliConsole);
    }
}
 
function makeCliEngine(args: CliArgs, cliConsole: CliConsole): CliEngine {
    if (args.watch) {
        return new WatchingCliEngine(args, cliConsole);
    } else {
        return new CliEngine(args, cliConsole);
    }
}
 
export async function runApp(args: CliArgs, cliConsole: CliConsole): Promise<void> {
    // Create an engine instance based on args
    const cliEngine = makeCliEngine(args, cliConsole);
 
    // Run it
    await cliEngine.runApp();
}
 
export function printHelp(cliConsole: CliConsole): void {
    cliConsole.log(
        `Usage: mooltipage [options] <page1> [page2 [page3...]]
        Pages can be individual files or entire directories.
        Directories will be recursively searched, and all HTML contents will be treated as pages.
        If inPath and outPath are not specified, then they will default to the current working directory.
        If no outPath is specified, then source pages will be overwritten when compiled.
        
        Options:
            --help                               Print this help and exit.
            --inPath=<input_path>                Specify a folder to read inputs from
            --outPath=<output_path>              Specify a folder to write outputs to
            --formatter=<formatter_name>         Use an HTML formatter (default "pretty")`
    );
}
 
/**
 * Console interface to receive output from CLI
 */
export interface CliConsole {
    /**
     * Receives a console message at "log" level.
     * Should function exactly the same as {@link console.log}.
     * @param message Message to log
     * @param optionalParams Optional parameters to attach to the message.
     */
    log(message?: unknown, ...optionalParams: unknown[]): void;
 
    /**
     * Receives a console message at "error" level.
     * Should function exactly the same as {@link console.error}.
     * @param message Message to log
     * @param optionalParams Optional parameters to attach to the message.
     */
    error(message?: unknown, ...optionalParams: unknown[]): void;
}