Skip to content

Commit

Permalink
Make emulators:exec exit with non-zero code when script fails (#1387)
Browse files Browse the repository at this point in the history
  • Loading branch information
samtstern committed Jun 11, 2019
1 parent 9cffe5a commit 6492495
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
* Fixes bug in Firestore emulator where property paths with special characters would cause errors due to ClassNotFound exceptions.
* Fixes bug in Firestore emulator where auto-id allocation only worked once per collection.
* Adds REST API to Firestore emulator for setting security rules.
* Fixes bug where `firebase emulators:exec` would succeed even if the script failed.
20 changes: 16 additions & 4 deletions src/commands/emulators-exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import getProjectNumber = require("../getProjectNumber");
import requireAuth = require("../requireAuth");
import requireConfig = require("../requireConfig");
import { Emulators } from "../emulator/types";
import * as FirebaseError from "../error";
import * as utils from "../utils";
import * as logger from "../logger";
import * as controller from "../emulator/controller";
import { EmulatorRegistry } from "../emulator/registry";
import { FirestoreEmulator } from "../emulator/firestoreEmulator";

async function runScript(script: string): Promise<void> {
async function runScript(script: string): Promise<number> {
utils.logBullet(`Running script: ${clc.bold(script)}`);

const env: NodeJS.ProcessEnv = { ...process.env };
Expand Down Expand Up @@ -52,15 +53,19 @@ async function runScript(script: string): Promise<void> {
if (signal) {
utils.logWarning(`Script exited with signal: ${signal}`);
setTimeout(reject, exitDelayMs);
return;
}

const exitCode = code || 0;
if (code === 0) {
utils.logSuccess(`Script exited successfully (code 0)`);
setTimeout(resolve, exitDelayMs);
} else {
utils.logWarning(`Script exited unsuccessfully (code ${code})`);
setTimeout(resolve, exitDelayMs);
}

setTimeout(() => {
resolve(exitCode);
}, exitDelayMs);
});
});
}
Expand All @@ -82,13 +87,20 @@ module.exports = new Command("emulators:exec <script>")
JSON.stringify(controller.VALID_EMULATOR_STRINGS)
)
.action(async (script: string, options: any) => {
let exitCode = 0;
try {
await controller.startAll(options);
await runScript(script);
exitCode = await runScript(script);
} catch (e) {
logger.debug("Error in emulators:exec", e);
throw e;
} finally {
await controller.cleanShutdown();
}

if (exitCode !== 0) {
throw new FirebaseError(`Script "${clc.bold(script)}" exited with code ${exitCode}`, {
exit: exitCode,
});
}
});

0 comments on commit 6492495

Please sign in to comment.