@@ -5,7 +5,7 @@ import * as os from "os"
5
5
import * as path from "path"
6
6
import { Args as VsArgs } from "../../lib/vscode/src/vs/server/ipc"
7
7
import { AuthType } from "./http"
8
- import { generatePassword , humanPath , paths } from "./util"
8
+ import { canConnect , generatePassword , humanPath , paths } from "./util"
9
9
10
10
export class Optional < T > {
11
11
public constructor ( public readonly value ?: T ) { }
@@ -152,12 +152,12 @@ const options: Options<Required<Args>> = {
152
152
"new-window" : {
153
153
type : "boolean" ,
154
154
short : "n" ,
155
- description : "Force to open a new window. (use with open-in) " ,
155
+ description : "Force to open a new window." ,
156
156
} ,
157
157
"reuse-window" : {
158
158
type : "boolean" ,
159
159
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." ,
161
161
} ,
162
162
163
163
locale : { type : "string" } ,
@@ -327,6 +327,21 @@ export const parse = (
327
327
328
328
logger . debug ( "parsed command line" , field ( "args" , args ) )
329
329
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
+
330
345
// --verbose takes priority over --log and --log takes priority over the
331
346
// environment variable.
332
347
if ( args . verbose ) {
@@ -369,21 +384,6 @@ export const parse = (
369
384
return args
370
385
}
371
386
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
-
387
387
async function defaultConfigFile ( ) : Promise < string > {
388
388
return `bind-addr: 127.0.0.1:8080
389
389
auth: password
@@ -410,10 +410,6 @@ export async function readConfigFile(configPath?: string): Promise<Args> {
410
410
logger . info ( `Wrote default config file to ${ humanPath ( configPath ) } ` )
411
411
}
412
412
413
- if ( ! process . env . CODE_SERVER_PARENT_PID && ! process . env . VSCODE_IPC_HOOK_CLI ) {
414
- logger . info ( `Using config file ${ humanPath ( configPath ) } ` )
415
- }
416
-
417
413
const configFile = await fs . readFile ( configPath )
418
414
const config = yaml . safeLoad ( configFile . toString ( ) , {
419
415
filename : configPath ,
@@ -496,3 +492,52 @@ async function copyOldMacOSDataDir(): Promise<void> {
496
492
await fs . copy ( oldDataDir , paths . data )
497
493
}
498
494
}
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