UNPKG

statigen

Version:

A static site generator that supports html, ejs, and markdown source files

100 lines 3.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StaticSiteGenerator = void 0; const parcelWatcher = require("@parcel/watcher"); const fastGlob = require("fast-glob"); const path = require("path"); const debounce = require("debounce"); const liveServer = require("@compodoc/live-server"); const chalk = require("chalk"); const util_1 = require("./util"); const Project_1 = require("./Project"); const diagnosticUtils_1 = require("./diagnosticUtils"); class StaticSiteGenerator { async run(options) { this.createProject(options); this.build(); const diagnostics = this.project.getDiagnostics(); (0, diagnosticUtils_1.printDiagnostics)(diagnostics); if (this.project.options.watch) { //return a promise that never resolves return new Promise(() => { // eslint-disable-next-line no-void void this.watch(); }); } else { if (diagnostics.length > 0) { throw new Error(`Found ${diagnostics.length} errors during publish`); } } } createProject(options) { this.project = new Project_1.Project(options); //load all the files into the project const filePaths = fastGlob //find all the files .sync(this.project.options.files, { cwd: this.project.options.sourceDir, absolute: false }) //use the platform-specific path separator .map(x => x.replace(/[\\\/]/g, path.sep)); //add all files to the project for (const filePathRelative of filePaths) { this.project.setFile(filePathRelative); } } build() { (0, util_1.log)('Building project...'); this.project.validate(); this.project.publish(); (0, util_1.log)('Done!'); } async watch() { console.log('Starting watcher'); if (this.watchInterval) { clearInterval(this.watchInterval); } //keep the process alive indefinitely by setting an interval that runs once every 12 days this.watchInterval = setInterval(() => { }, 1073741824); liveServer.start({ root: this.project.options.outDir, open: true, logLevel: 0 }); const build = debounce(() => { try { this.build(); (0, diagnosticUtils_1.printDiagnostics)(this.project.getDiagnostics()); } catch (e) { console.error(e); } }); console.log('Watching dir: ', this.project.options.sourceDir); this.watcher = await parcelWatcher.subscribe(this.project.options.sourceDir, (err, events) => { for (const event of events !== null && events !== void 0 ? events : []) { if (event.type === 'create') { (0, util_1.log)('File added', chalk.green(event.path)); this.project.setFile(event.path); } else if (event.type === 'update') { (0, util_1.log)('File changed', chalk.green(event.path)); this.project.setFile(event.path); } else if (event.type === 'delete') { (0, util_1.log)('File removed', chalk.green(event.path)); this.project.removeFile(event.path); } } build(); }); } async destroy() { var _a; await ((_a = this.watcher) === null || _a === void 0 ? void 0 : _a.unsubscribe()); } } exports.StaticSiteGenerator = StaticSiteGenerator; //# sourceMappingURL=StaticSiteGenerator.js.map