139 lines
4.2 KiB
JavaScript
Raw Normal View History

2020-02-21 20:05:52 -08:00
2020-02-29 16:27:55 -08:00
process.env.BLUEBIRD_DEBUG = true;
2020-02-27 18:57:39 -08:00
const loadPublicFiles = require('./public');
2020-02-29 16:27:55 -08:00
const loadPostFiles = require('./posts');
2020-02-25 19:37:10 -08:00
const Cache = require('./cache');
2020-02-27 18:57:39 -08:00
const Promise = require('bluebird');
const fs = require('fs-extra');
const { sortBy } = require('lodash');
2020-02-21 20:05:52 -08:00
2020-03-07 18:04:37 -08:00
const getEngines = require('./engines');
2020-02-27 18:57:39 -08:00
const primeTweets = require('./page-tweets');
const pageWriter = require('./page-writer');
2020-02-25 19:37:10 -08:00
const evaluate = require('./evaluate');
2020-02-27 18:57:39 -08:00
const { resolve } = require('./resolve');
2020-02-21 20:05:52 -08:00
2020-02-25 19:37:10 -08:00
const favicon = require('./favicon');
const scss = require('./scss');
2020-02-27 18:57:39 -08:00
const svg = require('./svg');
const scripts = require('./scripts');
function writeIndex (destination, files, compressed) {
files = files.map((p) => !p.draft && (p.toJson ? p.toJson() : p));
return fs.writeFile(resolve(destination), compressed ? JSON.stringify(files) : JSON.stringify(files, null, 2));
}
2020-02-21 20:05:52 -08:00
2020-02-25 19:37:10 -08:00
exports.everything = function (prod = false) {
2020-02-29 16:27:55 -08:00
async function fn () {
2020-02-21 20:05:52 -08:00
2020-02-29 16:27:55 -08:00
// load a directory scan of the public and post folders
const [ PublicFiles, PostFiles ] = await Promise.all([
2020-02-29 16:27:55 -08:00
loadPublicFiles(),
loadPostFiles(),
]);
2020-02-27 18:57:39 -08:00
// load data for all the files in that folder
await Promise.map(PublicFiles.assets, (p) => p.load());
await Promise.map(PublicFiles.pages, (p) => p.load(PublicFiles));
2020-02-21 20:05:52 -08:00
2020-02-29 16:27:55 -08:00
await Promise.map(PostFiles.assets, (p) => p.load());
await Promise.map(PostFiles.pages, (p) => p.load(PostFiles));
2020-02-27 18:57:39 -08:00
// prime tweet data for all pages
2020-04-07 10:31:52 -07:00
let pages = await primeTweets(PublicFiles.pages.filter((p) => !p.meta.ignore));
pages = pages.filter(Boolean);
2020-02-29 16:27:55 -08:00
let posts = await primeTweets(PostFiles.pages.filter((p) => !p.meta.ignore));
2020-04-07 10:31:52 -07:00
posts = posts.filter(Boolean);
posts = sortBy(posts, 'date');
2020-03-02 20:28:46 -08:00
posts.reverse();
2020-02-21 20:05:52 -08:00
const assets = [ ...PostFiles.assets, ...PublicFiles.assets ];
const [ tasks ] = await Promise.all([
await Promise.all([
PublicFiles.tasks,
PostFiles.tasks,
scss(prod),
scripts(prod),
svg(prod),
favicon(prod),
]),
fs.ensureDir(resolve('dist')),
writeIndex('pages.json', pages),
writeIndex('posts.json', posts),
writeIndex('assets.json', assets),
]);
const cache = new Cache({ prod });
await cache.load();
await evaluate(tasks.flat(), cache);
2020-04-07 10:31:52 -07:00
const { revManifest } = await cache.save();
const engines = await getEngines(prod);
2020-04-07 10:31:52 -07:00
const postIndex = await pageWriter(prod, engines, pages, posts);
postIndex.rev = revManifest;
2020-03-07 18:04:37 -08:00
await fs.writeFile(resolve('dist/tweets/index.json'), prod ? JSON.stringify(postIndex) : JSON.stringify(postIndex, null, 2));
2020-02-29 16:27:55 -08:00
}
2020-02-21 20:05:52 -08:00
2020-02-29 16:27:55 -08:00
fn.displayName = prod ? 'buildForProd' : 'build';
return fn;
2020-02-25 19:37:10 -08:00
};
exports.pages = function () {
async function fn () {
const prod = false;
// load a directory scan of the public and post folders
const [ PublicFiles, PostFiles ] = await Promise.all([
loadPublicFiles(),
loadPostFiles(),
]);
// load data for all the files in that folder
await Promise.map(PublicFiles.assets, (p) => p.load());
await Promise.map(PublicFiles.pages, (p) => p.load(PublicFiles));
await Promise.map(PostFiles.assets, (p) => p.load());
await Promise.map(PostFiles.pages, (p) => p.load(PostFiles));
// prime tweet data for all pages
const pages = await primeTweets(PublicFiles.pages.filter((p) => !p.meta.ignore));
let posts = await primeTweets(PostFiles.pages.filter((p) => !p.meta.ignore));
posts = sortBy(posts, 'date');
posts.reverse();
const engines = await getEngines(prod);
const postIndex = await pageWriter(prod, engines, pages, posts);
await fs.writeFile(resolve('dist/tweets/index.json'), prod ? JSON.stringify(postIndex) : JSON.stringify(postIndex, null, 2));
}
fn.displayName = 'buildPages';
return fn;
};
exports.task = function (action, prod = false) {
const fn = async () => {
const tasks = await {
scss,
favicon,
svg,
scripts,
}[action](prod);
if (!tasks.length) return;
await fs.ensureDir(resolve('dist'));
const cache = new Cache({ prod });
await cache.load();
await evaluate(tasks, cache);
await evaluate(tasks.flat(), cache);
await cache.save();
};
fn.displayName = prod ? action + 'ForProd' : action;
return fn;
};