Skip to content

Commit

Permalink
Fix reading from process.stdin in translate.js
Browse files Browse the repository at this point in the history
Reviewed By: jrwats

Differential Revision: D17605702

fbshipit-source-id: 386cc07145dafa8a532f11c26ca8e26fcf4a4524
  • Loading branch information
Alex Bettadapur authored and facebook-github-bot committed Oct 3, 2019
1 parent 0199b8d commit b0247c6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ We haven't had the best track record of code/feature changes before this date, b
</summary>

- [MAJOR] Remove `bin/tiger*` in favor of standalone `fb-tiger-hash` NPM package (native JS implementation)
- [BUG] Fix bug in translate command where reading from stdin could sometimes fail (#79)

</details>

Expand Down
51 changes: 33 additions & 18 deletions transform/babel-plugin-fbt/bin/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,29 +211,44 @@ function processGroups(phrases, translatedGroups) {
return localeToHashToFbt;
}

if (argv[args.HELP]) {
yargs.showHelp();
process.exit(0);
function createJSON(obj) {
return JSON.stringify(obj, ...(argv[args.PRETTY] ? [null, 2] : []));
}

const output = argv[args.STDIN]
? processJSON(JSON.parse(fs.readFileSync(process.stdin.fd, 'utf8')))
: processFiles(argv[args.SRC], argv[args.TRANSLATIONS]);
function writeOutput(output) {
const outputDir = yargs.argv[args.OUTPUT_DIR];
if (outputDir) {
fs.mkdirSync(outputDir, {recursive: true});

function createJSON(obj) {
return JSON.stringify(obj, ...(argv[args.PRETTY] ? [null, 2] : []));
Object.keys(output).forEach(locale => {
fs.writeFileSync(
path.join(outputDir, `${locale}.json`),
createJSON({[locale]: output[locale]}),
);
});
} else {
process.stdout.write(createJSON(output));
}
}

const outputDir = yargs.argv[args.OUTPUT_DIR];
if (outputDir) {
fs.mkdirSync(outputDir, {recursive: true});
if (argv[args.HELP]) {
yargs.showHelp();
process.exit(0);
}

Object.keys(output).forEach(locale => {
fs.writeFileSync(
path.join(outputDir, `${locale}.json`),
createJSON({[locale]: output[locale]}),
);
});
if (argv[args.STDIN]) {
const stream = process.stdin;
let source = '';
stream
.setEncoding('utf8')
.on('data', function(chunk) {
source += chunk;
})
.on('end', function() {
const output = processJSON(JSON.parse(source));
writeOutput(output);
});
} else {
process.stdout.write(createJSON(output));
const output = processFiles(argv[args.SRC], argv[args.TRANSLATIONS]);
writeOutput(output);
}

0 comments on commit b0247c6

Please sign in to comment.