@theia/workspace 1.34.3 → 1.34.4

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