diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-02-28 23:03:28 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-02-28 23:03:28 +0000 |
commit | 7cf710c66f5645193a765ededfed77eaec9a19a9 (patch) | |
tree | 37d90acc05413f8a0cca0b8c20b4bb84b0dbb41e /editors/code | |
parent | cc477edc6692608ca809ba39db274c5e837a3498 (diff) | |
parent | 6dc598fa6331de6124a255527e37804d82372fdc (diff) |
Merge #3372
3372: vscode: migrate to more type-safe assert impl r=matklad a=Veetaha
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions
Co-authored-by: Veetaha <[email protected]>
Diffstat (limited to 'editors/code')
-rw-r--r-- | editors/code/src/installation/download_artifact.ts | 9 | ||||
-rw-r--r-- | editors/code/src/installation/download_file.ts | 3 | ||||
-rw-r--r-- | editors/code/src/installation/server.ts | 3 | ||||
-rw-r--r-- | editors/code/src/util.ts | 17 |
4 files changed, 19 insertions, 13 deletions
diff --git a/editors/code/src/installation/download_artifact.ts b/editors/code/src/installation/download_artifact.ts index 8ed99bf0a..97e4d67c2 100644 --- a/editors/code/src/installation/download_artifact.ts +++ b/editors/code/src/installation/download_artifact.ts | |||
@@ -1,10 +1,10 @@ | |||
1 | import * as vscode from "vscode"; | 1 | import * as vscode from "vscode"; |
2 | import * as path from "path"; | 2 | import * as path from "path"; |
3 | import { promises as fs } from "fs"; | 3 | import { promises as fs } from "fs"; |
4 | import { strict as assert } from "assert"; | ||
5 | 4 | ||
6 | import { ArtifactReleaseInfo } from "./interfaces"; | 5 | import { ArtifactReleaseInfo } from "./interfaces"; |
7 | import { downloadFile } from "./download_file"; | 6 | import { downloadFile } from "./download_file"; |
7 | import { assert } from "../util"; | ||
8 | 8 | ||
9 | /** | 9 | /** |
10 | * Downloads artifact from given `downloadUrl`. | 10 | * Downloads artifact from given `downloadUrl`. |
@@ -19,11 +19,10 @@ export async function downloadArtifact( | |||
19 | installationDir: string, | 19 | installationDir: string, |
20 | displayName: string, | 20 | displayName: string, |
21 | ) { | 21 | ) { |
22 | await fs.mkdir(installationDir).catch(err => assert.strictEqual( | 22 | await fs.mkdir(installationDir).catch(err => assert( |
23 | err?.code, | 23 | err?.code === "EEXIST", |
24 | "EEXIST", | ||
25 | `Couldn't create directory "${installationDir}" to download ` + | 24 | `Couldn't create directory "${installationDir}" to download ` + |
26 | `${artifactFileName} artifact: ${err.message}` | 25 | `${artifactFileName} artifact: ${err?.message}` |
27 | )); | 26 | )); |
28 | 27 | ||
29 | const installationPath = path.join(installationDir, artifactFileName); | 28 | const installationPath = path.join(installationDir, artifactFileName); |
diff --git a/editors/code/src/installation/download_file.ts b/editors/code/src/installation/download_file.ts index 319cb995c..ee8949d61 100644 --- a/editors/code/src/installation/download_file.ts +++ b/editors/code/src/installation/download_file.ts | |||
@@ -2,8 +2,7 @@ import fetch from "node-fetch"; | |||
2 | import * as fs from "fs"; | 2 | import * as fs from "fs"; |
3 | import * as stream from "stream"; | 3 | import * as stream from "stream"; |
4 | import * as util from "util"; | 4 | import * as util from "util"; |
5 | import { strict as assert } from "assert"; | 5 | import { log, assert } from "../util"; |
6 | import { log } from "../util"; | ||
7 | 6 | ||
8 | const pipeline = util.promisify(stream.pipeline); | 7 | const pipeline = util.promisify(stream.pipeline); |
9 | 8 | ||
diff --git a/editors/code/src/installation/server.ts b/editors/code/src/installation/server.ts index cb5e56844..6a6cf4f8c 100644 --- a/editors/code/src/installation/server.ts +++ b/editors/code/src/installation/server.ts | |||
@@ -1,13 +1,12 @@ | |||
1 | import * as vscode from "vscode"; | 1 | import * as vscode from "vscode"; |
2 | import * as path from "path"; | 2 | import * as path from "path"; |
3 | import { strict as assert } from "assert"; | ||
4 | import { promises as dns } from "dns"; | 3 | import { promises as dns } from "dns"; |
5 | import { spawnSync } from "child_process"; | 4 | import { spawnSync } from "child_process"; |
6 | 5 | ||
7 | import { BinarySource } from "./interfaces"; | 6 | import { BinarySource } from "./interfaces"; |
8 | import { fetchArtifactReleaseInfo } from "./fetch_artifact_release_info"; | 7 | import { fetchArtifactReleaseInfo } from "./fetch_artifact_release_info"; |
9 | import { downloadArtifact } from "./download_artifact"; | 8 | import { downloadArtifact } from "./download_artifact"; |
10 | import { log } from "../util"; | 9 | import { log, assert } from "../util"; |
11 | 10 | ||
12 | export async function ensureServerBinary(source: null | BinarySource): Promise<null | string> { | 11 | export async function ensureServerBinary(source: null | BinarySource): Promise<null | string> { |
13 | if (!source) { | 12 | if (!source) { |
diff --git a/editors/code/src/util.ts b/editors/code/src/util.ts index 68c2a94d0..f56c6bada 100644 --- a/editors/code/src/util.ts +++ b/editors/code/src/util.ts | |||
@@ -1,22 +1,31 @@ | |||
1 | import * as lc from "vscode-languageclient"; | 1 | import * as lc from "vscode-languageclient"; |
2 | import * as vscode from "vscode"; | 2 | import * as vscode from "vscode"; |
3 | import { strict as nativeAssert } from "assert"; | ||
3 | 4 | ||
4 | let enabled: boolean = false; | 5 | export function assert(condition: boolean, explanation: string): asserts condition { |
6 | try { | ||
7 | nativeAssert(condition, explanation); | ||
8 | } catch (err) { | ||
9 | log.error(`Assertion failed:`, explanation); | ||
10 | throw err; | ||
11 | } | ||
12 | } | ||
5 | 13 | ||
6 | export const log = { | 14 | export const log = { |
15 | enabled: true, | ||
7 | debug(message?: any, ...optionalParams: any[]): void { | 16 | debug(message?: any, ...optionalParams: any[]): void { |
8 | if (!enabled) return; | 17 | if (!log.enabled) return; |
9 | // eslint-disable-next-line no-console | 18 | // eslint-disable-next-line no-console |
10 | console.log(message, ...optionalParams); | 19 | console.log(message, ...optionalParams); |
11 | }, | 20 | }, |
12 | error(message?: any, ...optionalParams: any[]): void { | 21 | error(message?: any, ...optionalParams: any[]): void { |
13 | if (!enabled) return; | 22 | if (!log.enabled) return; |
14 | debugger; | 23 | debugger; |
15 | // eslint-disable-next-line no-console | 24 | // eslint-disable-next-line no-console |
16 | console.error(message, ...optionalParams); | 25 | console.error(message, ...optionalParams); |
17 | }, | 26 | }, |
18 | setEnabled(yes: boolean): void { | 27 | setEnabled(yes: boolean): void { |
19 | enabled = yes; | 28 | log.enabled = yes; |
20 | } | 29 | } |
21 | }; | 30 | }; |
22 | 31 | ||