You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
webpack-plugin/src/siteomat.js

119 lines
3.3 KiB

2 years ago
const fs = require('fs')
const path = require('path')
const mkdirp = require('mkdirp')
const assign = require('assign-deep')
2 years ago
1 year ago
const configStore = require('./config.js')
const Engine = require('./engines/nunjucks.js')
const Sitemap = require('./factories/sitemap.js')
2 years ago
2 years ago
const PagesQuery = require('./queries/pages.js')
const parseYamlFile = require('./parsers/yaml.js')
2 years ago
/**
1 year ago
* Siteomat
2 years ago
*
*
2 years ago
*
* @author Björn Hase <me@herr-hase.wtf>
* @license http://opensource.org/licenses/MIT The MIT License
1 year ago
* @link https://gitea.node001.net/HerrHase/siteomat-webpack-plugin.git
2 years ago
*
2 years ago
*/
1 year ago
class Siteomat {
2 years ago
/**
*
*
* @param {string} source
* @param {string} destination
*
*/
1 year ago
constructor(source, views, options = {}) {
if (options.destination === undefined) {
throw new Error('Destination is undefined')
}
2 years ago
this._source = source
this._views = views
1 year ago
this._destination = options.destination
2 years ago
1 year ago
// fill singleton for configuration
2 years ago
configStore.set('source', source)
1 year ago
configStore.set('destination', this._destination)
2 years ago
configStore.set('views', views)
configStore.set('options', assign({
'minifyHtml': true
}, options))
2 years ago
2 years ago
// get config for site
if (fs.existsSync(this._source + '/site.yml')) {
const file = fs.readFileSync(this._source + '/site.yml', 'utf8')
this._site = parseYamlFile(file)
} else {
throw new Error('site.yml not found in ' + this._source + '!')
}
1 year ago
if (fs.existsSync(this._source + '/json.yml')) {
const file = fs.readFileSync(this._source + '/json.yml', 'utf8')
this._json = parseYamlFile(file)
}
2 years ago
configStore.set('site', this._site)
2 years ago
this._engine = new Engine(views, this._site)
}
/**
* let it rain \o/
*
*/
run() {
const query = new PagesQuery(this._source)
const results = query.find()
const sitemap = new Sitemap(this._site)
// run through pages and generate html files
results.forEach((page, index) => {
this._engine.render(page, (error, content) => {
2 years ago
2 years ago
// show errors
if (error) {
console.error(error)
}
// if no content show error message
if (!content) {
console.error('Error! Rendering Page ' + '"' + page.filename + '" is null')
return;
}
2 years ago
2 years ago
// create directories and write file = page
2 years ago
mkdirp(this._destination + page.pathname).then(() => {
fs.writeFileSync(this._destination + page.pathname + '/' + page.filename, content)
})
sitemap.addPage(page)
// if run is finish, write sitemap.xml
if ((index + 1) === results.length) {
fs.writeFileSync(this._destination + '/sitemap.xml', sitemap.getXmlAsString())
}
2 years ago
})
2 years ago
})
1 year ago
if (this._json) {
for (const [name, options] of Object.entries(this._json)) {
const json = query.find(options)
fs.writeFileSync(this._destination + '/' + name + '.json', JSON.stringify(json))
}
}
2 years ago
}
}
1 year ago
module.exports = Siteomat