@@ -7,7 +7,7 @@ import { v4 as uuid } from 'uuid';
77import { createGen2Renderer } from '@aws-amplify/amplify-gen2-codegen' ;
88
99import { UsageData } from '@aws-amplify/cli-internal' ;
10- import { AmplifyClient , UpdateAppCommand } from '@aws-sdk/client-amplify' ;
10+ import { AmplifyClient , UpdateAppCommand , GetAppCommand } from '@aws-sdk/client-amplify' ;
1111import { CloudFormationClient } from '@aws-sdk/client-cloudformation' ;
1212import { CognitoIdentityProviderClient , LambdaConfigType } from '@aws-sdk/client-cognito-identity-provider' ;
1313import { CognitoIdentityClient } from '@aws-sdk/client-cognito-identity' ;
@@ -21,7 +21,7 @@ import { BackendEnvironmentResolver } from './backend_environment_selector';
2121import { Analytics , AppAnalytics } from './analytics' ;
2222import { AppAuthDefinitionFetcher } from './app_auth_definition_fetcher' ;
2323import { AppStorageDefinitionFetcher } from './app_storage_definition_fetcher' ;
24- import { AmplifyCategories , IUsageData , stateManager } from '@aws-amplify/amplify-cli-core' ;
24+ import { AmplifyCategories , IUsageData , stateManager , pathManager } from '@aws-amplify/amplify-cli-core' ;
2525import { AuthTriggerConnection } from '@aws-amplify/amplify-gen1-codegen-auth-adapter' ;
2626import { DataDefinitionFetcher } from './data_definition_fetcher' ;
2727import { AmplifyStackParser } from './amplify_stack_parser' ;
@@ -36,7 +36,6 @@ interface CodegenCommandParameters {
3636 logger: AppContextLogger ;
3737 outputDirectory: string ;
3838 backendEnvironmentName: string | undefined ;
39- appId: string ;
4039 dataDefinitionFetcher: DataDefinitionFetcher ;
4140 authDefinitionFetcher: AppAuthDefinitionFetcher ;
4241 storageDefinitionFetcher: AppStorageDefinitionFetcher ;
@@ -46,6 +45,8 @@ interface CodegenCommandParameters {
4645const TEMP_GEN_2_OUTPUT_DIR = 'amplify-gen2' ;
4746const AMPLIFY_DIR = 'amplify' ;
4847const MIGRATION_DIR = '.amplify/migration' ;
48+ const GEN1_COMMAND = 'amplifyPush --simple' ;
49+ const GEN2_COMMAND = 'npx ampx pipeline-deploy --branch $AWS_BRANCH --app-id $AWS_APP_ID' ;
4950
5051enum GEN2_AMPLIFY_GITIGNORE_FILES_OR_DIRS {
5152 DOT_AMPLIFY = '.amplify' ,
@@ -57,7 +58,6 @@ enum GEN2_AMPLIFY_GITIGNORE_FILES_OR_DIRS {
5758const generateGen2Code = async ( {
5859 outputDirectory,
5960 backendEnvironmentName,
60- appId,
6161 authDefinitionFetcher,
6262 dataDefinitionFetcher,
6363 storageDefinitionFetcher,
@@ -66,7 +66,6 @@ const generateGen2Code = async ({
6666 const fetchingAWSResourceDetails = ora ( 'Fetching resource details from AWS' ) . start ( ) ;
6767 const gen2RenderOptions = {
6868 outputDir : outputDirectory ,
69- appId : appId ,
7069 backendEnvironmentName : backendEnvironmentName ,
7170 auth : await authDefinitionFetcher . getDefinition ( ) ,
7271 storage : await storageDefinitionFetcher . getDefinition ( ) ,
@@ -214,6 +213,39 @@ const unsupportedCategories = (): Map<string, string> => {
214213 return unsupportedCategoriesList ;
215214} ;
216215
216+ export async function updateAmplifyYmlFile ( amplifyClient : AmplifyClient , appId : string ) {
217+ const rootDir = pathManager . findProjectRoot ( ) ;
218+ assert ( rootDir ) ;
219+ const amplifyYmlPath = path . join ( rootDir , 'amplify.yml' ) ;
220+
221+ try {
222+ // Read the content of amplify.yml file if it exists
223+ const amplifyYmlContent = await fs . readFile ( amplifyYmlPath , 'utf-8' ) ;
224+
225+ await writeToAmplifyYmlFile ( amplifyYmlPath , amplifyYmlContent ) ;
226+ } catch ( error ) {
227+ if ( error . code === 'ENOENT' ) {
228+ // If amplify.yml file doesn't exist, make a getApp call to get buildSpec
229+ const getAppResponse = await amplifyClient . send ( new GetAppCommand ( { appId } ) ) ;
230+
231+ assert ( getAppResponse . app , 'App not found' ) ;
232+ const buildSpec = getAppResponse . app . buildSpec ;
233+ assert ( buildSpec , 'buildSpec not found in the app' ) ;
234+
235+ await writeToAmplifyYmlFile ( amplifyYmlPath , buildSpec ) ;
236+ } else {
237+ // Throw the original error if it's not related to file not found
238+ throw error ;
239+ }
240+ }
241+ }
242+
243+ async function writeToAmplifyYmlFile ( amplifyYmlPath : string , content : string ) {
244+ // Replace 'amplifyPush --simple' with 'npx ampx pipeline-deploy'
245+ content = content . replace ( new RegExp ( GEN1_COMMAND , 'g' ) , GEN2_COMMAND ) ;
246+ await fs . writeFile ( amplifyYmlPath , content , { encoding : 'utf-8' } ) ;
247+ }
248+
217249async function updateGitIgnoreForGen2 ( ) {
218250 const cwd = process . cwd ( ) ;
219251 const updateGitIgnore = ora ( 'Updating gitignore contents' ) . start ( ) ;
@@ -287,14 +319,15 @@ export async function execute() {
287319 ( ) => getAuthTriggersConnections ( ) ,
288320 ccbFetcher ,
289321 ) ,
290- dataDefinitionFetcher : new DataDefinitionFetcher ( backendEnvironmentResolver , amplifyStackParser ) ,
322+ dataDefinitionFetcher : new DataDefinitionFetcher ( backendEnvironmentResolver , new BackendDownloader ( s3Client ) , amplifyStackParser ) ,
291323 functionsDefinitionFetcher : new AppFunctionsDefinitionFetcher ( lambdaClient , backendEnvironmentResolver , stateManager ) ,
292324 analytics : new AppAnalytics ( appId ) ,
293325 logger : new AppContextLogger ( appId ) ,
294326 backendEnvironmentName : backendEnvironment ?. environmentName ,
295- appId : appId ,
296327 } ) ;
297328
329+ await updateAmplifyYmlFile ( amplifyClient , appId ) ;
330+
298331 await updateGitIgnoreForGen2 ( ) ;
299332
300333 const movingGen1BackendFiles = ora ( `Moving your Gen1 backend files to ${ format . highlight ( MIGRATION_DIR ) } ` ) . start ( ) ;
0 commit comments