Skip to content

Commit

Permalink
fix: ensure FF on stack transform, revert revert
Browse files Browse the repository at this point in the history
  • Loading branch information
jhockett committed Nov 11, 2021
1 parent a1bc526 commit 3323a7f
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 69 deletions.
2 changes: 1 addition & 1 deletion packages/amplify-category-custom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"dependencies": {
"amplify-cli-core": "1.33.0",
"amplify-prompts": "1.1.2",
"amplify-prompts": "1.3.0",
"execa": "^5.1.1",
"fs-extra": "^8.1.0",
"glob": "^7.2.0",
Expand Down
25 changes: 23 additions & 2 deletions packages/amplify-cli-core/src/feature-flags/featureFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,28 @@ export class FeatureFlags {
}
};

/**
* If feature flag exists do nothing, otherwise initalize the given feature flag with the default value.
* If the configuration file does not exist it will be created with the default features.
*/
public static ensureFeatureFlag = async (featureFlagSection: string, featureFlagName: string): Promise<void> => {
FeatureFlags.ensureInitialized();

let config = stateManager.getCLIJSON(FeatureFlags.instance.projectPath, undefined, {
throwIfNotExist: false,
preserveComments: true,
});

if (!config?.features) {
FeatureFlags.ensureDefaultFeatureFlags(false);
} else if (config.features?.[featureFlagSection]?.[featureFlagName] === undefined) {
const features = FeatureFlags.getExistingProjectDefaults();
_.set(config, ['features', featureFlagSection, featureFlagName], features[featureFlagSection][featureFlagName]);

stateManager.setCLIJSON(FeatureFlags.instance.projectPath, config);
}
};

public static getBoolean = (flagName: string): boolean => {
FeatureFlags.ensureInitialized();

Expand Down Expand Up @@ -718,12 +740,11 @@ export class FeatureFlags {
},
]);

