forked from salesforce/salesforcedx-vscode-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigureProjectCommand.ts
More file actions
318 lines (288 loc) · 11.5 KB
/
configureProjectCommand.ts
File metadata and controls
318 lines (288 loc) · 11.5 KB
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
311
312
313
314
315
316
317
318
/*
* Copyright (c) 2023, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import { Uri, WebviewPanel, commands, l10n, window } from 'vscode';
import * as process from 'process';
import { CommonUtils } from '@salesforce/lwc-dev-mobile-core/lib/common/CommonUtils';
import { InstructionsWebviewProvider } from '../../webviews/instructions';
import { wizardCommand } from './onboardingWizard';
import { CoreExtensionService } from '../../services/CoreExtensionService';
export type ProjectManagementChoiceAction = (panel?: WebviewPanel) => void;
export interface ProjectConfigurationProcessor {
getProjectManagementChoice(
createChoice: ProjectManagementChoiceAction,
openChoice: ProjectManagementChoiceAction
): void;
getProjectFolderPath(): Promise<Uri[] | undefined>;
preActionUserAcknowledgment(): Promise<void>;
executeProjectCreation(folderUri: Uri): Promise<string>;
executeProjectOpen(folderUri: Uri): Promise<void>;
}
export class DefaultProjectConfigurationProcessor
implements ProjectConfigurationProcessor
{
extensionUri: Uri;
constructor(extensionUri: Uri) {
this.extensionUri = extensionUri;
}
async preActionUserAcknowledgment(): Promise<void> {
return new Promise((resolve) => {
new InstructionsWebviewProvider(
this.extensionUri
).showInstructionWebview(
l10n.t('Offline Starter Kit: Configure Your Project'),
'resources/instructions/projectBootstrapAcknowledgment.html',
[
{
type: 'okButton',
action: async (panel) => {
panel.dispose();
return resolve();
}
}
]
);
});
}
async executeProjectCreation(folderUri: Uri): Promise<string> {
return new Promise(async (resolve) => {
await commands.executeCommand(
'git.clone',
ConfigureProjectCommand.STARTER_KIT_REPO_URI,
folderUri.fsPath
);
return resolve(folderUri.fsPath);
});
}
async executeProjectOpen(folderUri: Uri): Promise<void> {
return new Promise(async (resolve) => {
await commands.executeCommand('vscode.openFolder', folderUri, {
forceReuseWindow: true
});
return resolve();
});
}
async getProjectFolderPath(): Promise<Uri[] | undefined> {
return new Promise((resolve) => {
window
.showOpenDialog({
openLabel: l10n.t('Select project folder'),
canSelectFolders: true,
canSelectFiles: false,
canSelectMany: false
})
.then((result) => {
return resolve(result);
});
});
}
getProjectManagementChoice(
createChoice: ProjectManagementChoiceAction,
openChoice: ProjectManagementChoiceAction
): void {
new InstructionsWebviewProvider(
this.extensionUri
).showInstructionWebview(
l10n.t('Offline Starter Kit: Create or Open Project'),
'resources/instructions/projectBootstrapChoice.html',
[
{
type: 'createNewButton',
action: (panel) => {
createChoice(panel);
}
},
{
type: 'openExistingButton',
action: (panel) => {
openChoice(panel);
}
}
]
);
}
}
export class ConfigureProjectCommand {
static readonly STARTER_KIT_INITIAL_COMMIT =
'99b1fa9377694beb7918580aab445a2e9981f611';
static readonly STARTER_KIT_REPO_URI =
'https://github.com/salesforce/offline-app-developer-starter-kit.git';
extensionUri: Uri;
projectConfigurationProcessor: ProjectConfigurationProcessor;
constructor(
extensionUri: Uri,
projectConfigurationProcessor?: ProjectConfigurationProcessor
) {
this.extensionUri = extensionUri;
this.projectConfigurationProcessor =
projectConfigurationProcessor ??
new DefaultProjectConfigurationProcessor(extensionUri);
}
async createProjectAction(
panel?: WebviewPanel
): Promise<string | undefined> {
return new Promise((resolve) => {
// It's actually important to run this async, because
// createNewProject() will not resolve its Promise
// until a path is selected, allowing the user to
// cancel the open dialog and re-initiate it as many
// times as they want.
this.createNewProject(panel).then((path) => {
return resolve(path);
});
});
}
async openProjectAction(panel?: WebviewPanel): Promise<string | undefined> {
return new Promise((resolve) => {
// It's actually important to run this async, because
// createNewProject() will not resolve its Promise
// until a path is selected, allowing the user to
// cancel the open dialog and re-initiate it as many
// times as they want.
this.openExistingProject(panel).then((path) => {
return resolve(path);
});
});
}
async configureProject(): Promise<string | undefined> {
const telemetryService = CoreExtensionService.getTelemetryService();
// Send marker to record that the command got executed.
telemetryService.sendCommandEvent(wizardCommand, process.hrtime());
return new Promise((resolve) => {
this.projectConfigurationProcessor.getProjectManagementChoice(
async (panel) => {
const path = await this.createProjectAction(panel);
return resolve(path);
},
async (panel) => {
const path = await this.openProjectAction(panel);
return resolve(path);
}
);
});
}
async createNewProject(panel?: WebviewPanel): Promise<string> {
return new Promise(async (resolve, reject) => {
const folderUri =
await this.projectConfigurationProcessor.getProjectFolderPath();
if (!folderUri || folderUri.length === 0) {
// We explicitly do not want to resolve the Promise here, since the
// user "canceled", but could retry with the action request dialog
// that's still open. Only resolve the Promise when the user makes
// a choice.
return;
}
if (panel) {
panel.dispose();
}
this.projectConfigurationProcessor
.preActionUserAcknowledgment()
.then(async () => {
try {
const path =
await this.projectConfigurationProcessor.executeProjectCreation(
folderUri[0]
);
return resolve(path);
} catch (error) {
return reject(error);
}
});
});
}
async openExistingProject(panel?: WebviewPanel): Promise<string> {
return new Promise(async (resolve) => {
const folderUri =
await this.projectConfigurationProcessor.getProjectFolderPath();
if (!folderUri || folderUri.length === 0) {
// We explicitly do not want to resolve the Promise here, since the
// user "canceled", but could retry with the action request dialog
// that's still open. Only resolve the Promise when the user makes
// a choice.
return;
}
try {
await this.validateProjectFolder(folderUri[0]);
} catch (error) {
window.showErrorMessage((error as Error).message);
// Same as above. If they chose an invalid folder, "soft"-error
// and allow them to pick a different choice.
return;
}
if (panel) {
panel.dispose();
}
this.projectConfigurationProcessor
.preActionUserAcknowledgment()
.then(async () => {
await this.projectConfigurationProcessor.executeProjectOpen(
folderUri[0]
);
return resolve(folderUri[0].fsPath);
});
});
}
async validateProjectFolder(projectFolderUri: Uri): Promise<void> {
return new Promise(async (resolve, reject) => {
const origWorkingDir = process.cwd();
try {
// Can we chdir to the selected folder?
try {
process.chdir(projectFolderUri.fsPath);
} catch (error) {
return reject(
new Error(
l10n.t(
"Could not access the project folder at '{0}'.",
projectFolderUri.fsPath
)
)
);
}
// Is git installed?
try {
// TODO: There are a number of complexities to solving
// for this in the general platform case.
// Cf. https://github.com/microsoft/vscode/blob/89ec834df20d597ff96f7d303e7e0f2f055d2a4e/extensions/git/src/git.ts#L145-L165
await CommonUtils.executeCommandAsync('git --version');
} catch (error) {
return reject(new Error(l10n.t('git is not installed.')));
}
// Is this a git repo?
try {
await CommonUtils.executeCommandAsync('git status');
} catch (error) {
return reject(
new Error(
l10n.t(
"Folder '{0}' does not contain a git repository.",
projectFolderUri.fsPath
)
)
);
}
// Is this the Offline Starter Kit repo?
try {
await CommonUtils.executeCommandAsync(
`git merge-base HEAD ${ConfigureProjectCommand.STARTER_KIT_INITIAL_COMMIT}`
);
} catch (error) {
return reject(
new Error(
l10n.t(
"The git repository at '{0}' does not share history with the Offline Starter Kit.",
projectFolderUri.fsPath
)
)
);
}
return resolve();
} finally {
process.chdir(origWorkingDir);
}
});
}
}