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');
|
2021-08-25 11:23:31 -07:00
|
|
|
const actions = require('./actions');
|
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;
|
2020-04-07 09:49:21 -07:00
|
|
|
const taskLog = [ status.mode, (status.why ? status.why : ''), status.input, status.output ];
|
2020-02-25 19:37:10 -08:00
|
|
|
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) {
|
2021-08-25 11:23:31 -07:00
|
|
|
if (status.duplicate && await fs.pathExists(status.duplicate)) {
|
|
|
|
try {
|
|
|
|
result = await actions.copy({
|
|
|
|
input: status.duplicate,
|
|
|
|
output: 'dist/' + output,
|
|
|
|
});
|
|
|
|
log.info(`Task (${task.action.name}) failed for file ${output}, fell back to saved duplicate ${status.duplicate}`);
|
|
|
|
} catch (err2) {
|
2021-08-25 11:31:34 -07:00
|
|
|
log.error(`Task (${task.action.name}) failed for file ${output}, ${status.duplicate} could not be copied.\n`, err);
|
2021-08-25 11:23:31 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.error(`Task (${task.action.name}) failed for file ${output}.\n`, err);
|
|
|
|
return false;
|
|
|
|
}
|
2020-02-25 19:37:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
status = await cache.set(task, result, lastSeen);
|
|
|
|
}
|
|
|
|
if (taskLog && LOG[taskLog[0]]) log.info(...taskLog);
|
|
|
|
|
2021-08-11 10:19:07 -07:00
|
|
|
if (cache.isProd && status.revPath) {
|
2020-03-11 12:12:24 -07:00
|
|
|
await fs.writeFile(resolve('dist', status.revPath), result);
|
2020-02-25 19:37:10 -08:00
|
|
|
}
|
|
|
|
|
2020-04-07 09:49:33 -07:00
|
|
|
}, { concurrency: 20 });
|
2020-02-25 19:37:10 -08:00
|
|
|
|
|
|
|
};
|