Skip to content

Commit

Permalink
Add simple reporting API
Browse files Browse the repository at this point in the history
This diff adds a new method to the API passed to transformers, `report`.
This function accepts a string and will print that string (with the file
path) to stdout.

This makes it possible for transformers to report arbitrary information
to other tools that process jscodeshift's output.

In the future the API can be extended to be able to consume the reports
in a programmatic way.
  • Loading branch information
fkling committed Dec 3, 2018
1 parent 4e8da4b commit c902a00
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/Runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ const log = {
},
};

function report({file, msg}) {
bufferedWrite(lineBreak(`${colors.white.bgBlue(' REP ')}${file} ${msg}`));
}

function concatAll(arrays) {
const result = [];
for (const array of arrays) {
Expand Down Expand Up @@ -276,6 +280,9 @@ function run(transformFile, paths, options) {
case 'free':
child.send({files: next(), options});
break;
case 'report':
report(message);
break;
}
});
return new Promise(resolve => child.on('disconnect', resolve));
Expand Down
10 changes: 8 additions & 2 deletions src/Worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function setup(tr, babel) {
]
});
}

const module = require(tr);
transform = typeof module.default === 'function' ?
module.default :
Expand All @@ -80,10 +81,14 @@ function free() {
}

function updateStatus(status, file, msg) {
msg = msg ? file + ' ' + msg : file;
msg = msg ? file + ' ' + msg : file;
notify({action: 'status', status: status, msg: msg});
}

function report(file, msg) {
notify({action: 'report', file, msg});
}

function empty() {}

function stats(name, quantity) {
Expand Down Expand Up @@ -134,7 +139,8 @@ function run(data) {
{
j: jscodeshift,
jscodeshift: jscodeshift,
stats: options.dry ? stats : empty
stats: options.dry ? stats : empty,
report: msg => report(file, msg),
},
options
);
Expand Down

0 comments on commit c902a00

Please sign in to comment.