@theia/workspace 1.45.1 → 1.46.0-next.72

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/README.md +30 -30
  2. package/lib/browser/canonical-uri-service.d.ts +11 -11
  3. package/lib/browser/canonical-uri-service.js +59 -59
  4. package/lib/browser/diff-service.d.ts +10 -10
  5. package/lib/browser/diff-service.js +85 -85
  6. package/lib/browser/index.d.ts +7 -7
  7. package/lib/browser/index.js +34 -34
  8. package/lib/browser/quick-open-workspace.d.ts +25 -25
  9. package/lib/browser/quick-open-workspace.js +137 -137
  10. package/lib/browser/untitled-workspace-exit-dialog.d.ts +19 -19
  11. package/lib/browser/untitled-workspace-exit-dialog.js +74 -74
  12. package/lib/browser/workspace-breadcrumbs-contribution.d.ts +10 -10
  13. package/lib/browser/workspace-breadcrumbs-contribution.js +66 -66
  14. package/lib/browser/workspace-commands.d.ts +117 -117
  15. package/lib/browser/workspace-commands.js +601 -601
  16. package/lib/browser/workspace-commands.spec.d.ts +1 -1
  17. package/lib/browser/workspace-commands.spec.js +127 -127
  18. package/lib/browser/workspace-compare-handler.d.ts +26 -26
  19. package/lib/browser/workspace-compare-handler.js +66 -66
  20. package/lib/browser/workspace-delete-handler.d.ts +75 -75
  21. package/lib/browser/workspace-delete-handler.js +214 -214
  22. package/lib/browser/workspace-duplicate-handler.d.ts +30 -30
  23. package/lib/browser/workspace-duplicate-handler.js +86 -86
  24. package/lib/browser/workspace-frontend-contribution.d.ts +110 -110
  25. package/lib/browser/workspace-frontend-contribution.js +546 -546
  26. package/lib/browser/workspace-frontend-contribution.js.map +1 -1
  27. package/lib/browser/workspace-frontend-module.d.ts +3 -3
  28. package/lib/browser/workspace-frontend-module.js +91 -91
  29. package/lib/browser/workspace-input-dialog.d.ts +19 -19
  30. package/lib/browser/workspace-input-dialog.js +74 -74
  31. package/lib/browser/workspace-preferences.d.ts +11 -11
  32. package/lib/browser/workspace-preferences.js +46 -46
  33. package/lib/browser/workspace-schema-updater.d.ts +34 -34
  34. package/lib/browser/workspace-schema-updater.js +153 -153
  35. package/lib/browser/workspace-service.d.ts +184 -184
  36. package/lib/browser/workspace-service.js +739 -739
  37. package/lib/browser/workspace-storage-service.d.ts +15 -15
  38. package/lib/browser/workspace-storage-service.js +80 -80
  39. package/lib/browser/workspace-trust-preferences.d.ts +21 -21
  40. package/lib/browser/workspace-trust-preferences.js +65 -65
  41. package/lib/browser/workspace-trust-service.d.ts +27 -27
  42. package/lib/browser/workspace-trust-service.js +152 -152
  43. package/lib/browser/workspace-uri-contribution.d.ts +20 -20
  44. package/lib/browser/workspace-uri-contribution.js +113 -113
  45. package/lib/browser/workspace-uri-contribution.spec.d.ts +1 -1
  46. package/lib/browser/workspace-uri-contribution.spec.js +169 -169
  47. package/lib/browser/workspace-user-working-directory-provider.d.ts +12 -12
  48. package/lib/browser/workspace-user-working-directory-provider.js +62 -62
  49. package/lib/browser/workspace-utils.d.ts +15 -15
  50. package/lib/browser/workspace-utils.js +54 -54
  51. package/lib/browser/workspace-variable-contribution.d.ts +23 -23
  52. package/lib/browser/workspace-variable-contribution.js +237 -237
  53. package/lib/browser/workspace-window-title-updater.d.ts +7 -7
  54. package/lib/browser/workspace-window-title-updater.js +57 -57
  55. package/lib/browser-only/browser-only-workspace-server.d.ts +13 -0
  56. package/lib/browser-only/browser-only-workspace-server.d.ts.map +1 -0
  57. package/lib/browser-only/browser-only-workspace-server.js +81 -0
  58. package/lib/browser-only/browser-only-workspace-server.js.map +1 -0
  59. package/lib/browser-only/workspace-frontend-only-module.d.ts +4 -0
  60. package/lib/browser-only/workspace-frontend-only-module.d.ts.map +1 -0
  61. package/lib/browser-only/workspace-frontend-only-module.js +30 -0
  62. package/lib/browser-only/workspace-frontend-only-module.js.map +1 -0
  63. package/lib/common/index.d.ts +3 -3
  64. package/lib/common/index.js +30 -30
  65. package/lib/common/test/mock-workspace-server.d.ts +7 -7
  66. package/lib/common/test/mock-workspace-server.js +35 -35
  67. package/lib/common/untitled-workspace-service.d.ts +8 -8
  68. package/lib/common/untitled-workspace-service.js +60 -60
  69. package/lib/common/workspace-file-service.d.ts +26 -26
  70. package/lib/common/workspace-file-service.js +71 -71
  71. package/lib/common/workspace-protocol.d.ts +26 -26
  72. package/lib/common/workspace-protocol.js +23 -23
  73. package/lib/node/default-workspace-server.d.ts +65 -65
  74. package/lib/node/default-workspace-server.js +248 -248
  75. package/lib/node/default-workspace-server.spec.d.ts +1 -1
  76. package/lib/node/default-workspace-server.spec.js +85 -85
  77. package/lib/node/index.d.ts +2 -2
  78. package/lib/node/index.js +29 -29
  79. package/lib/node/workspace-backend-module.d.ts +3 -3
  80. package/lib/node/workspace-backend-module.js +33 -33
  81. package/package.json +9 -6
  82. package/src/browser/canonical-uri-service.ts +57 -57
  83. package/src/browser/diff-service.ts +62 -62
  84. package/src/browser/index.ts +23 -23
  85. package/src/browser/quick-open-workspace.ts +112 -112
  86. package/src/browser/untitled-workspace-exit-dialog.ts +70 -70
  87. package/src/browser/workspace-breadcrumbs-contribution.ts +56 -56
  88. package/src/browser/workspace-commands.spec.ts +153 -153
  89. package/src/browser/workspace-commands.ts +588 -588
  90. package/src/browser/workspace-compare-handler.ts +56 -56
  91. package/src/browser/workspace-delete-handler.ts +212 -212
  92. package/src/browser/workspace-duplicate-handler.ts +75 -75
  93. package/src/browser/workspace-frontend-contribution.ts +537 -537
  94. package/src/browser/workspace-frontend-module.ts +118 -118
  95. package/src/browser/workspace-input-dialog.ts +61 -61
  96. package/src/browser/workspace-preferences.ts +58 -58
  97. package/src/browser/workspace-schema-updater.ts +150 -150
  98. package/src/browser/workspace-service.ts +780 -780
  99. package/src/browser/workspace-storage-service.ts +67 -67
  100. package/src/browser/workspace-trust-preferences.ts +76 -76
  101. package/src/browser/workspace-trust-service.ts +147 -147
  102. package/src/browser/workspace-uri-contribution.spec.ts +191 -191
  103. package/src/browser/workspace-uri-contribution.ts +97 -97
  104. package/src/browser/workspace-user-working-directory-provider.ts +49 -49
  105. package/src/browser/workspace-utils.ts +45 -45
  106. package/src/browser/workspace-variable-contribution.ts +222 -222
  107. package/src/browser/workspace-window-title-updater.ts +45 -45
  108. package/src/browser-only/browser-only-workspace-server.ts +69 -0
  109. package/src/browser-only/workspace-frontend-only-module.ts +28 -0
  110. package/src/common/index.ts +19 -19
  111. package/src/common/test/mock-workspace-server.ts +29 -29
  112. package/src/common/untitled-workspace-service.ts +50 -50
  113. package/src/common/workspace-file-service.ts +72 -72
  114. package/src/common/workspace-protocol.ts +47 -47
  115. package/src/node/default-workspace-server.spec.ts +100 -100
  116. package/src/node/default-workspace-server.ts +244 -244
  117. package/src/node/index.ts +18 -18
  118. package/src/node/workspace-backend-module.ts +38 -38
