2020-02-27 18:57:39 -08:00
|
|
|
const { sortBy, uniqBy } = require('lodash');
|
2020-02-25 19:37:10 -08:00
|
|
|
const { resolve } = require('./resolve');
|
|
|
|
const log = require('fancy-log');
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
const fs = require('fs-extra');
|
2020-02-28 10:22:53 -08:00
|
|
|
const path = require('path');
|
2020-02-25 19:37:10 -08:00
|
|
|
|
|
|
|
const LOG = {
|
|
|
|
new: true,
|
|
|
|
update: true,
|
|
|
|
skip: true,
|
|
|
|
rebuild: true,
|
|
|
|
cached: false,
|
|
|
|
copy: false,
|
2020-02-27 20:41:18 -08:00
|
|
|
silent: false,
|
2020-02-25 19:37:10 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = exports = async function process (tasks, cache) {
|
|
|
|
const lastSeen = new Date();
|
|
|
|
|
2020-02-27 18:57:39 -08:00
|
|
|
tasks = uniqBy(tasks, 'output');
|
|
|
|
tasks = sortBy(tasks, [ 'input', 'output' ]);
|
|
|
|
|
|
|
|
await Promise.map(tasks, async (task) => {
|
2020-02-25 19:37:10 -08:00
|
|
|
let result;
|
|
|
|
let status = await cache.get(task);
|
|
|
|
const { input, output } = task;
|
|
|
|
const taskLog = [ status.mode, status.input, status.output ];
|
|
|
|
if (status.mode === 'skip') {
|
|
|
|
await cache.touch(task, lastSeen);
|
|
|
|
if (taskLog && LOG[taskLog[0]]) log.info(...taskLog);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status.mode === 'cached') {
|
|
|
|
result = status.cache;
|
2020-02-28 10:22:53 -08:00
|
|
|
await fs.ensureDir(path.dirname(resolve('dist', output)));
|
2020-02-25 19:37:10 -08:00
|
|
|
await fs.writeFile(resolve('dist', output), result);
|
|
|
|
await cache.touch(task, lastSeen);
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
result = await task.action({
|
|
|
|
...task,
|
|
|
|
input,
|
|
|
|
output: 'dist/' + output,
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
log.error(`Task (${task.action.name}) failed for file ${output}.\n`, err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = await cache.set(task, result, lastSeen);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (taskLog && LOG[taskLog[0]]) log.info(...taskLog);
|
|
|
|
|
|
|
|
if (cache.isProd) {
|
|
|
|
fs.writeFile(resolve('dist', status.revPath), result);
|
|
|
|
}
|
|
|
|
|
|
|
|
}, { concurrency: 1 });
|
|
|
|
|
|
|
|
};
|