( args: UserProvidedArgs, sessionSocket: string, )
| 844 | * explicitly passed on the command line, *NOT DEFAULTS* or the configuration. |
| 845 | */ |
| 846 | export const shouldOpenInExistingInstance = async ( |
| 847 | args: UserProvidedArgs, |
| 848 | sessionSocket: string, |
| 849 | ): Promise<string | undefined> => { |
| 850 | // Always use the existing instance if we're running from VS Code's terminal. |
| 851 | if (process.env.VSCODE_IPC_HOOK_CLI) { |
| 852 | logger.debug("Found VSCODE_IPC_HOOK_CLI") |
| 853 | return process.env.VSCODE_IPC_HOOK_CLI |
| 854 | } |
| 855 | |
| 856 | const paths = getResolvedPathsFromArgs(args) |
| 857 | const client = new EditorSessionManagerClient(sessionSocket) |
| 858 | |
| 859 | // If these flags are set then assume the user is trying to open in an |
| 860 | // existing instance since these flags have no effect otherwise. That means |
| 861 | // if there is no existing instance we should error rather than falling back |
| 862 | // to spawning code-server normally. |
| 863 | const openInFlagCount = ["reuse-window", "new-window"].reduce((prev, cur) => { |
| 864 | return args[cur as keyof UserProvidedArgs] ? prev + 1 : prev |
| 865 | }, 0) |
| 866 | if (openInFlagCount > 0) { |
| 867 | logger.debug("Found --reuse-window or --new-window") |
| 868 | const socketPath = await client.getConnectedSocketPath(paths[0]) |
| 869 | if (!socketPath) { |
| 870 | throw new Error(`No opened code-server instances found to handle ${paths[0]}`) |
| 871 | } |
| 872 | return socketPath |
| 873 | } |
| 874 | |
| 875 | // It's possible the user is trying to spawn another instance of code-server. |
| 876 | // 1. Check if any unrelated flags are set (this should only run when |
| 877 | // code-server is invoked exactly like this: `code-server my-file`). |
| 878 | // 2. That a file or directory was passed. |
| 879 | // 3. That the socket is active. |
| 880 | // 4. That an instance exists to handle the path (implied by #3). |
| 881 | if (Object.keys(args).length === 1 && typeof args._ !== "undefined" && args._.length > 0) { |
| 882 | if (!(await client.canConnect())) { |
| 883 | return undefined |
| 884 | } |
| 885 | const socketPath = await client.getConnectedSocketPath(paths[0]) |
| 886 | if (socketPath) { |
| 887 | logger.debug("Found existing code-server socket") |
| 888 | return socketPath |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | return undefined |
| 893 | } |
| 894 | |
| 895 | /** |
| 896 | * Arguments for running Code's server. |
no test coverage detected