@@ -1,244 +1,244 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2017 TypeFox and others.
3
- //
4
- // This program and the accompanying materials are made available under the
5
- // terms of the Eclipse Public License v. 2.0 which is available at
6
- // http://www.eclipse.org/legal/epl-2.0.
7
- //
8
- // This Source Code may also be made available under the following Secondary
9
- // Licenses when the conditions for such availability set forth in the Eclipse
10
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
- // with the GNU Classpath Exception which is available at
12
- // https://www.gnu.org/software/classpath/license.html.
13
- //
14
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
- // *****************************************************************************
16
-
17
- import * as path from 'path';
18
- import * as yargs from '@theia/core/shared/yargs';
19
- import * as fs from '@theia/core/shared/fs-extra';
20
- import * as jsoncparser from 'jsonc-parser';
21
- import { injectable, inject, postConstruct } from '@theia/core/shared/inversify';
22
- import { FileUri, BackendApplicationContribution } from '@theia/core/lib/node';
23
- import { CliContribution } from '@theia/core/lib/node/cli';
24
- import { Deferred } from '@theia/core/lib/common/promise-util';
25
- import { WorkspaceServer, UntitledWorkspaceService } from '../common';
26
- import { EnvVariablesServer } from '@theia/core/lib/common/env-variables';
27
- import URI from '@theia/core/lib/common/uri';
28
- import { notEmpty } from '@theia/core';
29
-
30
- @injectable()
31
- export class WorkspaceCliContribution implements CliContribution {
32
-
33
- @inject(EnvVariablesServer) protected readonly envVariablesServer: EnvVariablesServer;
34
- @inject(UntitledWorkspaceService) protected readonly untitledWorkspaceService: UntitledWorkspaceService;
35
-
36
- workspaceRoot = new Deferred<string | undefined>();
37
-
38
- configure(conf: yargs.Argv): void {
39
- conf.usage('$0 [workspace-directories] [options]');
40
- conf.option('root-dir', {
41
- description: 'DEPRECATED: Sets the workspace directory.',
42
- });
43
- }
44
-
45
- async setArguments(args: yargs.Arguments): Promise<void> {
46
- const workspaceArguments = args._.map(probablyAlreadyString => String(probablyAlreadyString));
47
- if (workspaceArguments.length === 0 && args['root-dir']) {
48
- workspaceArguments.push(String(args['root-dir']));
49
- }
50
- if (workspaceArguments.length === 0) {
51
- this.workspaceRoot.resolve(undefined);
52
- } else if (workspaceArguments.length === 1) {
53
- this.workspaceRoot.resolve(this.normalizeWorkspaceArg(workspaceArguments[0]));
54
- } else {
55
- this.workspaceRoot.resolve(this.buildWorkspaceForMultipleArguments(workspaceArguments));
56
- }
57
- }
58
-
59
- protected normalizeWorkspaceArg(raw: string): string {
60
- return path.resolve(raw).replace(/\/$/, '');
61
- }
62
-
63
- protected async buildWorkspaceForMultipleArguments(workspaceArguments: string[]): Promise<string | undefined> {
64
- try {
65
- const dirs = await Promise.all(workspaceArguments.map(async maybeDir => (await fs.stat(maybeDir).catch(() => undefined))?.isDirectory()));
66
- const folders = workspaceArguments.filter((_, index) => dirs[index]).map(dir => ({ path: this.normalizeWorkspaceArg(dir) }));
67
- if (folders.length < 2) {
68
- return folders[0]?.path;
69
- }
70
- const untitledWorkspaceUri = await this.untitledWorkspaceService.getUntitledWorkspaceUri(
71
- new URI(await this.envVariablesServer.getConfigDirUri()),
72
- async uri => !await fs.pathExists(uri.path.fsPath()),
73
- );
74
- const untitledWorkspacePath = untitledWorkspaceUri.path.fsPath();
75
-
76
- await fs.ensureDir(path.dirname(untitledWorkspacePath));
77
- await fs.writeFile(untitledWorkspacePath, JSON.stringify({ folders }, undefined, 4));
78
- return untitledWorkspacePath;
79
- } catch {
80
- return undefined;
81
- }
82
- }
83
- }
84
-
85
- @injectable()
86
- export class DefaultWorkspaceServer implements WorkspaceServer, BackendApplicationContribution {
87
-
88
- protected root: Deferred<string | undefined> = new Deferred();
89
- /**
90
- * Untitled workspaces that are not among the most recent N workspaces will be deleted on start. Increase this number to keep older files,
91
- * lower it to delete stale untitled workspaces more aggressively.
92
- */
93
- protected untitledWorkspaceStaleThreshold = 10;
94
-
95
- @inject(WorkspaceCliContribution)
96
- protected readonly cliParams: WorkspaceCliContribution;
97
-
98
- @inject(EnvVariablesServer)
99
- protected readonly envServer: EnvVariablesServer;
100
-
101
- @inject(UntitledWorkspaceService)
102
- protected readonly untitledWorkspaceService: UntitledWorkspaceService;
103
-
104
- @postConstruct()
105
- protected init(): void {
106
- this.doInit();
107
- }
108
-
109
- protected async doInit(): Promise<void> {
110
- const root = await this.getRoot();
111
- this.root.resolve(root);
112
- }
113
-
114
- async onStart(): Promise<void> {
115
- await this.removeOldUntitledWorkspaces();
116
- }
117
-
118
- protected async getRoot(): Promise<string | undefined> {
119
- let root = await this.getWorkspaceURIFromCli();
120
- if (!root) {
121
- const data = await this.readRecentWorkspacePathsFromUserHome();
122
- if (data && data.recentRoots) {
123
- root = data.recentRoots[0];
124
- }
125
- }
126
- return root;
127
- }
128
-
129
- getMostRecentlyUsedWorkspace(): Promise<string | undefined> {
130
- return this.root.promise;
131
- }
132
-
133
- async setMostRecentlyUsedWorkspace(rawUri: string): Promise<void> {
134
- const uri = rawUri && new URI(rawUri).toString(); // the empty string is used as a signal from the frontend not to load a workspace.
135
- this.root = new Deferred();
136
- this.root.resolve(uri);
137
- const recentRoots = Array.from(new Set([uri, ...await this.getRecentWorkspaces()]));
138
- this.writeToUserHome({ recentRoots });
139
- }
140
-
141
- async removeRecentWorkspace(rawUri: string): Promise<void> {
142
- const uri = rawUri && new URI(rawUri).toString(); // the empty string is used as a signal from the frontend not to load a workspace.
143
- const recentRoots = await this.getRecentWorkspaces();
144
- const index = recentRoots.indexOf(uri);
145
- if (index !== -1) {
146
- recentRoots.splice(index, 1);
147
- this.writeToUserHome({
148
- recentRoots
149
- });
150
- }
151
- }
152
-
153
- async getRecentWorkspaces(): Promise<string[]> {
154
- const data = await this.readRecentWorkspacePathsFromUserHome();
155
- if (data && data.recentRoots) {
156
- const allRootUris = await Promise.all(data.recentRoots.map(async element =>
157
- element && await this.workspaceStillExist(element) ? element : undefined));
158
- return allRootUris.filter(notEmpty);
159
- }
160
- return [];
161
- }
162
-
163
- protected async workspaceStillExist(workspaceRootUri: string): Promise<boolean> {
164
- return fs.pathExists(FileUri.fsPath(workspaceRootUri));
165
- }
166
-
167
- protected async getWorkspaceURIFromCli(): Promise<string | undefined> {
168
- const arg = await this.cliParams.workspaceRoot.promise;
169
- return arg !== undefined ? FileUri.create(arg).toString() : undefined;
170
- }
171
-
172
- /**
173
- * Writes the given uri as the most recently used workspace root to the user's home directory.
174
- * @param uri most recently used uri
175
- */
176
- protected async writeToUserHome(data: RecentWorkspacePathsData): Promise<void> {
177
- const file = await this.getUserStoragePath();
178
- await this.writeToFile(file, data);
179
- }
180
-
181
- protected async writeToFile(fsPath: string, data: object): Promise<void> {
182
- if (!await fs.pathExists(fsPath)) {
183
- await fs.mkdirs(path.resolve(fsPath, '..'));
184
- }
185
- await fs.writeJson(fsPath, data);
186
- }
187
-
188
- /**
189
- * Reads the most recently used workspace root from the user's home directory.
190
- */
191
- protected async readRecentWorkspacePathsFromUserHome(): Promise<RecentWorkspacePathsData | undefined> {
192
- const fsPath = await this.getUserStoragePath();
193
- const data = await this.readJsonFromFile(fsPath);
194
- return RecentWorkspacePathsData.create(data);
195
- }
196
-
197
- protected async readJsonFromFile(fsPath: string): Promise<object | undefined> {
198
- if (await fs.pathExists(fsPath)) {
199
- const rawContent = await fs.readFile(fsPath, 'utf-8');
200
- const strippedContent = jsoncparser.stripComments(rawContent);
201
- return jsoncparser.parse(strippedContent);
202
- }
203
- }
204
-
205
- protected async getUserStoragePath(): Promise<string> {
206
- const configDirUri = await this.envServer.getConfigDirUri();
207
- return path.resolve(FileUri.fsPath(configDirUri), 'recentworkspace.json');
208
- }
209
-
210
- /**
211
- * Removes untitled workspaces that are not among the most recently used workspaces.
212
- * Use the `untitledWorkspaceStaleThreshold` to configure when to delete workspaces.
213
- */
214
- protected async removeOldUntitledWorkspaces(): Promise<void> {
215
- const recents = (await this.getRecentWorkspaces()).map(FileUri.fsPath);
216
- const olderUntitledWorkspaces = recents
217
- .slice(this.untitledWorkspaceStaleThreshold)
218
- .filter(workspace => this.untitledWorkspaceService.isUntitledWorkspace(FileUri.create(workspace)));
219
- await Promise.all(olderUntitledWorkspaces.map(workspace => fs.promises.unlink(FileUri.fsPath(workspace)).catch(() => { })));
220
- if (olderUntitledWorkspaces.length > 0) {
221
- await this.writeToUserHome({ recentRoots: await this.getRecentWorkspaces() });
222
- }
223
- }
224
- }
225
-
226
- export interface RecentWorkspacePathsData {
227
- recentRoots: string[];
228
- }
229
-
230
- export namespace RecentWorkspacePathsData {
231
- /**
232
- * Parses `data` as `RecentWorkspacePathsData` but removes any non-string array entry.
233
- *
234
- * Returns undefined if the given `data` does not contain a `recentRoots` array property.
235
- */
236
- export function create(data: unknown): RecentWorkspacePathsData | undefined {
237
- if (typeof data !== 'object' || !data || !Array.isArray((data as RecentWorkspacePathsData).recentRoots)) {
238
- return;
239
- }
240
- return {
241
- recentRoots: (data as RecentWorkspacePathsData).recentRoots.filter(root => typeof root === 'string')
242
- };
243
- }
244
- }
1
+ // *****************************************************************************
2
+ // Copyright (C) 2017 TypeFox and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ import * as path from 'path';
18
+ import * as yargs from '@theia/core/shared/yargs';
19
+ import * as fs from '@theia/core/shared/fs-extra';
20
+ import * as jsoncparser from 'jsonc-parser';
21
+ import { injectable, inject, postConstruct } from '@theia/core/shared/inversify';
22
+ import { FileUri, BackendApplicationContribution } from '@theia/core/lib/node';
23
+ import { CliContribution } from '@theia/core/lib/node/cli';
24
+ import { Deferred } from '@theia/core/lib/common/promise-util';
25
+ import { WorkspaceServer, UntitledWorkspaceService } from '../common';
26
+ import { EnvVariablesServer } from '@theia/core/lib/common/env-variables';
27
+ import URI from '@theia/core/lib/common/uri';
28
+ import { notEmpty } from '@theia/core';
29
+
30
+ @injectable()
31
+ export class WorkspaceCliContribution implements CliContribution {
32
+
33
+ @inject(EnvVariablesServer) protected readonly envVariablesServer: EnvVariablesServer;
34
+ @inject(UntitledWorkspaceService) protected readonly untitledWorkspaceService: UntitledWorkspaceService;
35
+
36
+ workspaceRoot = new Deferred<string | undefined>();
37
+
38
+ configure(conf: yargs.Argv): void {
39
+ conf.usage('$0 [workspace-directories] [options]');
40
+ conf.option('root-dir', {
41
+ description: 'DEPRECATED: Sets the workspace directory.',
42
+ });
43
+ }
44
+
45
+ async setArguments(args: yargs.Arguments): Promise<void> {
46
+ const workspaceArguments = args._.map(probablyAlreadyString => String(probablyAlreadyString));
47
+ if (workspaceArguments.length === 0 && args['root-dir']) {
48
+ workspaceArguments.push(String(args['root-dir']));
49
+ }
50
+ if (workspaceArguments.length === 0) {
51
+ this.workspaceRoot.resolve(undefined);
52
+ } else if (workspaceArguments.length === 1) {
53
+ this.workspaceRoot.resolve(this.normalizeWorkspaceArg(workspaceArguments[0]));
54
+ } else {
55
+ this.workspaceRoot.resolve(this.buildWorkspaceForMultipleArguments(workspaceArguments));
56
+ }
57
+ }
58
+
59
+ protected normalizeWorkspaceArg(raw: string): string {
60
+ return path.resolve(raw).replace(/\/$/, '');
61
+ }
62
+
63
+ protected async buildWorkspaceForMultipleArguments(workspaceArguments: string[]): Promise<string | undefined> {
64
+ try {
65
+ const dirs = await Promise.all(workspaceArguments.map(async maybeDir => (await fs.stat(maybeDir).catch(() => undefined))?.isDirectory()));
66
+ const folders = workspaceArguments.filter((_, index) => dirs[index]).map(dir => ({ path: this.normalizeWorkspaceArg(dir) }));
67
+ if (folders.length < 2) {
68
+ return folders[0]?.path;
69
+ }
70
+ const untitledWorkspaceUri = await this.untitledWorkspaceService.getUntitledWorkspaceUri(
71
+ new URI(await this.envVariablesServer.getConfigDirUri()),
72
+ async uri => !await fs.pathExists(uri.path.fsPath()),
73
+ );
74
+ const untitledWorkspacePath = untitledWorkspaceUri.path.fsPath();
75
+
76
+ await fs.ensureDir(path.dirname(untitledWorkspacePath));
77
+ await fs.writeFile(untitledWorkspacePath, JSON.stringify({ folders }, undefined, 4));
78
+ return untitledWorkspacePath;
79
+ } catch {
80
+ return undefined;
81
+ }
82
+ }
83
+ }
84
+
85
+ @injectable()
86
+ export class DefaultWorkspaceServer implements WorkspaceServer, BackendApplicationContribution {
87
+
88
+ protected root: Deferred<string | undefined> = new Deferred();
89
+ /**
90
+ * Untitled workspaces that are not among the most recent N workspaces will be deleted on start. Increase this number to keep older files,
91
+ * lower it to delete stale untitled workspaces more aggressively.
92
+ */
93
+ protected untitledWorkspaceStaleThreshold = 10;
94
+
95
+ @inject(WorkspaceCliContribution)
96
+ protected readonly cliParams: WorkspaceCliContribution;
97
+
98
+ @inject(EnvVariablesServer)
99
+ protected readonly envServer: EnvVariablesServer;
100
+
101
+ @inject(UntitledWorkspaceService)
102
+ protected readonly untitledWorkspaceService: UntitledWorkspaceService;
103
+
104
+ @postConstruct()
105
+ protected init(): void {
106
+ this.doInit();
107
+ }
108
+
109
+ protected async doInit(): Promise<void> {
110
+ const root = await this.getRoot();
111
+ this.root.resolve(root);
112
+ }
113
+
114
+ async onStart(): Promise<void> {
115
+ await this.removeOldUntitledWorkspaces();
116
+ }
117
+
118
+ protected async getRoot(): Promise<string | undefined> {
119
+ let root = await this.getWorkspaceURIFromCli();
120
+ if (!root) {
121
+ const data = await this.readRecentWorkspacePathsFromUserHome();
122
+ if (data && data.recentRoots) {
123
+ root = data.recentRoots[0];
124
+ }
125
+ }
126
+ return root;
127
+ }
128
+
129
+ getMostRecentlyUsedWorkspace(): Promise<string | undefined> {
130
+ return this.root.promise;
131
+ }
132
+
133
+ async setMostRecentlyUsedWorkspace(rawUri: string): Promise<void> {
134
+ const uri = rawUri && new URI(rawUri).toString(); // the empty string is used as a signal from the frontend not to load a workspace.
135
+ this.root = new Deferred();
136
+ this.root.resolve(uri);
137
+ const recentRoots = Array.from(new Set([uri, ...await this.getRecentWorkspaces()]));
138
+ this.writeToUserHome({ recentRoots });
139
+ }
140
+
141
+ async removeRecentWorkspace(rawUri: string): Promise<void> {
142
+ const uri = rawUri && new URI(rawUri).toString(); // the empty string is used as a signal from the frontend not to load a workspace.
143
+ const recentRoots = await this.getRecentWorkspaces();
144
+ const index = recentRoots.indexOf(uri);
145
+ if (index !== -1) {
146
+ recentRoots.splice(index, 1);
147
+ this.writeToUserHome({
148
+ recentRoots
149
+ });
150
+ }
151
+ }
152
+
153
+ async getRecentWorkspaces(): Promise<string[]> {
154
+ const data = await this.readRecentWorkspacePathsFromUserHome();
155
+ if (data && data.recentRoots) {
156
+ const allRootUris = await Promise.all(data.recentRoots.map(async element =>
157
+ element && await this.workspaceStillExist(element) ? element : undefined));
158
+ return allRootUris.filter(notEmpty);
159
+ }
160
+ return [];
161
+ }
162
+
163
+ protected async workspaceStillExist(workspaceRootUri: string): Promise<boolean> {
164
+ return fs.pathExists(FileUri.fsPath(workspaceRootUri));
165
+ }
166
+
167
+ protected async getWorkspaceURIFromCli(): Promise<string | undefined> {
168
+ const arg = await this.cliParams.workspaceRoot.promise;
169
+ return arg !== undefined ? FileUri.create(arg).toString() : undefined;
170
+ }
171
+
172
+ /**
173
+ * Writes the given uri as the most recently used workspace root to the user's home directory.
174
+ * @param uri most recently used uri
175
+ */
176
+ protected async writeToUserHome(data: RecentWorkspacePathsData): Promise<void> {
177
+ const file = await this.getUserStoragePath();
178
+ await this.writeToFile(file, data);
179
+ }
180
+
181
+ protected async writeToFile(fsPath: string, data: object): Promise<void> {
182
+ if (!await fs.pathExists(fsPath)) {
183
+ await fs.mkdirs(path.resolve(fsPath, '..'));
184
+ }
185
+ await fs.writeJson(fsPath, data);
186
+ }
187
+
188
+ /**
189
+ * Reads the most recently used workspace root from the user's home directory.
190
+ */
191
+ protected async readRecentWorkspacePathsFromUserHome(): Promise<RecentWorkspacePathsData | undefined> {
192
+ const fsPath = await this.getUserStoragePath();
193
+ const data = await this.readJsonFromFile(fsPath);
194
+ return RecentWorkspacePathsData.create(data);
195
+ }
196
+
197
+ protected async readJsonFromFile(fsPath: string): Promise<object | undefined> {
198
+ if (await fs.pathExists(fsPath)) {
199
+ const rawContent = await fs.readFile(fsPath, 'utf-8');
200
+ const strippedContent = jsoncparser.stripComments(rawContent);
201
+ return jsoncparser.parse(strippedContent);
202
+ }
203
+ }
204
+
205
+ protected async getUserStoragePath(): Promise<string> {
206
+ const configDirUri = await this.envServer.getConfigDirUri();
207
+ return path.resolve(FileUri.fsPath(configDirUri), 'recentworkspace.json');
208
+ }
209
+
210
+ /**
211
+ * Removes untitled workspaces that are not among the most recently used workspaces.
212
+ * Use the `untitledWorkspaceStaleThreshold` to configure when to delete workspaces.
213
+ */
214
+ protected async removeOldUntitledWorkspaces(): Promise<void> {
215
+ const recents = (await this.getRecentWorkspaces()).map(FileUri.fsPath);
216
+ const olderUntitledWorkspaces = recents
217
+ .slice(this.untitledWorkspaceStaleThreshold)
218
+ .filter(workspace => this.untitledWorkspaceService.isUntitledWorkspace(FileUri.create(workspace)));
219
+ await Promise.all(olderUntitledWorkspaces.map(workspace => fs.promises.unlink(FileUri.fsPath(workspace)).catch(() => { })));
220
+ if (olderUntitledWorkspaces.length > 0) {
221
+ await this.writeToUserHome({ recentRoots: await this.getRecentWorkspaces() });
222
+ }
223
+ }
224
+ }
225
+
226
+ export interface RecentWorkspacePathsData {
227
+ recentRoots: string[];
228
+ }
229
+
230
+ export namespace RecentWorkspacePathsData {
231
+ /**
232
+ * Parses `data` as `RecentWorkspacePathsData` but removes any non-string array entry.
233
+ *
234
+ * Returns undefined if the given `data` does not contain a `recentRoots` array property.
235
+ */
236
+ export function create(data: unknown): RecentWorkspacePathsData | undefined {
237
+ if (typeof data !== 'object' || !data || !Array.isArray((data as RecentWorkspacePathsData).recentRoots)) {
238
+ return;
239
+ }
240
+ return {
241
+ recentRoots: (data as RecentWorkspacePathsData).recentRoots.filter(root => typeof root === 'string')
242
+ };
243
+ }
244
+ }
package/src/node/index.ts CHANGED
@@ -1,18 +1,18 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2017 TypeFox and others.
3
- //
4
- // This program and the accompanying materials are made available under the
5
- // terms of the Eclipse Public License v. 2.0 which is available at
6
- // http://www.eclipse.org/legal/epl-2.0.
7
- //
8
- // This Source Code may also be made available under the following Secondary
9
- // Licenses when the conditions for such availability set forth in the Eclipse
10
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
- // with the GNU Classpath Exception which is available at
12
- // https://www.gnu.org/software/classpath/license.html.
13
- //
14
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
- // *****************************************************************************
16
-
17
- export * from './default-workspace-server';
18
- export * from './workspace-backend-module';
1
+ // *****************************************************************************
2
+ // Copyright (C) 2017 TypeFox and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ export * from './default-workspace-server';
18
+ export * from './workspace-backend-module';
@@ -1,38 +1,38 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2017 TypeFox and others.
3
- //
4
- // This program and the accompanying materials are made available under the
5
- // terms of the Eclipse Public License v. 2.0 which is available at
6
- // http://www.eclipse.org/legal/epl-2.0.
7
- //
8
- // This Source Code may also be made available under the following Secondary
9
- // Licenses when the conditions for such availability set forth in the Eclipse
10
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
- // with the GNU Classpath Exception which is available at
12
- // https://www.gnu.org/software/classpath/license.html.
13
- //
14
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
- // *****************************************************************************
16
-
17
- import { ContainerModule } from '@theia/core/shared/inversify';
18
- import { ConnectionHandler, RpcConnectionHandler } from '@theia/core/lib/common';
19
- import { WorkspaceServer, workspacePath, UntitledWorkspaceService, WorkspaceFileService } from '../common';
20
- import { DefaultWorkspaceServer, WorkspaceCliContribution } from './default-workspace-server';
21
- import { CliContribution } from '@theia/core/lib/node/cli';
22
- import { BackendApplicationContribution } from '@theia/core/lib/node';
23
-
24
- export default new ContainerModule(bind => {
25
- bind(WorkspaceCliContribution).toSelf().inSingletonScope();
26
- bind(CliContribution).toService(WorkspaceCliContribution);
27
- bind(DefaultWorkspaceServer).toSelf().inSingletonScope();
28
- bind(WorkspaceServer).toService(DefaultWorkspaceServer);
29
- bind(BackendApplicationContribution).toService(WorkspaceServer);
30
- bind(UntitledWorkspaceService).toSelf().inSingletonScope();
31
- bind(WorkspaceFileService).toSelf().inSingletonScope();
32
-
33
- bind(ConnectionHandler).toDynamicValue(ctx =>
34
- new RpcConnectionHandler(workspacePath, () =>
35
- ctx.container.get(WorkspaceServer)
36
- )
37
- ).inSingletonScope();
38
- });
1
+ // *****************************************************************************
2
+ // Copyright (C) 2017 TypeFox and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ import { ContainerModule } from '@theia/core/shared/inversify';
18
+ import { ConnectionHandler, RpcConnectionHandler } from '@theia/core/lib/common';
19
+ import { WorkspaceServer, workspacePath, UntitledWorkspaceService, WorkspaceFileService } from '../common';
20
+ import { DefaultWorkspaceServer, WorkspaceCliContribution } from './default-workspace-server';
21
+ import { CliContribution } from '@theia/core/lib/node/cli';
22
+ import { BackendApplicationContribution } from '@theia/core/lib/node';
23
+
24
+ export default new ContainerModule(bind => {
25
+ bind(WorkspaceCliContribution).toSelf().inSingletonScope();
26
+ bind(CliContribution).toService(WorkspaceCliContribution);
27
+ bind(DefaultWorkspaceServer).toSelf().inSingletonScope();
28
+ bind(WorkspaceServer).toService(DefaultWorkspaceServer);
29
+ bind(BackendApplicationContribution).toService(WorkspaceServer);
30
+ bind(UntitledWorkspaceService).toSelf().inSingletonScope();
31
+ bind(WorkspaceFileService).toSelf().inSingletonScope();
32
+
33
+ bind(ConnectionHandler).toDynamicValue(ctx =>
34
+ new RpcConnectionHandler(workspacePath, () =>
35
+ ctx.container.get(WorkspaceServer)
36
+ )
37
+ ).inSingletonScope();
38
+ });