feat(cli): add test for readSocketPath (#4284)
* fix: update isNodeJSErrnoException * refactor(cli): export and purify readSocketPath * feat: add tests for readSocketPath * fix(ci): temporarily disable install deps from cache
This commit is contained in:
@ -3,7 +3,9 @@ import { promises as fs } from "fs"
|
||||
import yaml from "js-yaml"
|
||||
import * as os from "os"
|
||||
import * as path from "path"
|
||||
import { canConnect, generateCertificate, generatePassword, humanPath, paths } from "./util"
|
||||
import { canConnect, generateCertificate, generatePassword, humanPath, paths, isNodeJSErrnoException } from "./util"
|
||||
|
||||
const DEFAULT_SOCKET_PATH = path.join(os.tmpdir(), "vscode-ipc")
|
||||
|
||||
export enum Feature {
|
||||
// No current experimental features!
|
||||
@ -657,6 +659,27 @@ function bindAddrFromAllSources(...argsConfig: Args[]): Addr {
|
||||
return addr
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the socketPath based on path passed in.
|
||||
*
|
||||
* The one usually passed in is the DEFAULT_SOCKET_PATH.
|
||||
*
|
||||
* If it can't read the path, it throws an error and returns undefined.
|
||||
*/
|
||||
export async function readSocketPath(path: string): Promise<string | undefined> {
|
||||
try {
|
||||
return await fs.readFile(path, "utf8")
|
||||
} catch (error) {
|
||||
// If it doesn't exist, we don't care.
|
||||
// But if it fails for some reason, we should throw.
|
||||
// We want to surface that to the user.
|
||||
if (!isNodeJSErrnoException(error) || error.code !== "ENOENT") {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if it looks like the user is trying to open a file or folder in an
|
||||
* existing instance. The arguments here should be the arguments the user
|
||||
@ -668,24 +691,13 @@ export const shouldOpenInExistingInstance = async (args: Args): Promise<string |
|
||||
return process.env.VSCODE_IPC_HOOK_CLI
|
||||
}
|
||||
|
||||
const readSocketPath = async (): Promise<string | undefined> => {
|
||||
try {
|
||||
return await fs.readFile(path.join(os.tmpdir(), "vscode-ipc"), "utf8")
|
||||
} catch (error: any) {
|
||||
if (error.code !== "ENOENT") {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
// If these flags are set then assume the user is trying to open in an
|
||||
// existing instance since these flags have no effect otherwise.
|
||||
const openInFlagCount = ["reuse-window", "new-window"].reduce((prev, cur) => {
|
||||
return args[cur as keyof Args] ? prev + 1 : prev
|
||||
}, 0)
|
||||
if (openInFlagCount > 0) {
|
||||
return readSocketPath()
|
||||
return readSocketPath(DEFAULT_SOCKET_PATH)
|
||||
}
|
||||
|
||||
// It's possible the user is trying to spawn another instance of code-server.
|
||||
@ -693,7 +705,7 @@ export const shouldOpenInExistingInstance = async (args: Args): Promise<string |
|
||||
// exists), that a file or directory was passed, and that the socket is
|
||||
// active.
|
||||
if (Object.keys(args).length === 1 && args._.length > 0) {
|
||||
const socketPath = await readSocketPath()
|
||||
const socketPath = await readSocketPath(DEFAULT_SOCKET_PATH)
|
||||
if (socketPath && (await canConnect(socketPath))) {
|
||||
return socketPath
|
||||
}
|
||||
|
@ -490,7 +490,7 @@ export function escapeHtml(unsafe: string): string {
|
||||
* it has a .code property.
|
||||
*/
|
||||
export function isNodeJSErrnoException(error: unknown): error is NodeJS.ErrnoException {
|
||||
return error instanceof Error && (error as NodeJS.ErrnoException).code !== undefined
|
||||
return error !== undefined && (error as NodeJS.ErrnoException).code !== undefined
|
||||
}
|
||||
|
||||
// TODO: Replace with proper templating system.
|
||||
|
Reference in New Issue
Block a user