|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import * as nls from 'vscode-nls' |
| 7 | +const localize = nls.loadMessageBundle() |
| 8 | +import * as path from 'path' |
| 9 | +import * as vscode from 'vscode' |
| 10 | +import { getTelemetryResult, RegionProvider, ToolkitError } from '../../shared' |
| 11 | +import { getLogger } from '../../shared/logger' |
| 12 | +import { fileExists } from '../../shared/filesystemUtilities' |
| 13 | +import { CreateServerlessLandWizardForm } from '../appBuilder/wizards/serverlessLandWizard' |
| 14 | +import { Result, telemetry } from '../../shared/telemetry/telemetry' |
| 15 | +import { CreateServerlessLandWizard } from '../appBuilder/wizards/serverlessLandWizard' |
| 16 | +import { ExtContext } from '../../shared/extensions' |
| 17 | +import { addFolderToWorkspace } from '../../shared/utilities/workspaceUtils' |
| 18 | +import { getPattern } from '../../shared/utilities/downloadPatterns' |
| 19 | + |
| 20 | +export const readmeFile: string = 'README.md' |
| 21 | +const serverlessLandOwner = 'aws-samples' |
| 22 | +const serverlessLandRepo = 'serverless-patterns' |
| 23 | + |
| 24 | +/** |
| 25 | + * Creates a new Serverless Land project using the provided extension context |
| 26 | + * @param extContext Extension context containing AWS credentials and region information |
| 27 | + * @returns Promise that resolves when the project creation is complete |
| 28 | + * |
| 29 | + * This function: |
| 30 | + * 1. Validates AWS credentials and regions |
| 31 | + * 2. Launches the Serverless Land project creation wizard |
| 32 | + * 3. Creates the project structure |
| 33 | + * 4. Adds the project folder to the workspace |
| 34 | + * 5. Opens the README.md file if available |
| 35 | + * 6. Handles errors and emits telemetry |
| 36 | + */ |
| 37 | +export async function createNewServerlessLandProject(extContext: ExtContext): Promise<void> { |
| 38 | + let createResult: Result = 'Succeeded' |
| 39 | + let reason: string | undefined |
| 40 | + |
| 41 | + try { |
| 42 | + // Launch the project creation wizard |
| 43 | + const config = await launchProjectCreationWizard(extContext) |
| 44 | + if (!config) { |
| 45 | + createResult = 'Cancelled' |
| 46 | + reason = 'userCancelled' |
| 47 | + return |
| 48 | + } |
| 49 | + await downloadPatternCode(config) |
| 50 | + await openReadmeFile(config) |
| 51 | + await addFolderToWorkspace( |
| 52 | + { |
| 53 | + uri: vscode.Uri.joinPath(config.location, config.name), |
| 54 | + name: path.basename(config.name), |
| 55 | + }, |
| 56 | + true |
| 57 | + ) |
| 58 | + } catch (err) { |
| 59 | + createResult = getTelemetryResult(err) |
| 60 | + reason = getTelemetryResult(err) |
| 61 | + throw new ToolkitError('Error creating new ServerlessLand Application') |
| 62 | + } finally { |
| 63 | + // add telemetry |
| 64 | + telemetry.sam_init.emit({ |
| 65 | + result: createResult, |
| 66 | + reason: reason, |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +async function launchProjectCreationWizard( |
| 72 | + extContext: ExtContext |
| 73 | +): Promise<CreateServerlessLandWizardForm | undefined> { |
| 74 | + const awsContext = extContext.awsContext |
| 75 | + const regionProvider: RegionProvider = extContext.regionProvider |
| 76 | + const credentials = await awsContext.getCredentials() |
| 77 | + const schemaRegions = regionProvider.getRegions().filter((r) => regionProvider.isServiceInRegion('schemas', r.id)) |
| 78 | + const defaultRegion = awsContext.getCredentialDefaultRegion() |
| 79 | + |
| 80 | + return new CreateServerlessLandWizard({ |
| 81 | + credentials, |
| 82 | + schemaRegions, |
| 83 | + defaultRegion, |
| 84 | + }).run() |
| 85 | +} |
| 86 | + |
| 87 | +async function downloadPatternCode(config: CreateServerlessLandWizardForm): Promise<void> { |
| 88 | + const assetName = config.assetName + '.zip' |
| 89 | + const location = vscode.Uri.joinPath(config.location, config.name) |
| 90 | + try { |
| 91 | + await getPattern(serverlessLandOwner, serverlessLandRepo, assetName, location, true) |
| 92 | + } catch (error) { |
| 93 | + if (error instanceof ToolkitError) { |
| 94 | + throw error |
| 95 | + } |
| 96 | + throw new ToolkitError(`Failed to download pattern: ${error}`) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +async function openReadmeFile(config: CreateServerlessLandWizardForm): Promise<void> { |
| 101 | + try { |
| 102 | + const projectUri = await getProjectUri(config, readmeFile) |
| 103 | + if (!projectUri) { |
| 104 | + getLogger().warn('Project URI not found when trying to open README') |
| 105 | + return |
| 106 | + } |
| 107 | + |
| 108 | + const readmeUri = vscode.Uri.file(path.join(path.dirname(projectUri.fsPath), readmeFile)) |
| 109 | + if (!(await fileExists(readmeUri.fsPath))) { |
| 110 | + getLogger().warn( |
| 111 | + localize('AWS.serverlessLand.readme.notFound', 'README.md file not found in the project directory') |
| 112 | + ) |
| 113 | + return |
| 114 | + } |
| 115 | + |
| 116 | + try { |
| 117 | + const document = await vscode.workspace.openTextDocument(readmeUri) |
| 118 | + await vscode.window.showTextDocument(document, { preview: true }) |
| 119 | + } catch (err) { |
| 120 | + getLogger().error(`Failed to open README file: ${err}`) |
| 121 | + throw new ToolkitError('Failed to open README file') |
| 122 | + } |
| 123 | + } catch (err) { |
| 124 | + getLogger().error(`Error in openReadmeFile: ${err}`) |
| 125 | + throw new ToolkitError('Error processing README file') |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +async function getProjectUri( |
| 130 | + config: Pick<CreateServerlessLandWizardForm, 'location' | 'name'>, |
| 131 | + file: string |
| 132 | +): Promise<vscode.Uri | undefined> { |
| 133 | + if (!file) { |
| 134 | + throw Error('expected "file" parameter to have at least one item') |
| 135 | + } |
| 136 | + const cfnTemplatePath = path.resolve(config.location.fsPath, config.name, file) |
| 137 | + if (await fileExists(cfnTemplatePath)) { |
| 138 | + return vscode.Uri.file(cfnTemplatePath) |
| 139 | + } |
| 140 | + void vscode.window.showWarningMessage( |
| 141 | + localize( |
| 142 | + 'AWS.serverlessLand.initWizard.source.error.notFound', |
| 143 | + 'Project created successfully, but {0} file not found: {1}', |
| 144 | + file!, |
| 145 | + cfnTemplatePath! |
| 146 | + ) |
| 147 | + ) |
| 148 | +} |
0 commit comments