70 lines
1.6 KiB
JavaScript
Raw Normal View History

2020-02-21 20:05:52 -08:00
2020-02-25 19:37:10 -08:00
const createAssetFinder = require('./assets');
const Cache = require('./cache');
2020-02-21 20:05:52 -08:00
2020-02-25 19:37:10 -08:00
const evaluate = require('./evaluate');
2020-02-21 20:05:52 -08:00
2020-02-25 19:37:10 -08:00
const pages = require('./pages');
2020-02-21 20:05:52 -08:00
2020-02-25 19:37:10 -08:00
const twitter = require('./twitter');
const favicon = require('./favicon');
const assets = () => createAssetFinder().then(({ tasks }) => tasks);
2020-02-21 20:05:52 -08:00
2020-02-25 19:37:10 -08:00
exports.everything = function (prod = false) {
const fn = async () => {
2020-02-21 20:05:52 -08:00
2020-02-25 19:37:10 -08:00
const AssetFinder = await createAssetFinder();
2020-02-21 20:05:52 -08:00
2020-02-25 19:37:10 -08:00
await pages.parse(AssetFinder);
2020-02-21 20:05:52 -08:00
2020-02-25 19:37:10 -08:00
const tasks = await Promise.all([
AssetFinder.tasks,
twitter(prod),
favicon(prod),
]);
2020-02-21 20:05:52 -08:00
2020-02-25 19:37:10 -08:00
if (!tasks.length) return;
2020-02-21 20:05:52 -08:00
2020-02-25 19:37:10 -08:00
const cache = new Cache({ prod });
await cache.load();
await evaluate(tasks.flat(), cache);
await cache.save();
2020-02-21 20:05:52 -08:00
2020-02-25 19:37:10 -08:00
await pages.write(prod);
};
2020-02-21 20:05:52 -08:00
2020-02-25 19:37:10 -08:00
const ret = () => fn().catch((err) => { console.log(err.trace || err); throw err; });
ret.displayName = prod ? 'generateEverythingForProd' : 'generateEverything';
return ret;
};
2020-02-21 20:05:52 -08:00
2020-02-25 19:37:10 -08:00
exports.task = function (action, prod = false) {
let fn;
if (action === 'parse') {
fn = () => pages.parse();
} else if (action === 'pages') {
fn = () => pages.write(prod);
} else {
fn = async () => {
const tasks = await {
twitter,
favicon,
assets,
}[action](prod);
if (!tasks.length) return;
const cache = new Cache({ prod });
await cache.load();
await evaluate(tasks, cache);
await cache.save();
};
}
2020-02-21 20:05:52 -08:00
2020-02-25 19:37:10 -08:00
const ret = () => fn().catch((err) => { console.log(err.trace || err); throw err; });
ret.displayName = prod ? action + 'ForProd' : action;
2020-02-21 20:05:52 -08:00
2020-02-25 19:37:10 -08:00
return ret;
};