diff --git a/src/runner.ts b/src/runner.ts index c611a645385..53af8107ca2 100644 --- a/src/runner.ts +++ b/src/runner.ts @@ -32,7 +32,7 @@ import { import { FatalError } from "./error"; import { LintResult } from "./index"; import * as Linter from "./linter"; -import { arrayify, flatMap } from "./utils"; +import { arrayify, flatMap, writeFileAsync } from "./utils"; export interface Options { /** @@ -135,7 +135,7 @@ async function runWorker(options: Options, logger: Logger): Promise { throw new FatalError(`Cannot generate ${CONFIG_FILENAME}: file already exists`); } - fs.writeFileSync(CONFIG_FILENAME, JSON.stringify(DEFAULT_CONFIG, undefined, " ")); + await writeFileAsync(CONFIG_FILENAME, JSON.stringify(DEFAULT_CONFIG, undefined, " ")); return Status.Ok; } diff --git a/src/utils.ts b/src/utils.ts index 65ef068a3df..b06b296cc70 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -15,6 +15,8 @@ * limitations under the License. */ +import * as fs from "fs"; + /** * Enforces the invariant that the input is an array. */ @@ -213,3 +215,15 @@ export function detectBufferEncoding(buffer: Buffer, length = buffer.length): En export function denormalizeWinPath(path: string): string { return path.replace(/\\/g, "/"); } + +export async function writeFileAsync(fileName: string, data: string): Promise { + await new Promise((resolve, reject) => { + fs.writeFile(fileName, data, (error?: Error) => { + if (error !== null) { + reject(error); + } else { + resolve(); + } + }); + }); +}