UNPKG

markugen

Version:

Markdown to HTML/PDF static site generation tool

88 lines (87 loc) 2.55 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const markugen_1 = require("./markugen"); const colors_1 = __importDefault(require("colors")); /** * Abstract base class for all generators */ class Generator { /** * Instance of Markugen */ mark; /** * The type of the generator */ type; /** * The generate start time for recording elapsed time */ startTime; /** * Used internally to prevent multiple async calls to {@link generate} */ isActive = false; /** * Constructs a new generator with the given Markugen instance and options * @param mark the instance of {@link Markugen} * @param options the common {@link MarkugenOptions} */ constructor(mark, options) { this.mark = mark; if (options) this.mark.options = options; this.type = this.constructor.name.replace(/generator/i, '').toLowerCase(); } /** * Initializes generation */ start() { if (this.isActive) throw new Error('Generator already active, cannot call generate while active'); this.isActive = true; this.startTime = process.hrtime(); // write the html files this.group(colors_1.default.green('Generating:'), this.type); } /** * Computes the elapsed time and finishes everything */ finish() { const end = process.hrtime(this.startTime); const ms = end[0] * 1000 + end[1] / 1000000; const elapsed = (0, markugen_1.timeFormat)(ms, { fixed: 2 }); this.isActive = false; this.groupEnd(); this.log('Generating Finished:', elapsed); } /** * Starts a console group */ group(...args) { this.mark.group(...args); } /** * Ends a console group */ groupEnd() { this.mark.groupEnd(); } /** * Use in place of console.log so the app can handle coloring * and any cli options that were given */ log(label, ...args) { this.mark.log(label, ...args); } /** * Use in place of console.log so the app can handle coloring * and any cli options that were given */ warning(...args) { this.mark.warning(...args); } /** * Outputs the given error message * @param e the error to log */ error(e) { this.mark.error(e); } } exports.default = Generator;