|
| 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 { getLogger } from '../../../shared/logger/logger' |
| 11 | +import { checklogs } from '../../../shared/localizedText' |
| 12 | +import { telemetry } from '../../../shared/telemetry/telemetry' |
| 13 | +import { CreateServerlessLandWizardForm, CreateServerlessLandWizard } from './wizard' |
| 14 | +import { ExtContext } from '../../../shared/extensions' |
| 15 | +import { addFolderToWorkspace } from '../../../shared/utilities/workspaceUtils' |
| 16 | +import { ToolkitError } from '../../../shared/errors' |
| 17 | +import { fs } from '../../../shared/fs/fs' |
| 18 | +import { getPattern } from '../../../shared/utilities/downloadPatterns' |
| 19 | +import { MetadataManager } from './metadataManager' |
| 20 | + |
| 21 | +export const readmeFile: string = 'README.md' |
| 22 | +const serverlessLandOwner = 'aws-samples' |
| 23 | +const serverlessLandRepo = 'serverless-patterns' |
| 24 | + |
| 25 | +/** |
| 26 | + * Creates a new Serverless Land project using the provided extension context |
| 27 | + * @param extContext Extension context containing AWS credentials and region information |
| 28 | + * @returns Promise that resolves when the project creation is complete |
| 29 | + * |
| 30 | + * This function: |
| 31 | + * 1. Validates AWS credentials and regions |
| 32 | + * 2. Launches the Serverless Land project creation wizard |
| 33 | + * 3. Creates the project structure |
| 34 | + * 4. Adds the project folder to the workspace |
| 35 | + * 5. Opens the README.md file if available |
| 36 | + * 6. Handles errors and emits telemetry |
| 37 | + */ |
| 38 | +export async function createNewServerlessLandProject(extContext: ExtContext): Promise<void> { |
| 39 | + let metadataManager: MetadataManager |
| 40 | + |
| 41 | + try { |
| 42 | + metadataManager = MetadataManager.getInstance() |
| 43 | + // Launch the project creation wizard |
| 44 | + const config = await launchProjectCreationWizard(extContext) |
| 45 | + if (!config) { |
| 46 | + return |
| 47 | + } |
| 48 | + const assetName = metadataManager.getAssetName(config.pattern, config.runtime, config.iac) |
| 49 | + |
| 50 | + await downloadPatternCode(config, assetName) |
| 51 | + await openReadmeFile(config) |
| 52 | + await addFolderToWorkspace( |
| 53 | + { |
| 54 | + uri: vscode.Uri.joinPath(config.location, config.name), |
| 55 | + name: path.basename(config.name), |
| 56 | + }, |
| 57 | + true |
| 58 | + ) |
| 59 | + telemetry.record({ |
| 60 | + templateName: assetName, |
| 61 | + runtimeString: config.runtime, |
| 62 | + iac: config.iac, |
| 63 | + }) |
| 64 | + } catch (err) { |
| 65 | + getLogger().error( |
| 66 | + localize( |
| 67 | + 'AWS.serverlessland.initWizard.general.error', |
| 68 | + 'Error creating new Serverless Land Application. {0}', |
| 69 | + checklogs() |
| 70 | + ) |
| 71 | + ) |
| 72 | + getLogger().error('Error creating new Serverless Land Application: %O', err as Error) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +export async function launchProjectCreationWizard( |
| 77 | + extContext: ExtContext |
| 78 | +): Promise<CreateServerlessLandWizardForm | undefined> { |
| 79 | + const awsContext = extContext.awsContext |
| 80 | + const credentials = await awsContext.getCredentials() |
| 81 | + const defaultRegion = awsContext.getCredentialDefaultRegion() |
| 82 | + |
| 83 | + return new CreateServerlessLandWizard({ |
| 84 | + credentials, |
| 85 | + defaultRegion, |
| 86 | + }).run() |
| 87 | +} |
| 88 | + |
| 89 | +export async function downloadPatternCode(config: CreateServerlessLandWizardForm, assetName: string): Promise<void> { |
| 90 | + const fullAssetName = assetName + '.zip' |
| 91 | + const location = vscode.Uri.joinPath(config.location, config.name) |
| 92 | + try { |
| 93 | + await getPattern(serverlessLandOwner, serverlessLandRepo, fullAssetName, location, true) |
| 94 | + } catch (error) { |
| 95 | + if (error instanceof ToolkitError) { |
| 96 | + throw error |
| 97 | + } |
| 98 | + throw new ToolkitError(`Failed to download pattern: ${error}`) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +export async function openReadmeFile(config: CreateServerlessLandWizardForm): Promise<void> { |
| 103 | + try { |
| 104 | + const readmeUri = await getProjectUri(config, readmeFile) |
| 105 | + if (!readmeUri) { |
| 106 | + getLogger().warn('README.md file not found in the project directory') |
| 107 | + return |
| 108 | + } |
| 109 | + await vscode.commands.executeCommand('workbench.action.focusFirstEditorGroup') |
| 110 | + await vscode.commands.executeCommand('markdown.showPreview', readmeUri) |
| 111 | + } catch (err) { |
| 112 | + getLogger().error(`Error in openReadmeFile: ${err}`) |
| 113 | + throw new ToolkitError('Error processing README file') |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +export async function getProjectUri( |
| 118 | + config: Pick<CreateServerlessLandWizardForm, 'location' | 'name'>, |
| 119 | + file: string |
| 120 | +): Promise<vscode.Uri | undefined> { |
| 121 | + if (!file) { |
| 122 | + throw new ToolkitError('expected "file" parameter to have at least one item') |
| 123 | + } |
| 124 | + const cfnTemplatePath = path.resolve(config.location.fsPath, config.name, file) |
| 125 | + if (await fs.exists(cfnTemplatePath)) { |
| 126 | + return vscode.Uri.file(cfnTemplatePath) |
| 127 | + } |
| 128 | + void vscode.window.showWarningMessage( |
| 129 | + localize( |
| 130 | + 'AWS.serverlessLand.initWizard.source.error.notFound', |
| 131 | + 'Project created successfully, but {0} file not found: {1}', |
| 132 | + file!, |
| 133 | + cfnTemplatePath! |
| 134 | + ) |
| 135 | + ) |
| 136 | +} |
0 commit comments