@@ -29,32 +29,55 @@ import URI from '@theia/core/lib/common/uri';
2929@injectable ( )
3030export class WorkspaceCliContribution implements CliContribution {
3131
32+ @inject ( EnvVariablesServer ) protected readonly envVariablesServer : EnvVariablesServer ;
33+ @inject ( CommonWorkspaceUtils ) protected readonly workspaceUtils : CommonWorkspaceUtils ;
34+
3235 workspaceRoot = new Deferred < string | undefined > ( ) ;
3336
3437 configure ( conf : yargs . Argv ) : void {
35- conf . usage ( '$0 [workspace-directory ] [options]' ) ;
38+ conf . usage ( '$0 [workspace-directories ] [options]' ) ;
3639 conf . option ( 'root-dir' , {
3740 description : 'DEPRECATED: Sets the workspace directory.' ,
3841 } ) ;
3942 }
4043
41- setArguments ( args : yargs . Arguments ) : void {
42- let wsPath : string | undefined = typeof args . _ [ 2 ] === 'undefined' ? undefined : String ( args . _ [ 2 ] ) ;
43- if ( ! wsPath ) {
44- wsPath = args [ 'root-dir' ] as string ;
45- if ( ! wsPath ) {
46- this . workspaceRoot . resolve ( undefined ) ;
47- return ;
48- }
44+ async setArguments ( args : yargs . Arguments ) : Promise < void > {
45+ const workspaceArguments = args . _ . slice ( 2 ) . map ( probablyAlreadyString => String ( probablyAlreadyString ) ) ;
46+ if ( workspaceArguments . length === 0 && args [ 'root-dir' ] ) {
47+ workspaceArguments . push ( String ( args [ 'root-dir' ] ) ) ;
4948 }
50- if ( ! path . isAbsolute ( wsPath ) ) {
51- const cwd = process . cwd ( ) ;
52- wsPath = path . join ( cwd , wsPath ) ;
49+ if ( workspaceArguments . length === 0 ) {
50+ this . workspaceRoot . resolve ( undefined ) ;
51+ } else if ( workspaceArguments . length === 1 ) {
52+ this . workspaceRoot . resolve ( this . normalizeWorkspaceArg ( workspaceArguments [ 0 ] ) ) ;
53+ } else {
54+ this . workspaceRoot . resolve ( this . buildWorkspaceForMultipleArguments ( workspaceArguments ) ) ;
5355 }
54- if ( wsPath && wsPath . endsWith ( '/' ) ) {
55- wsPath = wsPath . slice ( 0 , - 1 ) ;
56+ }
57+
58+ protected normalizeWorkspaceArg ( raw : string ) : string {
59+ return path . resolve ( raw ) . replace ( / \/ $ / , '' ) ;
60+ }
61+
62+ protected async buildWorkspaceForMultipleArguments ( workspaceArguments : string [ ] ) : Promise < string | undefined > {
63+ try {
64+ const dirs = await Promise . all ( workspaceArguments . map ( async maybeDir => ( await fs . stat ( maybeDir ) . catch ( ( ) => undefined ) ) ?. isDirectory ( ) ) ) ;
65+ const folders = workspaceArguments . filter ( ( _ , index ) => dirs [ index ] ) . map ( dir => ( { path : this . normalizeWorkspaceArg ( dir ) } ) ) ;
66+ if ( folders . length < 2 ) {
67+ return folders [ 0 ] ?. path ;
68+ }
69+ const untitledWorkspaceUri = await this . workspaceUtils . getUntitledWorkspaceUri (
70+ new URI ( await this . envVariablesServer . getConfigDirUri ( ) ) ,
71+ async uri => ! await fs . pathExists ( uri . path . fsPath ( ) ) ,
72+ ) ;
73+ const untitledWorkspacePath = untitledWorkspaceUri . path . fsPath ( ) ;
74+
75+ await fs . ensureDir ( path . dirname ( untitledWorkspacePath ) ) ;
76+ await fs . writeFile ( untitledWorkspacePath , JSON . stringify ( { folders } , undefined , 4 ) ) ;
77+ return untitledWorkspacePath ;
78+ } catch {
79+ return undefined ;
5680 }
57- this . workspaceRoot . resolve ( wsPath ) ;
5881 }
5982}
6083
0 commit comments