@@ -5,7 +5,7 @@ import * as os from "os"
55import * as path from "path"
66import { Args as VsArgs } from "../../lib/vscode/src/vs/server/ipc"
77import { AuthType } from "./http"
8- import { generatePassword , humanPath , paths } from "./util"
8+ import { canConnect , generatePassword , humanPath , paths } from "./util"
99
1010export class Optional < T > {
1111 public constructor ( public readonly value ?: T ) { }
@@ -152,12 +152,12 @@ const options: Options<Required<Args>> = {
152152 "new-window" : {
153153 type : "boolean" ,
154154 short : "n" ,
155- description : "Force to open a new window. (use with open-in) " ,
155+ description : "Force to open a new window." ,
156156 } ,
157157 "reuse-window" : {
158158 type : "boolean" ,
159159 short : "r" ,
160- description : "Force to open a file or folder in an already opened window. (use with open-in) " ,
160+ description : "Force to open a file or folder in an already opened window." ,
161161 } ,
162162
163163 locale : { type : "string" } ,
@@ -327,6 +327,21 @@ export const parse = (
327327
328328 logger . debug ( "parsed command line" , field ( "args" , args ) )
329329
330+ return args
331+ }
332+
333+ export async function setDefaults ( args : Args ) : Promise < Args > {
334+ args = { ...args }
335+
336+ if ( ! args [ "user-data-dir" ] ) {
337+ await copyOldMacOSDataDir ( )
338+ args [ "user-data-dir" ] = paths . data
339+ }
340+
341+ if ( ! args [ "extensions-dir" ] ) {
342+ args [ "extensions-dir" ] = path . join ( args [ "user-data-dir" ] , "extensions" )
343+ }
344+
330345 // --verbose takes priority over --log and --log takes priority over the
331346 // environment variable.
332347 if ( args . verbose ) {
@@ -369,21 +384,6 @@ export const parse = (
369384 return args
370385}
371386
372- export async function setDefaults ( args : Args ) : Promise < Args > {
373- args = { ...args }
374-
375- if ( ! args [ "user-data-dir" ] ) {
376- await copyOldMacOSDataDir ( )
377- args [ "user-data-dir" ] = paths . data
378- }
379-
380- if ( ! args [ "extensions-dir" ] ) {
381- args [ "extensions-dir" ] = path . join ( args [ "user-data-dir" ] , "extensions" )
382- }
383-
384- return args
385- }
386-
387387async function defaultConfigFile ( ) : Promise < string > {
388388 return `bind-addr: 127.0.0.1:8080
389389auth: password
@@ -410,10 +410,6 @@ export async function readConfigFile(configPath?: string): Promise<Args> {
410410 logger . info ( `Wrote default config file to ${ humanPath ( configPath ) } ` )
411411 }
412412
413- if ( ! process . env . CODE_SERVER_PARENT_PID && ! process . env . VSCODE_IPC_HOOK_CLI ) {
414- logger . info ( `Using config file ${ humanPath ( configPath ) } ` )
415- }
416-
417413 const configFile = await fs . readFile ( configPath )
418414 const config = yaml . safeLoad ( configFile . toString ( ) , {
419415 filename : configPath ,
@@ -496,3 +492,52 @@ async function copyOldMacOSDataDir(): Promise<void> {
496492 await fs . copy ( oldDataDir , paths . data )
497493 }
498494}
495+
496+ export const shouldRunVsCodeCli = ( args : Args ) : boolean => {
497+ return ! ! args [ "list-extensions" ] || ! ! args [ "install-extension" ] || ! ! args [ "uninstall-extension" ]
498+ }
499+
500+ /**
501+ * Determine if it looks like the user is trying to open a file or folder in an
502+ * existing instance. The arguments here should be the arguments the user
503+ * explicitly passed on the command line, not defaults or the configuration.
504+ */
505+ export const shouldOpenInExistingInstance = async ( args : Args ) : Promise < string | undefined > => {
506+ // Always use the existing instance if we're running from VS Code's terminal.
507+ if ( process . env . VSCODE_IPC_HOOK_CLI ) {
508+ return process . env . VSCODE_IPC_HOOK_CLI
509+ }
510+
511+ const readSocketPath = async ( ) : Promise < string | undefined > => {
512+ try {
513+ return await fs . readFile ( path . join ( os . tmpdir ( ) , "vscode-ipc" ) , "utf8" )
514+ } catch ( error ) {
515+ if ( error . code !== "ENOENT" ) {
516+ throw error
517+ }
518+ }
519+ return undefined
520+ }
521+
522+ // If these flags are set then assume the user is trying to open in an
523+ // existing instance since these flags have no effect otherwise.
524+ const openInFlagCount = [ "reuse-window" , "new-window" ] . reduce ( ( prev , cur ) => {
525+ return args [ cur as keyof Args ] ? prev + 1 : prev
526+ } , 0 )
527+ if ( openInFlagCount > 0 ) {
528+ return readSocketPath ( )
529+ }
530+
531+ // It's possible the user is trying to spawn another instance of code-server.
532+ // Check if any unrelated flags are set (check against one because `_` always
533+ // exists), that a file or directory was passed, and that the socket is
534+ // active.
535+ if ( Object . keys ( args ) . length === 1 && args . _ . length > 0 ) {
536+ const socketPath = await readSocketPath ( )
537+ if ( socketPath && ( await canConnect ( socketPath ) ) ) {
538+ return socketPath
539+ }
540+ }
541+
542+ return undefined
543+ }
0 commit comments