Skip to content

Commit

Permalink
Add simple log collector to plugin context
Browse files Browse the repository at this point in the history
  • Loading branch information
allmarkedup committed Mar 9, 2019
1 parent d1be855 commit 9349471
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
5 changes: 4 additions & 1 deletion packages/app/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ module.exports = function(compiler, opts = {}) {
koa.context.mode = mode;
koa.context.utils = utils;

const state = await compiler.run();
const state = compiler.getState();

const { info } = await compiler.run();
app.emit('state.updated', state, info, true);

middleware.forEach(mw => koa.use(mw));

Expand Down
26 changes: 19 additions & 7 deletions packages/core/src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,38 @@ module.exports = function(config = {}) {
};

compiler.run = async function() {
const hrStart = process.hrtime();
emitter.emit('start');
const ctx = {};
const logs = [];
const hrStart = process.hrtime();
const { paths, opts } = parseSrc;

let files = await readFiles(paths, {
onlyFiles: false,
gitignore: !!opts.gitignore
});

ctx.files = files;
ctx.log = (message, level) => {
logs.push({ level, message });
};

const components = await compileComponents(files, opts);
files = files.filter(f => f.stats.isFile());
const applyPlugins = compose(
middlewares,
{ files }
ctx
);
await applyPlugins(components);
state.update({ components, files });
emitter.emit('finish', state, process.hrtime(hrStart));
return state;

const info = {
logs,
time: process.hrtime(hrStart)
};

emitter.emit('finish', state, info);
return { state, info };
};

compiler.watch = function(callback) {
Expand All @@ -84,12 +97,11 @@ module.exports = function(config = {}) {
function watchHandler(parseEvents) {
let lastResult = null;
return debounce(async (event, path) => {
const hrStart = process.hrtime();
try {
// Only re-parse for 'primary' events, otherwise just notify of changes
lastResult = parseEvents.includes(event) ? await compiler.run() : lastResult;
const time = process.hrtime(hrStart);
watchCallbacks.forEach(cb => cb(null, lastResult, { path, event, time }));
const info = { path, event, ...lastResult.info };
watchCallbacks.forEach(cb => cb(null, lastResult.state, info));
} catch (err) {
watchCallbacks.forEach(cb => cb(err));
}
Expand Down
14 changes: 8 additions & 6 deletions packages/fractalite/bin/fractalite
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,24 @@ const args = mri(process.argv.slice(2));
const mode = assign({ mode: 'develop' }, config.develop || {}, args);
const app = fractalite({ mode, ...config });

const interactiveLogger = new Signale({interactive: true});

app.on('error', function onError(err) {
if (err.message.indexOf('ECONNRESET') >= 0) {
return;
}
if (err.status === 404) {
interactiveLogger.warn(`${err.message} ${dim(`[${err.path}]`)}`);
logger.warn(`${err.message} ${dim(`[${err.path}]`)}`);
} else {
interactiveLogger.error(err);
logger.error(err);
}
});

app.on('state.pending', () => interactiveLogger.await(`Updating state`));
app.on('state.pending', () => logger.info(`Source change detected`));
app.on('state.updated', (state, info) => {
interactiveLogger.complete(`Updated in %d.%ds`, info.time[0], Math.round(info.time[1] / 1000000))
for (const log of info.logs || []) {
log.message instanceof Error ? logger.error(log.message) : logger[log.level || 'info'](log.message);
}
logger.success(`Compiled in %d.%ds`, info.time[0], Math.round(info.time[1] / 1000000))
logger.log('')
});

const server = await app.run();
Expand Down

0 comments on commit 9349471

Please sign in to comment.