/
SetupWorkspace.ts
310 lines (288 loc) · 9.17 KB
/
SetupWorkspace.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import {
CONSTANTS,
DENDRON_VSCODE_CONFIG_KEYS,
DVault,
ErrorFactory,
WorkspaceType,
} from "@dendronhq/common-all";
import { resolveTilde } from "@dendronhq/common-server";
import { WorkspaceService, WorkspaceUtils } from "@dendronhq/engine-server";
import fs from "fs-extra";
import _ from "lodash";
import path from "path";
import vscode, { Uri } from "vscode";
import { DENDRON_COMMANDS } from "../constants";
import { ExtensionProvider } from "../ExtensionProvider";
import { Logger } from "../logger";
import { VSCodeUtils } from "../vsCodeUtils";
import { BlankInitializer } from "../workspace/blankInitializer";
import { WorkspaceActivator } from "../workspace/workspaceActivator";
import { WorkspaceInitializer } from "../workspace/workspaceInitializer";
import { BasicCommand } from "./base";
import PathLike = fs.PathLike;
type CommandInput = {
rootDirRaw: string;
workspaceInitializer?: WorkspaceInitializer;
workspaceType?: WorkspaceType;
};
type CommandOpts = CommandInput & {
vault?: DVault;
skipOpenWs?: boolean;
/**
* override prompts
*/
skipConfirmation?: boolean;
/** Create self contained vaults, overriding the Dendron VSCode setting. */
selfContained?: boolean;
/**
* Open worksapce without reloading
*/
// eslint-disable-next-line camelcase
EXPERIMENTAL_openNativeWorkspaceNoReload?: boolean;
};
type CommandOutput = { wsVault?: DVault; additionalVaults?: DVault[] };
export { CommandOpts as SetupWorkspaceOpts };
const CODE_WS_LABEL = "Code Workspace";
const CODE_WS_DETAIL = undefined;
enum EXISTING_ROOT_ACTIONS {
CONTINUE = "Continue",
DELETE = "Delete",
ABORT = "Abort",
}
export class SetupWorkspaceCommand extends BasicCommand<
CommandOpts,
CommandOutput
> {
key = DENDRON_COMMANDS.INIT_WS.key;
async gatherInputs(): Promise<CommandInput | undefined> {
let workspaceType = WorkspaceType.CODE;
const defaultUri = Uri.file(resolveTilde("~"));
let rootDirRaw: string | undefined;
const workspaceFolders = vscode.workspace.workspaceFolders;
if (
workspaceFolders !== undefined &&
workspaceFolders.length > 0 &&
(await WorkspaceUtils.getWorkspaceType({
workspaceFolders,
})) === WorkspaceType.NONE
) {
// If there's a non-Dendron workspace already open, offer to convert that to a Dendron workspace first
const initNative = await VSCodeUtils.showQuickPick(
[
{
picked: true,
label: CODE_WS_LABEL,
description: CODE_WS_DETAIL,
detail: "A dedicated IDE workspace for just your notes",
},
...workspaceFolders.map((folder) => {
const folderName = folder.name || folder.uri.fsPath;
return {
label: "Native Workspace",
description: folder.uri.fsPath,
detail: `Take notes in "${folderName}" alongside your existing project`,
};
}),
],
{
ignoreFocusOut: true,
title: "Workspace type to initialize",
}
);
if (initNative === undefined) return;
if (
initNative.label !== CODE_WS_LABEL ||
initNative.description !== CODE_WS_DETAIL
) {
// Not sure if there's a better way to check for this, but this is if a native workspace option was selected
workspaceType = WorkspaceType.NATIVE;
rootDirRaw = await VSCodeUtils.gatherFolderPath({
default: "docs",
relativeTo: initNative.description,
override: {
title: "Path for Dendron Native Workspace",
prompt: `Path to folder, relative to ${initNative.label}`,
},
});
}
// If the code workspace option is selected, then we continue with `rootDirRaw` unset and type still set to `CODE`
}
if (!rootDirRaw) {
// Prompt user where to create workspace
const options: vscode.OpenDialogOptions = {
canSelectMany: false,
openLabel: "Create Workspace",
canSelectFiles: false,
canSelectFolders: true,
defaultUri,
};
rootDirRaw = await VSCodeUtils.openFilePicker(options);
if (_.isUndefined(rootDirRaw)) {
return;
}
}
return {
rootDirRaw,
workspaceType,
workspaceInitializer: new BlankInitializer(),
};
}
handleExistingRoot = async ({
rootDir,
skipConfirmation,
}: {
rootDir: string;
skipConfirmation?: boolean;
}): Promise<boolean> => {
if (!this.isEmptyDirectory(rootDir) && !skipConfirmation) {
const resp = await VSCodeUtils.showQuickPick(
[
{
label: EXISTING_ROOT_ACTIONS.CONTINUE,
detail: `Continue creating a workspace, putting Dendron files into the existing folder.`,
},
{
label: EXISTING_ROOT_ACTIONS.DELETE,
detail: "Delete this folder and continue creating a workspace.",
},
{
label: EXISTING_ROOT_ACTIONS.ABORT,
detail: "Abort creating a workspace.",
},
],
{
title: `${rootDir} already exists, how do you want to continue?`,
ignoreFocusOut: true,
canPickMany: false,
}
);
if (resp === undefined || resp.label === EXISTING_ROOT_ACTIONS.ABORT) {
vscode.window.showInformationMessage(
"did not initialize dendron workspace"
);
return false;
}
if (resp.label === EXISTING_ROOT_ACTIONS.DELETE) {
try {
fs.removeSync(rootDir);
return true;
} catch (err) {
this.L.error(JSON.stringify(err));
vscode.window.showErrorMessage(
`error removing ${rootDir}. please check that it's not currently open`
);
return false;
}
}
return true;
}
return true;
};
addAnalyticsPayload(opts?: CommandOpts) {
return {
workspaceType: opts?.workspaceType,
};
}
async execute(
opts: CommandOpts
): Promise<{ wsVault?: DVault; additionalVaults?: DVault[] }> {
const ctx = "SetupWorkspaceCommand";
// This command can run before the extension is registered, especially during testing
const defaultSelfContained =
VSCodeUtils.getWorkspaceConfig().get<boolean>(
DENDRON_VSCODE_CONFIG_KEYS.ENABLE_SELF_CONTAINED_VAULTS_WORKSPACE
) ?? false;
const {
rootDirRaw: rootDir,
skipOpenWs,
workspaceType,
selfContained,
} = _.defaults(opts, {
selfContained: defaultSelfContained,
});
Logger.info({ ctx, rootDir, skipOpenWs, workspaceType });
if (
!(await this.handleExistingRoot({
rootDir,
skipConfirmation: opts.skipConfirmation,
}))
) {
return {};
}
let wsVault: DVault | undefined;
let additionalVaults: DVault[] | undefined;
if (opts?.workspaceInitializer?.createVaults) {
({ wsVault, additionalVaults } = opts.workspaceInitializer.createVaults(
opts.vault
));
}
// Default to CODE workspace, otherwise create a NATIVE one
const createCodeWorkspace =
workspaceType === WorkspaceType.CODE || workspaceType === undefined;
const svc = await WorkspaceService.createWorkspace({
wsVault,
additionalVaults,
wsRoot: rootDir,
createCodeWorkspace,
useSelfContainedVault: selfContained,
});
Logger.info({
ctx: `${ctx}:postCreateWorkspace`,
wsRoot: rootDir,
wsVault,
});
if (opts?.workspaceInitializer?.onWorkspaceCreation) {
await opts.workspaceInitializer.onWorkspaceCreation({
wsVault,
additionalVaults,
wsRoot: rootDir,
svc,
});
}
if (!skipOpenWs) {
vscode.window.showInformationMessage("opening dendron workspace");
if (workspaceType === WorkspaceType.CODE) {
VSCodeUtils.openWS(
vscode.Uri.file(path.join(rootDir, CONSTANTS.DENDRON_WS_NAME)).fsPath
);
} else if (workspaceType === WorkspaceType.NATIVE) {
if (opts.EXPERIMENTAL_openNativeWorkspaceNoReload) {
const ext = ExtensionProvider.getExtension();
const { context } = ext;
ext.type = WorkspaceType.NATIVE;
const wsa = new WorkspaceActivator();
const resp = await wsa.init({
context,
ext,
wsRoot: rootDir,
});
if (resp.error) {
throw ErrorFactory.createInvalidStateError({
message: "issure init workspace",
});
}
await wsa.activate({
context,
ext,
wsRoot: rootDir,
engine: resp.data.engine,
wsService: resp.data.wsService,
workspaceInitializer: opts.workspaceInitializer,
});
} else {
// For native workspaces, we just need to reload the existing workspace because we want to keep the same workspace.
VSCodeUtils.reloadWindow();
}
}
}
return { wsVault, additionalVaults };
}
/**
* Tests whether or not the given directory is empty.
*/
private isEmptyDirectory(path: PathLike) {
if (!fs.existsSync(path)) return true;
const files = fs.readdirSync(path);
return !files || !files.length;
}
}