Skip to content

Commit

Permalink
fix(roc-plugin-repo): Improved logging from build, run and exec command
Browse files Browse the repository at this point in the history
  • Loading branch information
dlmr committed Jan 16, 2018
1 parent 1f2a5b8 commit e3c5379
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 145 deletions.
67 changes: 39 additions & 28 deletions extensions/roc-plugin-repo/src/actions/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,41 @@ function buildWithBabel(config, watch, createLogger) {
return (projects, mode, multipleTargets) =>
Promise.all(
projects.map(project => {
const logger = createLogger(project.name).logger;
const logger = createLogger(`${project.name} (${mode})`).logger;
const babelConfig = invokeHook('babel-config', mode, project);
try {
logger(`Building for ${mode} with Babel`);
return babel(
{
mode,
path: project.path,
src: `${project.path}/${settings.input}`,
out: multipleTargets
? `${project.path}/${settings.output}/${mode}`
: `${project.path}/${settings.output}`,
// We want to ignore potential __snapshots__ and __mocks__ directories
ignore: settings.test.concat([
'**/__snapshots__/**',
'**/__mocks__/**',
]),
copyFiles: true,
sourceMaps: true,
babelrc: false,
watch,
log: logger,
},
babelConfig,
);
} catch (err) {
err.projectName = project.name;
logger(
`Building for ${mode === 'cjs'
? 'CommonJS'
: 'ES Modules'} with Babel`,
);
return babel(
{
mode,
path: project.path,
src: `${project.path}/${settings.input}`,
out: multipleTargets
? `${project.path}/${settings.output}/${mode}`
: `${project.path}/${settings.output}`,
// We want to ignore potential __snapshots__ and __mocks__ directories
ignore: settings.test.concat([
'**/__snapshots__/**',
'**/__mocks__/**',
]),
copyFiles: true,
sourceMaps: true,
babelrc: false,
watch,
log: logger,
},
babelConfig,
).catch(err => {
err.projectName = project.name; // eslint-disable-line no-param-reassign
if (err._babel && err instanceof SyntaxError) {
// Display codeFrame if it is an Babel Error
err.message = `${err.message}\n${err.codeFrame}`;
err.message = `${err.message}\n${err.codeFrame}`; // eslint-disable-line no-param-reassign
}
throw err;
}
});
}),
);
}
Expand Down Expand Up @@ -69,5 +71,14 @@ export default (context, projects, { options: { watch }, createLogger }) => {
cjsJavaScriptBuild.length > 0,
);
}

if (
watch &&
(esmJavaScriptBuild.length > 0 || cjsJavaScriptBuild.length > 0)
) {
return { watch: true };
}

return undefined;
};
};
24 changes: 9 additions & 15 deletions extensions/roc-plugin-repo/src/commands/exec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import execa from 'execa';
import log from 'roc/log/default/small';
import Listr from 'listr';
import isCI from 'is-ci';

import run from './utils/run';

export default projects => ({
context,
arguments: { managed: { projects: selectedProjects } },
options: { managed: { silent, concurrent } },
options: { managed: { concurrent } },
extraArguments,
}) => {
const selected = projects.filter(
Expand All @@ -22,15 +22,9 @@ export default projects => ({
return log.warn('No command was given');
}

return new Listr(
selected.map(project => ({
title: `Running "${command}" in ${project.name}`,
task: () =>
execa.shell(command, {
stdout: silent ? undefined : 'inherit',
cwd: project.path,
}),
})),
{ concurrent, renderer: isCI ? 'verbose' : 'default' },
).run();
return run(selected, {
command,
concurrent,
isMonorepo: !!context.config.settings.repo.mono,
})();
};
21 changes: 8 additions & 13 deletions extensions/roc-plugin-repo/src/commands/run.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import execa from 'execa';
import log from 'roc/log/default/small';
import Listr from 'listr';
import isCI from 'is-ci';

import run from './utils/run';

function makeList(elements) {
return `${elements.map(element => ` - ${element}`).join('\n')}\n`;
Expand Down Expand Up @@ -68,14 +67,10 @@ export default projects => ({
return log.warn('Nothing matched the selected script');
}

return new Listr(
toRun.map(project => ({
title: `Running "${command}" using ${binary} in ${project.name}`,
task: () =>
execa(binary, ['run', command].concat(extraArguments), {
cwd: project.path,
}),
})),
{ concurrent, renderer: isCI ? 'verbose' : 'default' },
).run();
return run(toRun, {
binary,
command: ['run', command].concat(extraArguments),
concurrent,
isMonorepo: !!context.config.settings.repo.mono,
})();
};
84 changes: 84 additions & 0 deletions extensions/roc-plugin-repo/src/commands/utils/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import chalk from 'chalk';
import execa from 'execa';
import log from 'roc/log/default';
import logTransformer from 'strong-log-transformer';
import pAll from 'p-all';

const colorWheel = ['cyan', 'magenta', 'blue', 'yellow', 'green', 'red'];
let index = 0;
const getTag = projectName => {
const color = chalk[colorWheel[index++ % colorWheel.length]]; // eslint-disable-line no-plusplus
const tag = `${color(projectName)}:`;
return tag;
};

export const createLogger = projectName => {
const tag = getTag(projectName);
return {
logger: (msg, error) => log.small.log(`${tag} ${msg}`, error),
tag,
};
};

export default function run(
projects,
{ binary, command, concurrent = 1, isMonorepo = false },
) {
const commandString = binary ? `${binary} ${command.join(' ')}` : command;

return () =>
pAll(
projects.map(project => () => {
const tag = getTag(project.name);
const prefixedStdout = logTransformer({
tag,
});
const prefixedStderr = logTransformer({
tag,
mergeMultiline: true,
});
log.small.log(`${isMonorepo ? `${tag} ` : ''}${commandString}`);

const execaOptions = {
cwd: project.path,
env: {
FORCE_COLOR: true,
},
};

const child = binary
? execa(binary, command, execaOptions)
: execa.shell(command, execaOptions);

if (isMonorepo) {
if (projects.length > process.stdout.listenerCount('close')) {
process.stdout.setMaxListeners(projects.length);
process.stderr.setMaxListeners(projects.length);
}

child.stdout.pipe(prefixedStdout).pipe(process.stdout);
child.stderr.pipe(prefixedStderr).pipe(process.stderr);
} else {
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
}

return child.catch(error => {
error.message = error.stderr; // eslint-disable-line no-param-reassign
error.projectName = project.name; // eslint-disable-line no-param-reassign
throw error;
});
}),
{ concurrency: concurrent },
).catch(error => {
if (error.projectName) {
log.large.error(
`Problem when running "${commandString}"`,
error.projectName || error,
error,
);
} else {
log.large.error(`Problem when running "${commandString}"`, error);
}
});
}
Loading

0 comments on commit e3c5379

Please sign in to comment.