const fs = require('fs') const Handlebars = require('handlebars') const { ifEq } = require('./helpers') const ListingSet = require('./ListingSet') const i18n = require('./i18n.json') Handlebars.registerHelper('if_eq', ifEq) class Output { constructor () { this.items = [] this.timestamp = null } addItem (...p) { this.items.push(new ListingSet(...p)) } async _compileTemplate () { const templateFile = await fs.readFileSync('./src/output/template.hbs', 'utf8') return Handlebars.compile(templateFile) } async _writeToFile (filename, content) { try { await fs.unlinkSync(filename) } catch { console.log('File probably doesnt exist') } fs.writeFileSync(filename, content, 'utf8') } async submit () { this.timestamp = new Date() const template = await this._compileTemplate() Object.keys(i18n).forEach(locale => { const filename = i18n[locale].link || 'index' this._writeToFile(`./public/${filename}.html`, template({ ...this, t: i18n[locale], languages: i18n })) }) } } module.exports = new Output()