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 #3337

Closed
wants to merge 2 commits 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): Promise<void> {
await new Promise<void>((resolve, reject) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can simply return the Promise without awaiting

fs.writeFile(fileName, data, (error?: Error) => {
if (error !== null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the mixed types here don't make sense. The optional paramter indicates that error may be undefined, but you compare it to null.
I guess consistently using null here is the right thing to do.

reject(error);
} else {
resolve();
}
});
});
}