// project level flags
this.registerFlag('project', [
{
name: 'overrides',
type: 'boolean',
defaultValueForExistingProjects: false,
defaultValueForExistingProjects: true,
defaultValueForNewProjects: true,
},
]);
Expand Down
4 changes: 2 additions & 2 deletions packages/amplify-cli-extensibility-helper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
"@aws-cdk/aws-lambda": "~1.124.0",
"@aws-cdk/aws-s3": "~1.124.0",
"@aws-cdk/core": "~1.124.0",
"amplify-cli-core": "1.31.1",
"amplify-prompts": "1.1.2"
"amplify-cli-core": "1.33.0",
"amplify-prompts": "1.3.0"
},
"jest": {
"transform": {
Expand Down
2 changes: 1 addition & 1 deletion packages/amplify-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"amplify-category-interactions": "2.7.0",
"amplify-category-notifications": "2.19.4",
"amplify-category-predictions": "2.10.0",
"amplify-category-xr": "2.8.24",
"amplify-category-xr": "2.9.0",
"amplify-cli-core": "1.33.0",
"amplify-cli-logger": "1.1.0",
"amplify-codegen": "^2.23.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FeatureFlags } from 'amplify-cli-core';
import { $TSContext } from 'amplify-cli-core';

export function getCategoryPluginInfo(context, category, service?) {
export function getCategoryPluginInfo(context: $TSContext, category: string, service?) {
let categoryPluginInfo;

const pluginInfosForCategory = context.pluginPlatform.plugins[category];
Expand All @@ -18,7 +18,7 @@ export function getCategoryPluginInfo(context, category, service?) {
}
} else {
const overidedPlugin = pluginInfosForCategory.find(plugin => {
return plugin.packageName === `@aws-amplify/amplify-category-${category}` && FeatureFlags.getBoolean(`overrides.${category}`);
return plugin.packageName === `@aws-amplify/amplify-category-${category}`;
});
if (overidedPlugin !== undefined) {
return overidedPlugin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,28 +133,17 @@ export async function getResourceStatus(
const tagsUpdated = !_.isEqual(stateManager.getProjectTags(), stateManager.getCurrentProjectTags());

// if not equal there is a root stack update
if (FeatureFlags.getBoolean('project.overrides')) {
const rootStackUpdated = await isRootStackModifiedSinceLastPush(getHashForRootStack);

return {
resourcesToBeCreated,
resourcesToBeUpdated,
resourcesToBeSynced,
resourcesToBeDeleted,
rootStackUpdated,
tagsUpdated,
allResources,
};
} else {
return {
resourcesToBeCreated,
resourcesToBeUpdated,
resourcesToBeSynced,
resourcesToBeDeleted,
tagsUpdated,
allResources,
};
}
const rootStackUpdated = await isRootStackModifiedSinceLastPush(getHashForRootStack);

return {
resourcesToBeCreated,
resourcesToBeUpdated,
resourcesToBeSynced,
resourcesToBeDeleted,
rootStackUpdated,
tagsUpdated,
allResources,
};
}

export function getAllResources(amplifyMeta, category, resourceName, filteredResources) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,23 @@
entry code for amplify override root
*/

import { $TSContext, FeatureFlags, generateOverrideSkeleton, pathManager } from 'amplify-cli-core';
import { printer } from 'amplify-prompts';
import { $TSContext, generateOverrideSkeleton, pathManager } from 'amplify-cli-core';
import * as fs from 'fs-extra';
import * as path from 'path';

const subcommand = 'override';

export const name = 'overrides';

export const run = async (context: $TSContext) => {
if (FeatureFlags.getBoolean('project.overrides')) {
const backendDir = pathManager.getBackendDirPath();
const backendDir = pathManager.getBackendDirPath();

const destPath = path.join(backendDir, 'awscloudformation');
fs.ensureDirSync(destPath);
const srcPath = path.join(__dirname, '..', '..', '..', 'resources', 'overrides-resource');
// removing runtime old root cfn stack
// no need for rollback since these files will be autogenerated after push
const oldRootStackFile = path.join(destPath, 'nested-cloudformation-stack.yml');
if (fs.existsSync(oldRootStackFile)) {
fs.unlinkSync(oldRootStackFile);
}
await generateOverrideSkeleton(context, srcPath, destPath);
} else {
printer.info('Project level overrides is currently not turned on. In cli.json file please include the following:');
printer.info(
JSON.stringify(
{
project: {
overrides: true,
},
},
undefined,
2,
),
);
const destPath = path.join(backendDir, 'awscloudformation');
fs.ensureDirSync(destPath);
const srcPath = path.join(__dirname, '..', '..', '..', 'resources', 'overrides-resource');
// removing runtime old root cfn stack
// no need for rollback since these files will be autogenerated after push
const oldRootStackFile = path.join(destPath, 'nested-cloudformation-stack.yml');
if (fs.existsSync(oldRootStackFile)) {
fs.unlinkSync(oldRootStackFile);
}
await generateOverrideSkeleton(context, srcPath, destPath);
};
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,7 @@ function cloneCLIJSONForNewEnvironment(context) {
export async function onInitSuccessful(context) {
configurationManager.onInitSuccessful(context);
if (context.exeInfo.isNewEnv) {
if (FeatureFlags.getBoolean('project.overrides')) {
await storeRootStackTemplate(context);
}
await storeRootStackTemplate(context);
context = await storeCurrentCloudBackend(context);
await storeArtifactsForAmplifyService(context);
await uploadHooksDirectory(context);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { $TSContext, IAmplifyResource, JSONUtilities, pathManager } from 'amplify-cli-core';
import { $TSContext, FeatureFlags, IAmplifyResource, JSONUtilities, pathManager } from 'amplify-cli-core';
import { printer } from 'amplify-prompts';
import * as fs from 'fs-extra';
import ora from 'ora';
Expand All @@ -24,6 +24,7 @@ export async function transformResourceWithOverrides(context: $TSContext, resour
spinner = ora(`Building resource ${resource.category}/${resource.resourceName}`);
spinner.start();
await transformCategoryStack(context, resource);
FeatureFlags.ensureFeatureFlag('project', 'overrides');
spinner.stop();
return;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,9 @@ export async function run(context: $TSContext, resourceDefinition: $TSObject, re

try {
await updateCloudFormationNestedStack(context, nestedStack, resourcesToBeCreated, resourcesToBeUpdated);
if (FeatureFlags.getBoolean('project.overrides')) {
await storeRootStackTemplate(context, nestedStack);
// if the only root stack updates, function is called with empty resources . this fn copies amplifyMeta and backend Config to #current-cloud-backend
context.amplify.updateamplifyMetaAfterPush([]);
}
await storeRootStackTemplate(context, nestedStack);
// if the only root stack updates, function is called with empty resources . this fn copies amplifyMeta and backend Config to #current-cloud-backend
context.amplify.updateamplifyMetaAfterPush([]);
} catch (err) {
if (err?.name === 'ValidationError' && err?.message === 'No updates are to be performed.') {
return;
Expand Down

0 comments on commit 3323a7f

Please sign in to comment.