| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type { |
| b69ab31 | | | 9 | AbsolutePath, |
| b69ab31 | | | 10 | PlatformSpecificClientToServerMessages, |
| b69ab31 | | | 11 | RepoRelativePath, |
| b69ab31 | | | 12 | ServerToClientMessage, |
| b69ab31 | | | 13 | } from 'isl/src/types'; |
| b69ab31 | | | 14 | import type {Repository} from './Repository'; |
| b69ab31 | | | 15 | import type {RepositoryContext} from './serverTypes'; |
| b69ab31 | | | 16 | |
| b69ab31 | | | 17 | import {spawn} from 'node:child_process'; |
| b69ab31 | | | 18 | import pathModule from 'node:path'; |
| b69ab31 | | | 19 | import {nullthrows} from 'shared/utils'; |
| b69ab31 | | | 20 | |
| b69ab31 | | | 21 | /** |
| b69ab31 | | | 22 | * Platform-specific server-side API for each target: vscode extension host, electron standalone, browser, ... |
| b69ab31 | | | 23 | * See also platform.ts |
| b69ab31 | | | 24 | */ |
| b69ab31 | | | 25 | export interface ServerPlatform { |
| b69ab31 | | | 26 | platformName: string; |
| b69ab31 | | | 27 | /** Override the analytics Session ID. Should be globally unique. */ |
| b69ab31 | | | 28 | sessionId?: string; |
| b69ab31 | | | 29 | handleMessageFromClient( |
| b69ab31 | | | 30 | this: ServerPlatform, |
| b69ab31 | | | 31 | repo: Repository | undefined, |
| b69ab31 | | | 32 | ctx: RepositoryContext, |
| b69ab31 | | | 33 | message: PlatformSpecificClientToServerMessages, |
| b69ab31 | | | 34 | postMessage: (message: ServerToClientMessage) => void, |
| b69ab31 | | | 35 | onDispose: (disapose: () => unknown) => void, |
| b69ab31 | | | 36 | ): void | Promise<void>; |
| b69ab31 | | | 37 | } |
| b69ab31 | | | 38 | |
| b69ab31 | | | 39 | export const browserServerPlatform: ServerPlatform = { |
| b69ab31 | | | 40 | platformName: 'browser', |
| b69ab31 | | | 41 | handleMessageFromClient( |
| b69ab31 | | | 42 | this: ServerPlatform, |
| b69ab31 | | | 43 | repo: Repository | undefined, |
| b69ab31 | | | 44 | ctx: RepositoryContext, |
| b69ab31 | | | 45 | message: PlatformSpecificClientToServerMessages, |
| b69ab31 | | | 46 | ) { |
| b69ab31 | | | 47 | switch (message.type) { |
| b69ab31 | | | 48 | case 'platform/openContainingFolder': { |
| b69ab31 | | | 49 | const absPath: AbsolutePath = pathModule.join( |
| b69ab31 | | | 50 | nullthrows(repo?.info.repoRoot), |
| b69ab31 | | | 51 | message.path, |
| b69ab31 | | | 52 | ); |
| b69ab31 | | | 53 | let args: Array<string> = []; |
| b69ab31 | | | 54 | // use OS-builtin open command to open parent directory |
| b69ab31 | | | 55 | // (which may open different file extensions with different programs) |
| b69ab31 | | | 56 | switch (process.platform) { |
| b69ab31 | | | 57 | case 'darwin': |
| b69ab31 | | | 58 | args = ['/usr/bin/open', pathModule.dirname(absPath)]; |
| b69ab31 | | | 59 | break; |
| b69ab31 | | | 60 | case 'win32': |
| b69ab31 | | | 61 | // On windows, we can select the file in the newly opened explorer window by giving the full path |
| b69ab31 | | | 62 | args = ['explorer.exe', '/select,', absPath]; |
| b69ab31 | | | 63 | break; |
| b69ab31 | | | 64 | case 'linux': |
| b69ab31 | | | 65 | args = ['xdg-open', pathModule.dirname(absPath)]; |
| b69ab31 | | | 66 | break; |
| b69ab31 | | | 67 | } |
| b69ab31 | | | 68 | repo?.initialConnectionContext.logger.log('open file', absPath); |
| b69ab31 | | | 69 | if (args.length > 0) { |
| b69ab31 | | | 70 | spawnInBackground(repo, args); |
| b69ab31 | | | 71 | } |
| b69ab31 | | | 72 | break; |
| b69ab31 | | | 73 | } |
| b69ab31 | | | 74 | case 'platform/openFile': { |
| b69ab31 | | | 75 | openFile(repo, ctx, message.path); |
| b69ab31 | | | 76 | break; |
| b69ab31 | | | 77 | } |
| b69ab31 | | | 78 | case 'platform/openFiles': { |
| b69ab31 | | | 79 | for (const path of message.paths) { |
| b69ab31 | | | 80 | openFile(repo, ctx, path); |
| b69ab31 | | | 81 | } |
| b69ab31 | | | 82 | break; |
| b69ab31 | | | 83 | } |
| b69ab31 | | | 84 | } |
| b69ab31 | | | 85 | }, |
| b69ab31 | | | 86 | }; |
| b69ab31 | | | 87 | |
| b69ab31 | | | 88 | async function openFile( |
| b69ab31 | | | 89 | repo: Repository | undefined, |
| b69ab31 | | | 90 | ctx: RepositoryContext | undefined, |
| b69ab31 | | | 91 | path: RepoRelativePath, |
| b69ab31 | | | 92 | ) { |
| b69ab31 | | | 93 | if (repo == null || ctx == null) { |
| b69ab31 | | | 94 | return; |
| b69ab31 | | | 95 | } |
| b69ab31 | | | 96 | const opener = await repo.getConfig(ctx, 'isl.open-file-cmd'); |
| b69ab31 | | | 97 | const absPath: AbsolutePath = pathModule.join(repo.info.repoRoot, path); |
| b69ab31 | | | 98 | let args: Array<string> = []; |
| b69ab31 | | | 99 | if (opener) { |
| b69ab31 | | | 100 | // opener should be either a JSON string (wrapped in quotes) or a JSON array of strings, |
| b69ab31 | | | 101 | // to include arguments |
| b69ab31 | | | 102 | try { |
| b69ab31 | | | 103 | const jsonOpenerArgs = JSON.parse(opener); |
| b69ab31 | | | 104 | args = Array.isArray(jsonOpenerArgs) |
| b69ab31 | | | 105 | ? [...jsonOpenerArgs, absPath] |
| b69ab31 | | | 106 | : [jsonOpenerArgs, absPath]; |
| b69ab31 | | | 107 | } catch { |
| b69ab31 | | | 108 | // if it's not JSON, it should be a regular string |
| b69ab31 | | | 109 | args = [opener, absPath]; |
| b69ab31 | | | 110 | } |
| b69ab31 | | | 111 | } else { |
| b69ab31 | | | 112 | // by default, use OS-builtin open command to open files |
| b69ab31 | | | 113 | // (which may open different file extensions with different programs) |
| b69ab31 | | | 114 | switch (process.platform) { |
| b69ab31 | | | 115 | case 'darwin': |
| b69ab31 | | | 116 | args = ['/usr/bin/open', absPath]; |
| b69ab31 | | | 117 | break; |
| b69ab31 | | | 118 | case 'win32': |
| b69ab31 | | | 119 | args = ['notepad.exe', absPath]; |
| b69ab31 | | | 120 | break; |
| b69ab31 | | | 121 | case 'linux': |
| b69ab31 | | | 122 | args = ['xdg-open', absPath]; |
| b69ab31 | | | 123 | break; |
| b69ab31 | | | 124 | } |
| b69ab31 | | | 125 | } |
| b69ab31 | | | 126 | repo.initialConnectionContext.logger.log('open file', absPath); |
| b69ab31 | | | 127 | if (args.length > 0) { |
| b69ab31 | | | 128 | spawnInBackground(repo, args); |
| b69ab31 | | | 129 | } |
| b69ab31 | | | 130 | } |
| b69ab31 | | | 131 | |
| b69ab31 | | | 132 | /** |
| b69ab31 | | | 133 | * Because the ISL server is likely running in the background and is |
| b69ab31 | | | 134 | * no longer attached to a terminal, this is designed for the case |
| b69ab31 | | | 135 | * where the user opens the file in a windowed editor (hence |
| b69ab31 | | | 136 | * `windowsHide: false`, which is the default for |
| b69ab31 | | | 137 | * `child_process.spawn()`, but not for `execa()`): |
| b69ab31 | | | 138 | * |
| b69ab31 | | | 139 | * - For users using a simple one-window-per-file graphical text |
| b69ab31 | | | 140 | * editor, like notepad.exe, this is relatively straightforward. |
| b69ab31 | | | 141 | * - For users who prefer a terminal-based editor, like Emacs, |
| b69ab31 | | | 142 | * a conduit like EmacsClient would be required. |
| b69ab31 | | | 143 | * |
| b69ab31 | | | 144 | * Further, killing ISL should not kill the editor, so this follows |
| b69ab31 | | | 145 | * the pattern for spawning an independent, long-running process in |
| b69ab31 | | | 146 | * Node.js as described here: |
| b69ab31 | | | 147 | * |
| b69ab31 | | | 148 | * https://nodejs.org/docs/latest-v10.x/api/child_process.html#child_process_options_detached |
| b69ab31 | | | 149 | */ |
| b69ab31 | | | 150 | function spawnInBackground(repo: Repository | undefined, args: Array<string>) { |
| b69ab31 | | | 151 | // TODO: Report error if spawn() fails? |
| b69ab31 | | | 152 | // TODO: support passing the column/line number to programs that support it? e.g. vscode: `code /path/to/file:10:20` |
| b69ab31 | | | 153 | const proc = spawn(args[0], args.slice(1), { |
| b69ab31 | | | 154 | detached: true, |
| b69ab31 | | | 155 | stdio: 'ignore', |
| b69ab31 | | | 156 | windowsHide: false, |
| b69ab31 | | | 157 | windowsVerbatimArguments: true, |
| b69ab31 | | | 158 | }); |
| b69ab31 | | | 159 | // Silent error. Don't crash the server process. |
| b69ab31 | | | 160 | proc.on('error', err => { |
| b69ab31 | | | 161 | repo?.initialConnectionContext.logger.log('failed to open', args, err); |
| b69ab31 | | | 162 | }); |
| b69ab31 | | | 163 | proc.unref(); |
| b69ab31 | | | 164 | } |