|
| 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 } from '../../shared' |
| 11 | +import { getLogger } from '../../shared/logger' |
| 12 | +import globals from '../../shared/extensionGlobals' |
| 13 | +import { checklogs } from '../../shared/localizedText' |
| 14 | +// import { fileExists } from '../../shared/filesystemUtilities' |
| 15 | +// import { CreateServerlessLandWizardForm } from '../appBuilder/wizards/serverlessLandWizard' |
| 16 | +import { Result, telemetry } from '../../shared/telemetry/telemetry' |
| 17 | +import { CreateServerlessLandWizard } from '../appBuilder/wizards/serverlessLandWizard' |
| 18 | +import { ExtContext } from '../../shared/extensions' |
| 19 | +import { addFolderToWorkspace } from '../../shared/utilities/workspaceUtils' |
| 20 | + |
| 21 | +export const readmeFile: string = 'README.md' |
| 22 | + |
| 23 | +// export async function getProjectUri( |
| 24 | +// config: Pick<CreateServerlessLandWizardForm, 'location' | 'name'>, |
| 25 | +// files: string |
| 26 | +// ): Promise<vscode.Uri | undefined> { |
| 27 | +// if (files.length === 0) { |
| 28 | +// throw Error('expected "files" parameter to have at least one item') |
| 29 | +// } |
| 30 | +// let file: string |
| 31 | +// let cfnTemplatePath: string |
| 32 | +// for (const f of files) { |
| 33 | +// file = f |
| 34 | +// cfnTemplatePath = path.resolve(config.location.fsPath, config.name, file) |
| 35 | +// if (await fileExists(cfnTemplatePath)) { |
| 36 | +// return vscode.Uri.file(cfnTemplatePath) |
| 37 | +// } |
| 38 | +// } |
| 39 | +// void vscode.window.showWarningMessage( |
| 40 | +// localize( |
| 41 | +// 'AWS.samcli.initWizard.source.error.notFound', |
| 42 | +// 'Project created successfully, but {0} file not found: {1}', |
| 43 | +// file!, |
| 44 | +// cfnTemplatePath! |
| 45 | +// ) |
| 46 | +// ) |
| 47 | +// } |
| 48 | + |
| 49 | +/** |
| 50 | + * Creates a new Serverless Land project using the provided extension context |
| 51 | + * @param extContext Extension context containing AWS credentials and region information |
| 52 | + * @returns Promise that resolves when the project creation is complete |
| 53 | + * |
| 54 | + * This function: |
| 55 | + * 1. Validates AWS credentials and regions |
| 56 | + * 2. Launches the Serverless Land project creation wizard |
| 57 | + * 3. Creates the project structure |
| 58 | + * 4. Adds the project folder to the workspace |
| 59 | + * 5. Opens the README.md file if available |
| 60 | + * 6. Handles errors and emits telemetry |
| 61 | + */ |
| 62 | +export async function createNewServerlessLandProject(extContext: ExtContext): Promise<void> { |
| 63 | + const awsContext = extContext.awsContext |
| 64 | + const regionProvider: RegionProvider = extContext.regionProvider |
| 65 | + let createResult: Result = 'Succeeded' |
| 66 | + let reason: string | undefined |
| 67 | + |
| 68 | + try { |
| 69 | + const credentials = await awsContext.getCredentials() |
| 70 | + const schemaRegions = regionProvider |
| 71 | + .getRegions() |
| 72 | + .filter((r) => regionProvider.isServiceInRegion('schemas', r.id)) |
| 73 | + const defaultRegion = awsContext.getCredentialDefaultRegion() |
| 74 | + |
| 75 | + // Launch the project creation wizard |
| 76 | + const config = await new CreateServerlessLandWizard({ |
| 77 | + credentials, |
| 78 | + schemaRegions, |
| 79 | + defaultRegion, |
| 80 | + }).run() |
| 81 | + if (!config) { |
| 82 | + createResult = 'Cancelled' |
| 83 | + reason = 'userCancelled' |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + // Add the project folder to the workspace |
| 88 | + await addFolderToWorkspace( |
| 89 | + { |
| 90 | + uri: config.location, |
| 91 | + name: path.basename(config.location.fsPath), |
| 92 | + }, |
| 93 | + true |
| 94 | + ) |
| 95 | + |
| 96 | + // Verify project creation and locate project files |
| 97 | + // const projectUri = await getProjectUri(config, readmeFile) |
| 98 | + // if (!projectUri) { |
| 99 | + // reason = 'fileNotFound' |
| 100 | + |
| 101 | + // return |
| 102 | + // } |
| 103 | + |
| 104 | + // Open README.md file |
| 105 | + // const readmeUri = vscode.Uri.file(path.join(path.dirname(projectUri.fsPath), readmeFile)) |
| 106 | + // if (await fileExists(readmeUri.fsPath)) { |
| 107 | + // const document = await vscode.workspace.openTextDocument(readmeUri) |
| 108 | + // await vscode.window.showTextDocument(document) |
| 109 | + // } else { |
| 110 | + // getLogger().warn( |
| 111 | + // localize('AWS.serverlessLand.readme.notFound', 'README.md file not found in the project directory') |
| 112 | + // ) |
| 113 | + // } |
| 114 | + } catch (err) { |
| 115 | + createResult = getTelemetryResult(err) |
| 116 | + reason = getTelemetryResult(err) |
| 117 | + |
| 118 | + globals.outputChannel.show(true) |
| 119 | + getLogger().error( |
| 120 | + localize('AWS.samcli.initWizard.general.error', 'Error creating new SAM Application. {0}', checklogs()) |
| 121 | + ) |
| 122 | + |
| 123 | + getLogger().error('Error creating new SAM Application: %O', err as Error) |
| 124 | + } finally { |
| 125 | + // add telemetry |
| 126 | + telemetry.sam_init.emit({ |
| 127 | + result: createResult, |
| 128 | + reason: reason, |
| 129 | + }) |
| 130 | + } |
| 131 | +} |
0 commit comments