Change processSerializedCode func to receive error as 1st arg

Summary:
Since example code of [How to run Prepack](https://github.com/facebook/prepack#how-to-run-prepack) doesn't work(https://github.com/facebook/prepack/issues/781 )
I fixed `processSerializedCode` function to handle error as 1st argument
which follows 'error first callback' convention.
And to verify the changes I've added test code for the case of `StdIn`
Closes https://github.com/facebook/prepack/pull/784

Differential Revision: D5375275

Pulled By: NTillmann

fbshipit-source-id: ea85d0f30db4305aa7fcb5b69bf28f5998a84962
This commit is contained in:
Sangboak Lee 2017-07-06 12:35:04 -07:00 committed by Facebook Github Bot
parent 77363ffb86
commit 1c23533c39
6 changed files with 59 additions and 11 deletions

View File

@ -23,6 +23,7 @@ test:
- yarn test-serializer-with-coverage
- yarn test-sourcemaps
- yarn test-node-cli-mode
- yarn test-std-in
- yarn test-residual
- yarn test-test262 -- --statusFile $CIRCLE_ARTIFACTS/test262-status.txt --timeout 120 --cpuScale 0.25 --verbose:
timeout: 1800

View File

@ -40,7 +40,8 @@
"test-error-handler": "babel-node scripts/test-error-handler.js",
"test-error-handler-with-coverage": "./node_modules/.bin/istanbul cover ./lib/test-error-handler.js --dir coverage.error && ./node_modules/.bin/remap-istanbul -i coverage.error/coverage.json -o coverage-sourcemapped.error -t html",
"test-node-cli-mode": "bash < scripts/test-node-cli-mode.sh",
"test": "npm run test-residual && npm run test-serializer && npm run test-sourcemaps && npm run test-test262 && npm run test-internal && npm run test-error-handler",
"test-std-in": "bash < scripts/test-std-in.sh",
"test": "npm run test-residual && npm run test-serializer && npm run test-sourcemaps && npm run test-test262 && npm run test-internal && npm run test-error-handler && npm run test-std-in",
"repl": "node lib/repl-cli.js",
"prepack": "node lib/prepack-cli.js",
"prepack-prepack": "node --stack_size=10000 --max_old_space_size=8096 ./bin/prepack.js ./lib/prepack-cli.js --out ./lib/prepack-cli.prepacked.js --compatibility node-cli --mathRandomSeed rnd",

20
scripts/test-std-in.sh Normal file
View File

@ -0,0 +1,20 @@
# Prepack test input
cat ./test/std-in/StdIn.js | node ./bin/prepack.js --out StdIn-test.js
# Run the resulting program and check it for expected output
node ./StdIn-test.js | grep "Hello world from std-in!" > /dev/null
if [[ $? -ne 0 ]]; then
exit 1
fi
rm ./StdIn-test.js
# Prepack test empty input and check if it logs correct msg
echo "" | node ./bin/prepack.js --out StdIn-test.js | grep "No source code to prepack" > /dev/null
if [[ $? -ne 0 ]]; then
exit 1
fi
# Prepack test input and check if it exits with signal 1
cat ./test/std-in/StdInError.js | node ./bin/prepack.js --out StdInError-test.js > /dev/null
if [[ $? -ne 1 ]]; then
exit 1
fi

View File

@ -142,7 +142,7 @@ function run(
return;
}
let serialized = prepackFileSync(inputFilename, resolvedOptions);
processSerializedCode(serialized);
processSerializedCode(null, serialized);
} catch (x) {
console.log(x.message);
console.log(x.stack);
@ -159,7 +159,11 @@ function run(
}
}
function processSerializedCode(serialized) {
function processSerializedCode(err, serialized) {
if (err) {
console.error(err);
process.exit(1);
}
if (errors.size > 0) {
console.log("Errors found while prepacking");
for (let [loc, error] of errors) {
@ -170,14 +174,20 @@ function run(
}
process.exit(1);
}
if (outputFilename) {
console.log(`Prepacked source code written to ${outputFilename}.`);
fs.writeFileSync(outputFilename, serialized.code);
} else {
console.log(serialized.code);
}
if (outputSourceMap) {
fs.writeFileSync(outputSourceMap, serialized.map ? JSON.stringify(serialized.map) : "");
if (serialized) {
if (serialized.code === '') {
console.log('No source code to prepack.');
return;
}
if (outputFilename) {
console.log(`Prepacked source code written to ${outputFilename}.`);
fs.writeFileSync(outputFilename, serialized.code);
} else {
console.log(serialized.code);
}
if (outputSourceMap) {
fs.writeFileSync(outputSourceMap, serialized.map ? JSON.stringify(serialized.map) : "");
}
}
}

8
test/std-in/StdIn.js Normal file
View File

@ -0,0 +1,8 @@
function hello() {
return 'Hello';
}
function world() {
return 'world';
}
let greeting = hello() + ' ' + world();
console.log(greeting + ' from std-in!');

View File

@ -0,0 +1,8 @@
function hello() {
return 'Hello';
}
function world() {
return 'world';
// SyntaxError: Unexpected token
let greeting = hello() + ' ' + world();
console.log(greeting + ' from std-in!');