All files / lib/api mooltipage.ts

100% Statements 22/22
100% Branches 10/10
100% Functions 6/6
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 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 123 124 125 126 127 128 129 130 131 132 13326x   26x                                                                                             26x 3x           26x                                   8x 6x   2x     8x               5x 8x                   9x     9x 6x                     8x     8x     8x       8x 1x     7x 7x 7x    
import {PipelineIOImpl, StandardPipeline} from '../pipeline/standardPipeline';
import {HtmlFormatter, Page, Pipeline, PipelineIO} from '..';
import {createStandardHtmlFormatter, FormatterMode} from '../pipeline/module/standardHtmlFormatter';
 
/**
 * Called whenever a page is compiled.
 * @param page Compiled Page object
 */
export type PageCompiledCallback = (page: Page) => Promise<void>;
 
/**
 * Options recognized by Mooltipage
 * See {@link DefaultMpOptions} for default values
 */
export interface MpOptions {
    /**
     * Optional path to look for input files.
     * Defaults to current working directory.
     */
    readonly inPath?: string;
 
    /**
     * Optional path to place output files.
     * Defaults to current working directory.
     */
    readonly outPath?: string;
 
    /**
     * Optional custom pipeline IO to use.
     * The implementation MUST be configured to use the same values of {@link inPath} and {@link outPath}, if applicable.
     */
    readonly pipelineIO?: PipelineIO;
 
    /**
     * Optional name of the HTML formatter to use.
     * Defaults to standard formatter in "pretty" mode.
     * Recommended to switch to "minimized" mode for production builds.
     */
    readonly formatter?: string;
 
    /**
     * Optional callback for page compilation.
     */
    readonly onPageCompiled?: PageCompiledCallback;
}
 
/**
 * Default Mooltipage options
 */
export class DefaultMpOptions implements MpOptions {
    readonly formatter: FormatterMode.PRETTY = FormatterMode.PRETTY;
}
 
/**
 * Mooltipage JS API entry point.
 */
export class Mooltipage {
    /**
     * Configuration objects for this Mooltipage instance
     */
    readonly options: MpOptions;
 
    /**
     * Pipeline instance that will be used for the lifetime of this instance
     */
    readonly pipeline: Pipeline;
 
    /**
     * Constructs a new Mooltipage instance.
     * An options object can be passed to configure the instance.
     * If no options are provided, then {@link DefaultMpOptions} will be used.
     * @param options Configuration options
     */
    constructor(options?: MpOptions) {
        if (options !== undefined) {
            this.options = options;
        } else {
            this.options = new DefaultMpOptions();
        }
 
        this.pipeline = createPipeline(this.options);
    }
 
    /**
     * Compiles a list of pages.
     * @param pagePaths List paths to pages to compile
     */
    async processPages(pagePaths: Iterable<string>): Promise<void> {
        for (const pagePath of pagePaths) {
            await this.processPage(pagePath);
        }
    }
 
    /**
     * Compiles a single page.
     * @param pagePath Path to page to compile
     */
    async processPage(pagePath: string): Promise<void> {
        // compile page
        const page = await this.pipeline.compilePage(pagePath);
 
        // callback
        if (this.options.onPageCompiled) {
            await this.options.onPageCompiled(page);
        }
    }
}
 
/**
 * Creates a pipeline from an options object
 * @param options Options object to configure pipeline
 */
function createPipeline(options: MpOptions): Pipeline {
    // create the HTML formatter, if specified
    const formatter: HtmlFormatter = createStandardHtmlFormatter(options.formatter);
 
    // create pipeline IO
    const pipelineIO = createPipelineIO(options);
 
    // create pipeline
    return new StandardPipeline(pipelineIO, formatter);
}
 
function createPipelineIO(options: MpOptions): PipelineIO {
    if (options.pipelineIO !== undefined) {
        return options.pipelineIO;
 
    } else {
        const sourcePath = options.inPath ?? process.cwd();
        const destinationPath = options.outPath ?? process.cwd();
        return new PipelineIOImpl(sourcePath, destinationPath);
    }
}