-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
default-workspace-server.ts
179 lines (156 loc) · 6.16 KB
/
default-workspace-server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/********************************************************************************
* Copyright (C) 2017 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import * as path from 'path';
import * as yargs from 'yargs';
import * as fs from 'fs-extra';
import * as os from 'os';
import * as jsoncparser from 'jsonc-parser';
import { injectable, inject, postConstruct } from 'inversify';
import { FileUri } from '@theia/core/lib/node';
import { CliContribution } from '@theia/core/lib/node/cli';
import { Deferred } from '@theia/core/lib/common/promise-util';
import { WorkspaceServer } from '../common';
@injectable()
export class WorkspaceCliContribution implements CliContribution {
workspaceRoot = new Deferred<string | undefined>();
configure(conf: yargs.Argv): void {
conf.usage('$0 [workspace-directory] [options]');
conf.option('root-dir', {
description: 'DEPRECATED: Sets the workspace directory.',
});
}
setArguments(args: yargs.Arguments): void {
let wsPath = args._[2];
if (!wsPath) {
wsPath = args['root-dir'];
if (!wsPath) {
this.workspaceRoot.resolve();
return;
}
}
if (!path.isAbsolute(wsPath)) {
const cwd = process.cwd();
wsPath = path.join(cwd, wsPath);
}
if (wsPath && wsPath.endsWith('/')) {
wsPath = wsPath.slice(0, -1);
}
this.workspaceRoot.resolve(wsPath);
}
}
@injectable()
export class DefaultWorkspaceServer implements WorkspaceServer {
protected root: Deferred<string | undefined> = new Deferred();
@inject(WorkspaceCliContribution)
protected readonly cliParams: WorkspaceCliContribution;
@postConstruct()
protected async init(): Promise<void> {
const root = await this.getRoot();
this.root.resolve(root);
}
protected async getRoot(): Promise<string | undefined> {
let root = await this.getWorkspaceURIFromCli();
if (!root) {
const data = await this.readRecentWorkspacePathsFromUserHome();
if (data && data.recentRoots) {
root = data.recentRoots[0];
}
}
return root;
}
getMostRecentlyUsedWorkspace(): Promise<string | undefined> {
return this.root.promise;
}
async setMostRecentlyUsedWorkspace(uri: string): Promise<void> {
this.root = new Deferred();
const listUri: string[] = [];
const oldListUri = await this.getRecentWorkspaces();
listUri.push(uri);
if (oldListUri) {
oldListUri.forEach(element => {
if (element !== uri && element.length > 0) {
listUri.push(element);
}
});
}
this.root.resolve(uri);
this.writeToUserHome({
recentRoots: listUri
});
}
async getRecentWorkspaces(): Promise<string[]> {
const listUri: string[] = [];
const data = await this.readRecentWorkspacePathsFromUserHome();
if (data && data.recentRoots) {
data.recentRoots.forEach(element => {
if (element.length > 0) {
if (this.workspaceStillExist(element)) {
listUri.push(element);
}
}
});
}
return listUri;
}
protected workspaceStillExist(wspath: string): boolean {
return fs.pathExistsSync(FileUri.fsPath(wspath));
}
protected async getWorkspaceURIFromCli(): Promise<string | undefined> {
const arg = await this.cliParams.workspaceRoot.promise;
return arg !== undefined ? FileUri.create(arg).toString() : undefined;
}
/**
* Writes the given uri as the most recently used workspace root to the user's home directory.
* @param uri most recently used uri
*/
protected async writeToUserHome(data: RecentWorkspacePathsData): Promise<void> {
const file = this.getUserStoragePath();
await this.writeToFile(file, data);
}
protected async writeToFile(filePath: string, data: object): Promise<void> {
if (!await fs.pathExists(filePath)) {
await fs.mkdirs(path.resolve(filePath, '..'));
}
await fs.writeJson(filePath, data);
}
/**
* Reads the most recently used workspace root from the user's home directory.
*/
protected async readRecentWorkspacePathsFromUserHome(): Promise<RecentWorkspacePathsData | undefined> {
const filePath = this.getUserStoragePath();
const data = await this.readJsonFromFile(filePath);
return RecentWorkspacePathsData.is(data) ? data : undefined;
}
protected async readJsonFromFile(filePath: string): Promise<object | undefined> {
if (await fs.pathExists(filePath)) {
const rawContent = await fs.readFile(filePath, 'utf-8');
const strippedContent = jsoncparser.stripComments(rawContent);
return jsoncparser.parse(strippedContent);
}
}
protected getUserStoragePath(): string {
return path.resolve(os.homedir(), '.theia', 'recentworkspace.json');
}
}
interface RecentWorkspacePathsData {
recentRoots: string[];
}
namespace RecentWorkspacePathsData {
export function is(data: Object | undefined): data is RecentWorkspacePathsData {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return !!data && typeof data === 'object' && ('recentRoots' in data) && Array.isArray((data as any)['recentRoots']);
}
}