@viewdo/dxp-story-cli
Version:
README.md
62 lines (51 loc) • 1.76 kB
JavaScript
const path = require('path');
const fs = require('fs');
const { common } = require('../../_utilities/file-names');
const isEmpty = require('lodash/isEmpty');
class HtmlWriter {
constructor(console = console) {
Object.assign(this, {
path,
fs,
common,
isEmpty,
console
});
}
write(data, state_template_path) {
const { footer = {}, header = {} } = data;
this._write_header(header, state_template_path);
this._write_footer(footer, state_template_path);
}
_write_header(header, state_template_path) {
const { fs, path, common, console } = this;
const { html: header_html = "" } = header;
const header_path = path.resolve(`${state_template_path}/${common.header()}`);
if (fs.existsSync(header_path)) {
if(console.debug) console.debug(`Can't write header at ${header_path} because it exists.`);
return
}
try {
fs.writeFileSync(header_path, header_html);
} catch (err) {
console.log(err);
throw err;
}
}
_write_footer(footer, state_template_path) {
const { fs, path, common, console } = this;
const { html: footer_html = "" } = footer;
const footer_path = path.resolve(`${state_template_path}/${common.footer()}`);
if (fs.existsSync(footer_path)) {
if(console.debug) console.debug(`Can't write footer at ${footer_path} because it exists.`);
return
}
try {
fs.writeFileSync(footer_path, footer_html);
} catch (err) {
console.log(err);
throw err;
}
}
}
module.exports = HtmlWriter;