From 2b2ad6b6c05da591da8017fbe688aebbb36ef822 Mon Sep 17 00:00:00 2001 From: akshbhu <39866697+akshbhu@users.noreply.github.com> Date: Tue, 26 Oct 2021 15:15:09 -0700 Subject: [PATCH] fix: parameters file path and updates cloud backend with build (#8564) --- .../auth-stack-transform.ts | 2 +- .../user-pool-group-stack-transform.ts | 2 +- .../provider-utils/awscloudformation/index.js | 4 +- .../cdk-stack-builder/ddb-stack-transform.ts | 2 +- .../cdk-stack-builder/s3-stack-transform.ts | 2 +- .../src/state-manager/pathManager.ts | 50 +++++++++++-------- .../src/state-manager/stateManager.ts | 23 ++++++++- .../root-stack-transform.ts | 2 +- .../src/utils/archiver.js | 8 +++ 9 files changed, 66 insertions(+), 29 deletions(-) diff --git a/packages/amplify-category-auth/src/provider-utils/awscloudformation/auth-stack-builder/auth-stack-transform.ts b/packages/amplify-category-auth/src/provider-utils/awscloudformation/auth-stack-builder/auth-stack-transform.ts index 95ab2bccbf0..614daa3f15f 100644 --- a/packages/amplify-category-auth/src/provider-utils/awscloudformation/auth-stack-builder/auth-stack-transform.ts +++ b/packages/amplify-category-auth/src/provider-utils/awscloudformation/auth-stack-builder/auth-stack-transform.ts @@ -102,7 +102,7 @@ export class AmplifyAuthTransform extends AmplifyCategoryTransform { const backendDir = pathManager.getBackendDirPath(); const overrideDir = path.join(backendDir, this._category, this.resourceName); const isBuild = await buildOverrideDir(backendDir, overrideDir).catch(error => { - printer.warn(`Skipping build as ${error.message}`); + printer.debug(`Skipping build as ${error.message}`); return false; }); if (isBuild) { diff --git a/packages/amplify-category-auth/src/provider-utils/awscloudformation/auth-stack-builder/user-pool-group-stack-transform.ts b/packages/amplify-category-auth/src/provider-utils/awscloudformation/auth-stack-builder/user-pool-group-stack-transform.ts index 2232c9c3fa8..af65d86debb 100644 --- a/packages/amplify-category-auth/src/provider-utils/awscloudformation/auth-stack-builder/user-pool-group-stack-transform.ts +++ b/packages/amplify-category-auth/src/provider-utils/awscloudformation/auth-stack-builder/user-pool-group-stack-transform.ts @@ -175,7 +175,7 @@ export class AmplifyUserPoolGroupTransform extends AmplifyCategoryTransform { const backendDir = pathManager.getBackendDirPath(); const overrideDir = path.join(backendDir, this._category, this._resourceName); const isBuild = await buildOverrideDir(backendDir, overrideDir).catch(error => { - printer.warn(`Skipping build as ${error.message}`); + printer.debug(`Skipping build as ${error.message}`); return false; }); if (isBuild) { diff --git a/packages/amplify-category-auth/src/provider-utils/awscloudformation/index.js b/packages/amplify-category-auth/src/provider-utils/awscloudformation/index.js index 98a2b680bfe..6c28f98fae6 100644 --- a/packages/amplify-category-auth/src/provider-utils/awscloudformation/index.js +++ b/packages/amplify-category-auth/src/provider-utils/awscloudformation/index.js @@ -38,9 +38,7 @@ async function updateConfigOnEnvInit(context, category, service) { const providerPlugin = context.amplify.getPluginInstance(context, provider); // previously selected answers - const currentAuthName = await getAuthResourceName(context); - const cliState = new AuthInputState(currentAuthName); - const resourceParams = await cliState.loadResourceParameters(context, cliState.getCLIInputPayload()); + const resourceParams = providerPlugin.loadResourceParameters(context, 'auth', service); // ask only env specific questions let currentEnvSpecificValues = context.amplify.loadEnvResourceParameters(context, category, service); diff --git a/packages/amplify-category-storage/src/provider-utils/awscloudformation/cdk-stack-builder/ddb-stack-transform.ts b/packages/amplify-category-storage/src/provider-utils/awscloudformation/cdk-stack-builder/ddb-stack-transform.ts index 4342016172b..f0eac17d740 100644 --- a/packages/amplify-category-storage/src/provider-utils/awscloudformation/cdk-stack-builder/ddb-stack-transform.ts +++ b/packages/amplify-category-storage/src/provider-utils/awscloudformation/cdk-stack-builder/ddb-stack-transform.ts @@ -171,7 +171,7 @@ export class DDBStackTransform { const overrideFilePath = pathManager.getResourceDirectoryPath(undefined, 'storage', this._resourceName); const isBuild = await buildOverrideDir(backendDir, overrideFilePath).catch(error => { - printer.warn(`Skipping build as ${error.message}`); + printer.debug(`Skipping build as ${error.message}`); return false; }); // skip if packageManager or override.ts not found diff --git a/packages/amplify-category-storage/src/provider-utils/awscloudformation/cdk-stack-builder/s3-stack-transform.ts b/packages/amplify-category-storage/src/provider-utils/awscloudformation/cdk-stack-builder/s3-stack-transform.ts index 72b56679a8b..2b969e950d1 100644 --- a/packages/amplify-category-storage/src/provider-utils/awscloudformation/cdk-stack-builder/s3-stack-transform.ts +++ b/packages/amplify-category-storage/src/provider-utils/awscloudformation/cdk-stack-builder/s3-stack-transform.ts @@ -167,7 +167,7 @@ export class AmplifyS3ResourceStackTransform { const backendDir = pathManager.getBackendDirPath(); const overrideFilePath = pathManager.getResourceDirectoryPath(undefined, AmplifyCategories.STORAGE, this.resourceName); const isBuild = await buildOverrideDir(backendDir, overrideFilePath).catch(error => { - printer.warn(`Skipping build as ${error.message}`); + printer.debug(`Skipping build as ${error.message}`); return false; }); //Skip if packageManager or override.ts not found diff --git a/packages/amplify-cli-core/src/state-manager/pathManager.ts b/packages/amplify-cli-core/src/state-manager/pathManager.ts index f013f6a3ad3..cbbdefefc74 100644 --- a/packages/amplify-cli-core/src/state-manager/pathManager.ts +++ b/packages/amplify-cli-core/src/state-manager/pathManager.ts @@ -21,6 +21,8 @@ export const PathConstants = { CurrentCloudBackendDirName: '#current-cloud-backend', HooksDirName: 'hooks', + // resource level + BuildDirName: 'build', // 2nd Level OverrideDirName: 'overrides', ProviderName: 'awscloudformation', @@ -41,7 +43,6 @@ export const PathConstants = { HooksShellSampleFileName: 'post-push.sh.sample', HooksJsSampleFileName: 'pre-push.js.sample', HooksReadmeFileName: 'hooks-readme.md', - OverrideFileName: 'override.ts', LocalEnvFileName: 'local-env-info.json', LocalAWSInfoFileName: 'local-aws-info.json', @@ -52,10 +53,11 @@ export const PathConstants = { CLIJSONFileNameGlob: 'cli*.json', CLIJsonWithEnvironmentFileName: (env: string) => `cli.${env}.json`, + CLIInputsJsonFileName: 'cli-inputs.json', + CfnFileName: (resourceName: string) => `${resourceName}-awscloudformation-template.json`, CustomPoliciesFilename: 'custom-policies.json', - cliInputsFileName: 'cli-inputs.json', }; export class PathManager { @@ -92,16 +94,6 @@ export class PathManager { getBackendDirPath = (projectPath?: string): string => this.constructPath(projectPath, [PathConstants.AmplifyDirName, PathConstants.BackendDirName]); - getCliInputsPath = (projectPath: string, category: string, resourceName: string): string => { - return this.constructPath(projectPath, [ - PathConstants.AmplifyDirName, - PathConstants.BackendDirName, - category, - resourceName, - PathConstants.cliInputsFileName, - ]); - }; - getCurrentCloudBackendDirPath = (projectPath?: string): string => this.constructPath(projectPath, [PathConstants.AmplifyDirName, PathConstants.CurrentCloudBackendDirName]); @@ -142,11 +134,29 @@ export class PathManager { getResourceDirectoryPath = (projectPath: string | undefined, category: string, resourceName: string): string => this.constructPath(projectPath, [PathConstants.AmplifyDirName, PathConstants.BackendDirName, category, resourceName]); - getResourceParametersFilePath = (projectPath: string | undefined, category: string, resourceName: string): string => - path.join(this.getResourceDirectoryPath(projectPath, category, resourceName), 'build', PathConstants.ParametersJsonFileName); + getResourceInputsJsonFilePath = (projectPath: string | undefined, category: string, resourceName: string): string => + path.join(this.getResourceDirectoryPath(projectPath, category, resourceName), PathConstants.CLIInputsJsonFileName); + + getResourceParametersFilePath = (projectPath: string | undefined, category: string, resourceName: string): string => { + let isBuildParametersjson: boolean = false; + const resourceDirPath = this.getResourceDirectoryPath(projectPath, category, resourceName); + if (!fs.existsSync(path.join(resourceDirPath, PathConstants.ParametersJsonFileName))) { + isBuildParametersjson = true; + } + const basePath = isBuildParametersjson ? path.join(resourceDirPath, PathConstants.BuildDirName) : resourceDirPath; + return path.join(basePath, PathConstants.ParametersJsonFileName); + }; - getResourceCfnTemplatePath = (projectPath: string | undefined, category: string, resourceName: string): string => - path.join(this.getResourceDirectoryPath(projectPath, category, resourceName), 'build', PathConstants.CfnFileName(resourceName)); + getResourceCfnTemplatePath = ( + projectPath: string | undefined, + category: string, + resourceName: string, + buildDirectory = false, + ): string => { + const resourceDirPath = this.getResourceDirectoryPath(projectPath, category, resourceName); + const basePath = buildDirectory ? path.join(resourceDirPath, PathConstants.BuildDirName) : resourceDirPath; + return path.join(basePath, PathConstants.CfnFileName(resourceName)); + }; getReadMeFilePath = (projectPath?: string): string => this.constructPath(projectPath, [PathConstants.AmplifyDirName, PathConstants.ReadMeFileName]); @@ -167,8 +177,8 @@ export class PathManager { getDotAWSDirPath = (): string => path.normalize(path.join(homedir(), PathConstants.DotAWSDirName)); - getCustomPoliciesPath = (category: string, resourceName: string): string => - path.join(this.getResourceDirectoryPath(undefined, category, resourceName), PathConstants.CustomPoliciesFilename); + getCustomPoliciesPath = (category: string, resourceName: string): string => + path.join(this.getResourceDirectoryPath(undefined, category, resourceName), PathConstants.CustomPoliciesFilename); getAWSCredentialsFilePath = (): string => path.normalize(path.join(this.getDotAWSDirPath(), PathConstants.AWSCredentials)); @@ -214,7 +224,7 @@ export class PathManager { PathConstants.AmplifyDirName, PathConstants.BackendDirName, PathConstants.ProviderName, - PathConstants.CfnStacksBuildDirName, + PathConstants.BuildDirName, ]); }; @@ -223,7 +233,7 @@ export class PathManager { PathConstants.AmplifyDirName, PathConstants.CurrentCloudBackendDirName, PathConstants.ProviderName, - PathConstants.CfnStacksBuildDirName, + PathConstants.BuildDirName, ]); }; diff --git a/packages/amplify-cli-core/src/state-manager/stateManager.ts b/packages/amplify-cli-core/src/state-manager/stateManager.ts index a63679be3f1..6e7ba0681d6 100644 --- a/packages/amplify-cli-core/src/state-manager/stateManager.ts +++ b/packages/amplify-cli-core/src/state-manager/stateManager.ts @@ -2,7 +2,7 @@ import * as fs from 'fs-extra'; import * as path from 'path'; import _ from 'lodash'; import { PathConstants, pathManager } from './pathManager'; -import { $TSMeta, $TSTeamProviderInfo, $TSAny, DeploymentSecrets, HooksConfig } from '..'; +import { $TSMeta, $TSTeamProviderInfo, $TSAny, DeploymentSecrets, HooksConfig, $TSObject } from '..'; import { JSONUtilities } from '../jsonUtilities'; import { SecretFileMode } from '../cliConstants'; import { HydrateTags, ReadTags, Tag } from '../tags'; @@ -145,6 +145,21 @@ export class StateManager { return this.getData<$TSAny>(filePath, mergedOptions); }; + getResourceInputsJson = ( + projectPath: string | undefined, + category: string, + resourceName: string, + options?: GetOptions<$TSAny>, + ): $TSAny => { + const filePath = pathManager.getResourceInputsJsonFilePath(projectPath, category, resourceName); + const mergedOptions = { + throwIfNotExist: true, + ...options, + }; + + return this.getData<$TSAny>(filePath, mergedOptions); + }; + getCurrentResourceParametersJson = ( projectPath: string | undefined, category: string, @@ -269,6 +284,12 @@ export class StateManager { JSONUtilities.writeJson(filePath, parameters); }; + setResourceInputsJson = (projectPath: string | undefined, category: string, resourceName: string, inputs: $TSObject): void => { + const filePath = pathManager.getResourceInputsJsonFilePath(projectPath, category, resourceName); + + JSONUtilities.writeJson(filePath, inputs); + }; + cliJSONFileExists = (projectPath: string, env?: string): boolean => { try { return fs.existsSync(pathManager.getCLIJSONFilePath(projectPath, env)); diff --git a/packages/amplify-provider-awscloudformation/src/root-stack-builder/root-stack-transform.ts b/packages/amplify-provider-awscloudformation/src/root-stack-builder/root-stack-transform.ts index 5d2c2edc2cd..78db6c18496 100644 --- a/packages/amplify-provider-awscloudformation/src/root-stack-builder/root-stack-transform.ts +++ b/packages/amplify-provider-awscloudformation/src/root-stack-builder/root-stack-transform.ts @@ -49,7 +49,7 @@ export class AmplifyRootStackTransform { const backendDir = pathManager.getBackendDirPath(); const overrideFilePath = path.join(backendDir, this._resourceName); const isBuild = await buildOverrideDir(backendDir, overrideFilePath).catch(error => { - amplifyPrinter.printer.warn(`Skipping build as ${error.message}`); + amplifyPrinter.printer.debug(`Skipping build as ${error.message}`); return false; }); // skip if packageManager or override.ts not found diff --git a/packages/amplify-provider-awscloudformation/src/utils/archiver.js b/packages/amplify-provider-awscloudformation/src/utils/archiver.js index 2fa8cc43817..643f6f9e316 100644 --- a/packages/amplify-provider-awscloudformation/src/utils/archiver.js +++ b/packages/amplify-provider-awscloudformation/src/utils/archiver.js @@ -25,6 +25,14 @@ function run(folder, zipFilePath, ignorePattern = DEFAULT_IGNORE_PATTERN, extraF cwd: folder, dot: true, }); + zip.glob('storage/*/build/**', { + cwd: folder, + dot: true, + }); + zip.glob('auth/*/build/**', { + cwd: folder, + dot: true, + }); zip.glob('**', { cwd: folder, ignore: ignorePattern,