Skip to content

Commit

Permalink
Open existing project, validation
Browse files Browse the repository at this point in the history
  • Loading branch information
khawkins committed Jun 12, 2023
1 parent 94844a0 commit c960fac
Showing 1 changed file with 109 additions and 10 deletions.
119 changes: 109 additions & 10 deletions src/commands/configureProjectCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/

import { Uri, WebviewPanel, commands, window } from 'vscode';
import * as process from 'process';
import { CommonUtils } from '@salesforce/lwc-dev-mobile-core/lib/common/CommonUtils';
import { InstructionsWebviewProvider } from '../webviews';

export class ConfigureProjectCommand {
Expand Down Expand Up @@ -43,8 +45,10 @@ export class ConfigureProjectCommand {
{
buttonId: 'openExistingButton',
action: async (panel) => {
const path = await this.openExistingProject(panel);
return resolve(path);
// See above for rationale for running this async.
this.openExistingProject(panel).then((path) => {
return resolve(path);
});
}
}
]
Expand Down Expand Up @@ -77,7 +81,7 @@ export class ConfigureProjectCommand {
panel.dispose();
try {
const path = await this.executeProjectCreation(
folderUri
folderUri[0]
);
return resolve(path);
} catch (error) {
Expand All @@ -90,27 +94,122 @@ export class ConfigureProjectCommand {
});
}

private async openExistingProject(
panel: WebviewPanel
): Promise<string | undefined> {
return new Promise((resolve, reject) => {});
private async openExistingProject(panel: WebviewPanel): Promise<string> {
return new Promise(async (resolve) => {
const folderUri = await window.showOpenDialog({
openLabel: 'Select project folder',
canSelectFolders: true,
canSelectFiles: false,
canSelectMany: false
});
if (!folderUri || folderUri.length === 0) {
return;
}

try {
await this.validateProjectFolder(folderUri[0]);
} catch (error) {
window.showErrorMessage((error as Error).message);
return;
}

panel.dispose();
new InstructionsWebviewProvider(
this.extensionUri
).showInstructionWebview(
'Offline Starter Kit: Follow the Prompts',
'src/instructions/projectBootstrapAcknowledgment.html',
[
{
buttonId: 'okButton',
action: async (panel) => {
panel.dispose();
await commands.executeCommand(
'vscode.openFolder',
folderUri[0],
{ forceReuseWindow: true }
);
return resolve(folderUri[0].fsPath);
}
}
]
);
});
}

private async executeProjectCreation(folderUri: Uri[]): Promise<string> {
private async executeProjectCreation(folderUri: Uri): Promise<string> {
return new Promise(async (resolve, reject) => {
const githubRepoUri: string =
'https://github.com/salesforce/offline-app-developer-starter-kit.git';
try {
await commands.executeCommand(
'git.clone',
githubRepoUri,
folderUri[0].fsPath
folderUri.fsPath
);
return resolve(folderUri[0].fsPath);
return resolve(folderUri.fsPath);
} catch (error) {
window.showErrorMessage(`Failed to clone: ${error}`);
return reject(error);
}
});
}

private 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(
`Could not access the project folder at '${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('git is not installed.'));
}

// Is this a git repo?
try {
await CommonUtils.executeCommandAsync('git status');
} catch (error) {
return reject(
new Error(
`Folder '${projectFolderUri.fsPath}' does not contain a git repository.`
)
);
}

// Is this the Offline Starter Kit repo?
try {
const oskInitialCommit =
'99b1fa9377694beb7918580aab445a2e9981f611';
await CommonUtils.executeCommandAsync(
`git merge-base HEAD ${oskInitialCommit}`
);
} catch (error) {
return reject(
new Error(
'This git repository does not share history with the Offline Starter Kit.'
)
);
}

return resolve();
} finally {
process.chdir(origWorkingDir);
}
});
}
}

0 comments on commit c960fac

Please sign in to comment.