1
+ import { promises as fs , constants } from 'fs' ;
1
2
import { injectable , inject } from '@theia/core/shared/inversify' ;
2
3
import { ILogger } from '@theia/core/lib/common/logger' ;
3
4
import { DefaultWorkspaceServer as TheiaDefaultWorkspaceServer } from '@theia/workspace/lib/node/default-workspace-server' ;
4
5
import { ConfigService } from '../../../common/protocol/config-service' ;
6
+ import { SketchesService } from '../../../common/protocol' ;
7
+ import { FileUri } from '@theia/core/lib/node' ;
5
8
6
9
@injectable ( )
7
10
export class DefaultWorkspaceServer extends TheiaDefaultWorkspaceServer {
@@ -11,13 +14,49 @@ export class DefaultWorkspaceServer extends TheiaDefaultWorkspaceServer {
11
14
@inject ( ILogger )
12
15
protected readonly logger : ILogger ;
13
16
14
- protected override async getWorkspaceURIFromCli ( ) : Promise < string | undefined > {
17
+ @inject ( SketchesService )
18
+ private readonly sketchesService : SketchesService ;
19
+
20
+ override async onStart ( ) : Promise < void > {
21
+ // NOOP
22
+ // No need to remove untitled workspaces. IDE2 does not use workspaces.
23
+ }
24
+
25
+ override async getMostRecentlyUsedWorkspace ( ) : Promise < string | undefined > {
26
+ const uri = await super . getMostRecentlyUsedWorkspace ( ) ;
27
+ if ( ! uri ) {
28
+ const { uri } = await this . sketchesService . createNewSketch ( ) ;
29
+ return uri ;
30
+ }
31
+ return uri ;
32
+ }
33
+
34
+ /**
35
+ * This is the async re-implementation of the default Theia behavior.
36
+ */
37
+ override async getRecentWorkspaces ( ) : Promise < string [ ] > {
38
+ const listUri : string [ ] = [ ] ;
39
+ const data = await this . readRecentWorkspacePathsFromUserHome ( ) ;
40
+ if ( data && data . recentRoots ) {
41
+ await Promise . all (
42
+ data . recentRoots
43
+ . filter ( ( element ) => Boolean ( element ) )
44
+ . map ( async ( element ) => {
45
+ if ( await this . exists ( element ) ) {
46
+ listUri . push ( element ) ;
47
+ }
48
+ } )
49
+ ) ;
50
+ }
51
+ return listUri ;
52
+ }
53
+
54
+ private async exists ( uri : string ) : Promise < boolean > {
15
55
try {
16
- const config = await this . configService . getConfiguration ( ) ;
17
- return config . sketchDirUri ;
18
- } catch ( err ) {
19
- this . logger . error ( `Failed to determine the sketch directory: ${ err } ` ) ;
20
- return super . getWorkspaceURIFromCli ( ) ;
56
+ await fs . access ( FileUri . fsPath ( uri ) , constants . R_OK | constants . W_OK ) ;
57
+ return true ;
58
+ } catch {
59
+ return false ;
21
60
}
22
61
}
23
62
}
0 commit comments