Skip to content

Commit 5c4089c

Browse files
committed
refactor(cli): export and purify readSocketPath
1 parent 83e16b9 commit 5c4089c

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

src/node/cli.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { promises as fs } from "fs"
33
import yaml from "js-yaml"
44
import * as os from "os"
55
import * as path from "path"
6-
import { canConnect, generateCertificate, generatePassword, humanPath, paths } from "./util"
6+
import { canConnect, generateCertificate, generatePassword, humanPath, paths, isNodeJSErrnoException } from "./util"
7+
8+
const DEFAULT_SOCKET_PATH = path.join(os.tmpdir(), "vscode-ipc")
79

810
export enum Feature {
911
// No current experimental features!
@@ -667,6 +669,26 @@ export const shouldRunVsCodeCli = (args: Args): boolean => {
667669
return extensionRelatedArgs.some((arg) => argKeys.includes(arg))
668670
}
669671

672+
/**
673+
* Reads the socketPath which defaults to a temporary directory
674+
* with another directory called vscode-ipc.
675+
*
676+
* If it can't read the path, it throws an error and returns undefined.
677+
*/
678+
export async function readSocketPath(path: string): Promise<string | undefined> {
679+
try {
680+
return await fs.readFile(path, "utf8")
681+
} catch (error) {
682+
// If it doesn't exist, we don't care.
683+
// But if it fails for some reason, we should throw.
684+
// We want to surface that to the user.
685+
if (!isNodeJSErrnoException(error) || error.code !== "ENOENT") {
686+
throw error
687+
}
688+
}
689+
return undefined
690+
}
691+
670692
/**
671693
* Determine if it looks like the user is trying to open a file or folder in an
672694
* existing instance. The arguments here should be the arguments the user
@@ -678,32 +700,21 @@ export const shouldOpenInExistingInstance = async (args: Args): Promise<string |
678700
return process.env.VSCODE_IPC_HOOK_CLI
679701
}
680702

681-
const readSocketPath = async (): Promise<string | undefined> => {
682-
try {
683-
return await fs.readFile(path.join(os.tmpdir(), "vscode-ipc"), "utf8")
684-
} catch (error: any) {
685-
if (error.code !== "ENOENT") {
686-
throw error
687-
}
688-
}
689-
return undefined
690-
}
691-
692703
// If these flags are set then assume the user is trying to open in an
693704
// existing instance since these flags have no effect otherwise.
694705
const openInFlagCount = ["reuse-window", "new-window"].reduce((prev, cur) => {
695706
return args[cur as keyof Args] ? prev + 1 : prev
696707
}, 0)
697708
if (openInFlagCount > 0) {
698-
return readSocketPath()
709+
return readSocketPath(DEFAULT_SOCKET_PATH)
699710
}
700711

701712
// It's possible the user is trying to spawn another instance of code-server.
702713
// Check if any unrelated flags are set (check against one because `_` always
703714
// exists), that a file or directory was passed, and that the socket is
704715
// active.
705716
if (Object.keys(args).length === 1 && args._.length > 0) {
706-
const socketPath = await readSocketPath()
717+
const socketPath = await readSocketPath(DEFAULT_SOCKET_PATH)
707718
if (socketPath && (await canConnect(socketPath))) {
708719
return socketPath
709720
}

0 commit comments

Comments
 (0)