Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Async-ified writing the tslint.json file in --init #3332

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -135,7 +135,7 @@ async function runWorker(options: Options, logger: Logger): Promise<Status> {
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;
}

Expand Down
14 changes: 14 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* limitations under the License.
*/

import * as fs from "fs";

/**
* Enforces the invariant that the input is an array.
*/
Expand Down Expand Up @@ -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) {
await new Promise((resolve, reject) => {
fs.writeFile(fileName, data, (error?: Error) => {
if (error != null) {
reject(error);
} else {
resolve();
}
});
});
}