Skip to content

Commit

Permalink
feat(display): improve the informational output when running the CLI
Browse files Browse the repository at this point in the history
`codemod` now prints all files it runs with, but dims those where there are no changes. This is similar to what prettier does.
  • Loading branch information
eventualbuddha committed Jan 24, 2018
1 parent 76ef2ff commit c6eea8f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ EXAMPLES

export default async function run(
argv: Array<string>,
stdin: NodeJS.ReadableStream,
stdout: NodeJS.WritableStream,
stderr: NodeJS.WritableStream,
stdin: NodeJS.ReadStream,
stdout: NodeJS.WriteStream,
stderr: NodeJS.WriteStream,
fs: typeof realFs = realFs
): Promise<number> {
let options = Options.parse(argv.slice(2));
Expand Down Expand Up @@ -119,12 +119,17 @@ export default async function run(

runner = new TransformRunner(sourcesIterator, plugins);

let dim = stdout.isTTY ? '\x1b[2m' : '';
let reset = stdout.isTTY ? '\x1b[0m' : '';

for (let result of runner.run()) {
if (result.output) {
if (options.stdio) {
stdout.write(`${result.output}\n`);
} else {
if (result.output !== result.source.content) {
if (result.output === result.source.content) {
stdout.write(`${dim}${result.source.path}${reset}\n`);
} else {
stats.modified++;
stdout.write(`${result.source.path}\n`);
if (!dryRun) {
Expand Down
15 changes: 15 additions & 0 deletions test/cli/CLITest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,21 @@ describe('CLI', function() {
strictEqual(await readFile(afile, 'utf8'), '3 + 4;');
});

it('prints files not processed in dim colors', async function() {
let afile = await createTemporaryFile('a-file.js', '3 + 4;');
let { status, stdout, stderr } = await runCodemodCLI([afile]);

deepEqual(
{ status, stdout, stderr },
{
status: 0,
stdout: `${afile}\n1 file(s), 0 modified, 0 errors\n`,
stderr: ''
}
);
strictEqual(await readFile(afile, 'utf8'), '3 + 4;');
});

it('can load plugins written with ES modules by default', async function() {
let afile = await createTemporaryFile('a-file.js', '3 + 4;');
let { status, stdout, stderr } = await runCodemodCLI([
Expand Down

0 comments on commit c6eea8f

Please sign in to comment.