UNPKG

@shockpkg/dir-projector

Version:

Package for creating Shockwave Director projectors

118 lines (108 loc) 2.71 kB
import { basename, dirname, join as pathJoin } from 'node:path'; import { mkdir, writeFile } from 'node:fs/promises'; import { Bundle } from "../bundle.mjs"; import { ProjectorHtml } from "../projector/html.mjs"; import { htmlEncode, trimExtension } from "../util.mjs"; /** * BundleHtml object. */ export class BundleHtml extends Bundle { /** * ProjectorHtml instance. */ /** * BundleHtml constructor. * * @param path Output path. * @param flat Flat bundle. */ constructor(path, flat = false) { super(path, flat); this.projector = this._createProjector(); } /** * Main application file extension. * * @returns File extension. */ get extension() { const a = basename(this.path).split('.'); const ext = a.length > 1 ? a.pop() : ''; if (!ext) { throw new Error('Failed to extract extension'); } return `.${ext}`; } /** * Get the nested subdirectory. * * @returns Directory name. */ get subdir() { return trimExtension(basename(this.path), this.extension); } /** * Get the nested index. * * @returns File name. */ get index() { return `index${this.extension}`; } /** * Get launcher HTML code. * * @returns HTML code. */ getLauncher() { const { projector, subdir, index } = this; const { lang, title } = projector; const path = `${subdir}/${index}`; const url = path.split(/[/\\]/).map(encodeURIComponent).join('/'); const hAttr = lang === null ? '' : ` lang="${htmlEncode(lang, true)}"`; return ['<!DOCTYPE html>', `<html${hAttr}>`, ' <head>', ' <meta charset="UTF-8">', ' <meta http-equiv="X-UA-Compatible" content="IE=Edge">', ` <meta http-equiv="refresh" content="0; url=${htmlEncode(url, true)}">`, ...(title === null ? [] : [` <title>${htmlEncode(title)}</title>`]), ' </head>', ' <body></body>', '</html>', ''].map(s => s.replace(/^\s+/, s => '\t'.repeat(s.length))).join('\n'); } /** * @inheritdoc */ async _close() { if (!this.flat) { await this._writeLauncher(); } await super._close(); } /** * @inheritdoc */ _getProjectorPathNested() { return pathJoin(dirname(this.path), this.subdir, this.index); } /** * Create projector instance for the bundle. * * @returns Projector instance. */ _createProjector() { return new ProjectorHtml(this._getProjectorPath()); } /** * Write the launcher file. */ async _writeLauncher() { const { path } = this; await mkdir(dirname(path), { recursive: true }); await writeFile(path, this.getLauncher()); } } //# sourceMappingURL=html.mjs.map