Skip to content

Commit 64198bb

Browse files
committed
chore: cleanup binary which logic
1 parent 1cd1225 commit 64198bb

File tree

2 files changed

+35
-28
lines changed

2 files changed

+35
-28
lines changed

src/extension.ts

+29-28
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import * as vscode from "vscode"
44
import { CoderHelpProvider } from "./help"
5-
import * as which from "which"
65

76
import {
87
CoderWorkspacesProvider,
@@ -17,7 +16,7 @@ import {
1716
handleInspectCommand,
1817
handleShowLogsCommand,
1918
} from "./logs"
20-
import { execCombined } from "./utils"
19+
import { execCombined, binaryExists } from "./utils"
2120

2221
export function activate(context: vscode.ExtensionContext) {
2322
preflightCheckCoderInstalled()
@@ -52,31 +51,33 @@ export function activate(context: vscode.ExtensionContext) {
5251

5352
export const outputChannel = vscode.window.createOutputChannel("Coder")
5453

55-
const preflightCheckCoderInstalled = () => {
56-
which("coder", (err: any) => {
57-
if (err) {
58-
which("brew", async (err: any) => {
59-
if (err) {
60-
vscode.window.showErrorMessage(
61-
`"coder" CLI not found in $PATH. Please follow the install and authentication [instructions here](https://coder.com/docs/cli/installation)`,
62-
"Dismiss",
63-
)
64-
} else {
65-
const action = await vscode.window.showErrorMessage(`"coder" CLI not found in $PATH`, "Install with `brew`")
66-
if (action) {
67-
outputChannel.show()
68-
const cmd = "brew install cdr/coder/coder-cli"
69-
outputChannel.appendLine(cmd)
70-
const output = await execCombined(cmd)
71-
outputChannel.appendLine(output.stderr)
72-
which("coder", err => err ? (
73-
outputChannel.appendLine(`Install failed. "coder" still not found in $PATH.`)
74-
) : (
75-
outputChannel.appendLine("Installation successful.\nACTION REQUIRED: run \"coder login [https://coder.domain.com]\"")
76-
))
77-
}
78-
}
79-
})
54+
const preflightCheckCoderInstalled = async () => {
55+
const coderExists = await binaryExists("coder")
56+
if (coderExists) {
57+
return
58+
}
59+
const brewExists = await binaryExists("brew")
60+
if (!brewExists) {
61+
vscode.window.showErrorMessage(
62+
`"coder" CLI not found in $PATH. Please follow the install and authentication [instructions here](https://coder.com/docs/cli/installation).`,
63+
"Dismiss",
64+
)
65+
} else {
66+
const action = await vscode.window.showErrorMessage(`"coder" CLI not found in $PATH`, "Install with `brew`")
67+
if (action) {
68+
outputChannel.show()
69+
const cmd = "brew install cdr/coder/coder-cli"
70+
outputChannel.appendLine(cmd)
71+
const output = await execCombined(cmd)
72+
outputChannel.appendLine(output.stderr)
73+
const coderExists = await binaryExists("coder")
74+
if (coderExists) {
75+
outputChannel.appendLine(
76+
'Installation successful.\nACTION REQUIRED: run "coder login [https://coder.domain.com]"',
77+
)
78+
} else {
79+
outputChannel.appendLine(`Install failed. "coder" still not found in $PATH.`)
80+
}
8081
}
81-
})
82+
}
8283
}

src/utils.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as path from "path"
22
import * as cp from "child_process"
33
import * as vscode from "vscode"
4+
import * as nodeWhich from 'which'
45

56
export const mediaDir = path.join(__filename, "..", "..", "media")
67

@@ -21,6 +22,11 @@ export const execJSON = async <T>(command: string): Promise<T> => {
2122
return JSON.parse(output)
2223
}
2324

25+
// binaryExists returns "true" if the binary is found in $PATH
26+
export const binaryExists = async (bin: string): Promise<boolean> => {
27+
return new Promise((res) => { nodeWhich(bin, err => res(!err)) })
28+
}
29+
2430
export const bubbleError = (f: () => void) => {
2531
try {
2632
f()

0 commit comments

Comments
 (0)