|
1 | 1 | import { promises as fs, constants } from 'fs';
|
2 | 2 | import { injectable, inject } from '@theia/core/shared/inversify';
|
3 |
| -import { |
4 |
| - DefaultWorkspaceServer as TheiaDefaultWorkspaceServer, |
5 |
| - WorkspaceCliContribution as TheiaWorkspaceCliContribution, |
6 |
| -} from '@theia/workspace/lib/node/default-workspace-server'; |
| 3 | +import { DefaultWorkspaceServer as TheiaDefaultWorkspaceServer } from '@theia/workspace/lib/node/default-workspace-server'; |
7 | 4 | import { SketchesService } from '../../../common/protocol';
|
8 |
| -import { FileUri } from '@theia/core/lib/node/file-uri'; |
| 5 | +import { FileUri } from '@theia/core/lib/node'; |
9 | 6 | import { IsTempSketch } from '../../is-temp-sketch';
|
10 |
| -import { EnvVariablesServer } from '@theia/core/lib/common/env-variables'; |
11 |
| -import { Deferred } from '@theia/core/lib/common/promise-util'; |
12 |
| -import { MaybePromise } from '@theia/core/lib/common/types'; |
13 |
| -import URI from '@theia/core/lib/common/uri'; |
14 |
| -import * as yargs from '@theia/core/shared/yargs'; |
15 |
| -import { THEIA_EXT } from '@theia/workspace/lib/common/utils'; |
16 |
| -import * as fsExtra from 'fs-extra'; |
17 |
| -import * as path from 'path'; |
18 |
| - |
19 |
| -@injectable() |
20 |
| -export class WorkspaceCliContribution extends TheiaWorkspaceCliContribution { |
21 |
| - @inject(EnvVariablesServer) |
22 |
| - private readonly envVariablesServer: EnvVariablesServer; |
23 |
| - |
24 |
| - override workspaceRoot = new Deferred<string | undefined>(); |
25 |
| - |
26 |
| - override configure(conf: yargs.Argv): void { |
27 |
| - conf.usage('$0 [workspace-directories] [options]'); |
28 |
| - conf.option('root-dir', { |
29 |
| - description: 'DEPRECATED: Sets the workspace directory.', |
30 |
| - }); |
31 |
| - } |
32 |
| - |
33 |
| - override async setArguments(args: yargs.Arguments): Promise<void> { |
34 |
| - const workspaceArguments = args._.slice(2).map((probablyAlreadyString) => |
35 |
| - String(probablyAlreadyString) |
36 |
| - ); |
37 |
| - if (workspaceArguments.length === 0 && args['root-dir']) { |
38 |
| - workspaceArguments.push(String(args['root-dir'])); |
39 |
| - } |
40 |
| - if (workspaceArguments.length === 0) { |
41 |
| - this.workspaceRoot.resolve(undefined); |
42 |
| - } else if (workspaceArguments.length === 1) { |
43 |
| - this.workspaceRoot.resolve( |
44 |
| - this.normalizeWorkspaceArg(workspaceArguments[0]) |
45 |
| - ); |
46 |
| - } else { |
47 |
| - this.workspaceRoot.resolve( |
48 |
| - await this.buildWorkspaceForMultipleArguments(workspaceArguments) |
49 |
| - ); |
50 |
| - } |
51 |
| - } |
52 |
| - |
53 |
| - protected normalizeWorkspaceArg(raw: string): string { |
54 |
| - return path.resolve(raw).replace(/\/$/, ''); |
55 |
| - } |
56 |
| - |
57 |
| - protected async buildWorkspaceForMultipleArguments( |
58 |
| - workspaceArguments: string[] |
59 |
| - ): Promise<string | undefined> { |
60 |
| - try { |
61 |
| - const dirs = await Promise.all( |
62 |
| - workspaceArguments.map(async (maybeDir) => |
63 |
| - (await fs.stat(maybeDir).catch(() => undefined))?.isDirectory() |
64 |
| - ) |
65 |
| - ); |
66 |
| - const folders = workspaceArguments |
67 |
| - .filter((_, index) => dirs[index]) |
68 |
| - .map((dir) => ({ path: this.normalizeWorkspaceArg(dir) })); |
69 |
| - if (folders.length < 2) { |
70 |
| - return folders[0]?.path; |
71 |
| - } |
72 |
| - const untitledWorkspaceUri = await this.getUntitledWorkspaceUri( |
73 |
| - new URI(await this.envVariablesServer.getConfigDirUri()), |
74 |
| - async (uri) => !(await fsExtra.pathExists(uri.path.fsPath())) |
75 |
| - ); |
76 |
| - const untitledWorkspacePath = untitledWorkspaceUri.path.fsPath(); |
77 |
| - |
78 |
| - await fsExtra.ensureDir(path.dirname(untitledWorkspacePath)); |
79 |
| - await fs.writeFile( |
80 |
| - untitledWorkspacePath, |
81 |
| - JSON.stringify({ folders }, undefined, 4) |
82 |
| - ); |
83 |
| - return untitledWorkspacePath; |
84 |
| - } catch { |
85 |
| - return undefined; |
86 |
| - } |
87 |
| - } |
88 |
| - |
89 |
| - async getUntitledWorkspaceUri( |
90 |
| - configDirUri: URI, |
91 |
| - isAcceptable: (candidate: URI) => MaybePromise<boolean>, |
92 |
| - warnOnHits?: () => unknown |
93 |
| - ): Promise<URI> { |
94 |
| - const parentDir = configDirUri.resolve('workspaces'); |
95 |
| - let uri; |
96 |
| - let attempts = 0; |
97 |
| - do { |
98 |
| - attempts++; |
99 |
| - uri = parentDir.resolve( |
100 |
| - `Untitled-${Math.round(Math.random() * 1000)}.${THEIA_EXT}` |
101 |
| - ); |
102 |
| - if (attempts === 10) { |
103 |
| - warnOnHits?.(); |
104 |
| - } |
105 |
| - if (attempts === 50) { |
106 |
| - throw new Error( |
107 |
| - 'Workspace Service: too many attempts to find unused filename.' |
108 |
| - ); |
109 |
| - } |
110 |
| - } while (!(await isAcceptable(uri))); |
111 |
| - return uri; |
112 |
| - } |
113 |
| -} |
114 | 7 |
|
115 | 8 | @injectable()
|
116 | 9 | export class DefaultWorkspaceServer extends TheiaDefaultWorkspaceServer {
|
|
0 commit comments