Skip to content

Commit 9c8ab46

Browse files
committed
refactor(cli): export and purify readSocketPath
1 parent c4c439f commit 9c8ab46

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!
@@ -657,6 +659,26 @@ function bindAddrFromAllSources(...argsConfig: Args[]): Addr {
657659
return addr
658660
}
659661

662+
/**
663+
* Reads the socketPath which defaults to a temporary directory
664+
* with another directory called vscode-ipc.
665+
*
666+
* If it can't read the path, it throws an error and returns undefined.
667+
*/
668+
export async function readSocketPath(path: string): Promise<string | undefined> {
669+
try {
670+
return await fs.readFile(path, "utf8")
671+
} catch (error) {
672+
// If it doesn't exist, we don't care.
673+
// But if it fails for some reason, we should throw.
674+
// We want to surface that to the user.
675+
if (!isNodeJSErrnoException(error) || error.code !== "ENOENT") {
676+
throw error
677+
}
678+
}
679+
return undefined
680+
}
681+
660682
/**
661683
* Determine if it looks like the user is trying to open a file or folder in an
662684
* existing instance. The arguments here should be the arguments the user
@@ -668,32 +690,21 @@ export const shouldOpenInExistingInstance = async (args: Args): Promise<string |
668690
return process.env.VSCODE_IPC_HOOK_CLI
669691
}
670692

671-
const readSocketPath = async (): Promise<string | undefined> => {
672-
try {
673-
return await fs.readFile(path.join(os.tmpdir(), "vscode-ipc"), "utf8")
674-
} catch (error: any) {
675-
if (error.code !== "ENOENT") {
676-
throw error
677-
}
678-
}
679-
return undefined
680-
}
681-
682693
// If these flags are set then assume the user is trying to open in an
683694
// existing instance since these flags have no effect otherwise.
684695
const openInFlagCount = ["reuse-window", "new-window"].reduce((prev, cur) => {
685696
return args[cur as keyof Args] ? prev + 1 : prev
686697
}, 0)
687698
if (openInFlagCount > 0) {
688-
return readSocketPath()
699+
return readSocketPath(DEFAULT_SOCKET_PATH)
689700
}
690701

691702
// It's possible the user is trying to spawn another instance of code-server.
692703
// Check if any unrelated flags are set (check against one because `_` always
693704
// exists), that a file or directory was passed, and that the socket is
694705
// active.
695706
if (Object.keys(args).length === 1 && args._.length > 0) {
696-
const socketPath = await readSocketPath()
707+
const socketPath = await readSocketPath(DEFAULT_SOCKET_PATH)
697708
if (socketPath && (await canConnect(socketPath))) {
698709
return socketPath
699710
}

0 commit comments

Comments
 (0)