Skip to content

Commit

Permalink
fixup! Add loggerFinishAndExit for log file consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
yuqu committed May 18, 2020
1 parent 014c77b commit 985c7a4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
19 changes: 12 additions & 7 deletions src/util/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import chalk from "chalk";
import dedent from "ts-dedent";
import os from "os";
import path from "path";
import { isVerbose } from "../util/env";
import { isVerbose } from "./env";
import { CLIError } from "../errors";
import logger, { loggerFinishAndExit } from "../util/logger";
import logger from "./logger";
import { gracefulExit } from "./process";

const errorHandler = (error: Error): Promise<never> => {
if (isVerbose()) {
Expand All @@ -28,16 +29,20 @@ const errorHandler = (error: Error): Promise<never> => {
const logFile = path.join(os.homedir(), ".zeplin", "cli.log");
logger.info(`\nPlease check ${chalk.dim(logFile)} for details.\n`);

return loggerFinishAndExit(1);
return gracefulExit(1);
};

type FunctionReturnsPromise = (...args: Array<any>) => Promise<void>;

function commandRunner(fn: FunctionReturnsPromise): FunctionReturnsPromise {
return (...args: Array<any>): Promise<void> =>
fn(...args)
.then(() => loggerFinishAndExit())
.catch(errorHandler);
return async (...args: Array<any>): Promise<void> => {
try {
await fn(...args);
await gracefulExit();
} catch (e) {
await errorHandler(e);
}
};
}

export { commandRunner };
6 changes: 3 additions & 3 deletions src/util/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ fileTransport.on("open", () => {
});
});

const loggerFinishAndExit = (exitCode = 0): Promise<never> => new Promise((): void => {
const waitForLoggerFinish = (): Promise<never> => new Promise((resolve): void => {
fileLogWatcher.on("finish", (): void => {
process.exit(exitCode);
resolve();
});
logger.end();
});

export default logger;
export {
loggerFinishAndExit
waitForLoggerFinish
};
10 changes: 10 additions & 0 deletions src/util/process.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { waitForLoggerFinish } from "./logger";

const gracefulExit = async (exitCode = 0): Promise<never> => {
await waitForLoggerFinish();
process.exit(exitCode);
};

export {
gracefulExit
};

0 comments on commit 985c7a4

Please sign in to comment.