html-build
Version:
Utility script to build HTML documents - Appends scripts and styles, removes debug parts, append HTML partials, template options, etc.
47 lines (46 loc) • 1.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const path = require("path");
const params_1 = require("./lib/params");
const util_1 = require("./lib/util");
const processors_1 = require("./lib/processors");
function build(src, dest, options) {
const params = params_1.getParams(src, options);
const destPath = getDestPath(src, dest, params);
const content = createContent(src, dest, params);
// write the contents to destination
util_1.writeFile(destPath, content);
}
exports.build = build;
function getDestPath(src, dest, params) {
// replace files in the same folder
if (params.replace) {
return src;
}
// copy original folder structure into dest folder and compile templates
else if (params.basePath || params.basePath === "") {
// new path = dest + (src path without basePath at the beginning)
return dest + src.substring(params.basePath.length, src.length);
}
// Regex for path
else if (util_1.isFile(dest)) {
return dest;
}
// default: copy all files into destination
else {
return path.join(dest, path.basename(src));
}
}
function createContent(src, dest, params) {
let content = util_1.readFile(src);
content = ensureContent(content, params);
content = processors_1.transformContent(content, params, dest);
return content;
}
function ensureContent(content, params) {
if (!params.EOL) {
const match = content.match(/\r?\n/);
params.EOL = match ? match[0] : "\n";
}
return content.replace(/\r?\n/g, params.EOL);
}