diff --git a/packages/amplify-category-geo/amplify-plugin.json b/packages/amplify-category-geo/amplify-plugin.json index c0e36ecff65..24b496cf3ff 100644 --- a/packages/amplify-category-geo/amplify-plugin.json +++ b/packages/amplify-category-geo/amplify-plugin.json @@ -12,5 +12,5 @@ "commandAliases":{ "configure": "update" }, - "eventHandlers": [] + "eventHandlers": ["PrePush"] } \ No newline at end of file diff --git a/packages/amplify-category-geo/src/__tests__/commands/geo/remove.test.ts b/packages/amplify-category-geo/src/__tests__/commands/geo/remove.test.ts index 893509abd8d..2d613d44045 100644 --- a/packages/amplify-category-geo/src/__tests__/commands/geo/remove.test.ts +++ b/packages/amplify-category-geo/src/__tests__/commands/geo/remove.test.ts @@ -59,15 +59,4 @@ describe('remove command tests', () => { expect(mockRemoveResource).toHaveBeenCalledWith(mockContext, service); }); - - it('remove resource workflow is not invoked for unsupported region', async() => { - mockAmplifyMeta.providers[provider] = { - Region: 'eu-west-2' - }; - stateManager.getMeta = jest.fn().mockReturnValue(mockAmplifyMeta); - - await run(mockContext); - - expect(mockRemoveResource).toBeCalledTimes(0); - }); }); diff --git a/packages/amplify-category-geo/src/commands/geo/remove.ts b/packages/amplify-category-geo/src/commands/geo/remove.ts index 73c82f4d697..2bf435392b1 100644 --- a/packages/amplify-category-geo/src/commands/geo/remove.ts +++ b/packages/amplify-category-geo/src/commands/geo/remove.ts @@ -4,17 +4,12 @@ import { supportedServices } from '../../supportedServices'; import { $TSAny, $TSContext } from 'amplify-cli-core'; import { removeResource } from '../../provider-controllers'; import { printer } from 'amplify-prompts'; -import { verifySupportedRegion } from '../../service-utils/resourceUtils'; export const name = 'remove'; export const run = async(context: $TSContext) => { const { amplify } = context; try { - if(!verifySupportedRegion()) { - return; - } - const result: {service: string, providerName: string} = await amplify.serviceSelectionPrompt(context, category, supportedServices, chooseServiceMessageRemove); if (result.providerName !== provider) { diff --git a/packages/amplify-category-geo/src/index.ts b/packages/amplify-category-geo/src/index.ts index 5c9ab4f8556..359fdfe7c88 100644 --- a/packages/amplify-category-geo/src/index.ts +++ b/packages/amplify-category-geo/src/index.ts @@ -1,11 +1,11 @@ -import { $TSContext, $TSObject, stateManager } from 'amplify-cli-core'; +import { $TSContext, $TSObject, stateManager, exitOnNextTick, $TSAny } from 'amplify-cli-core'; import { category } from './constants'; import * as addCommand from './commands/geo/add'; import * as updateCommand from './commands/geo/update'; import * as removeCommand from './commands/geo/remove'; import * as consoleCommand from './commands/geo/console'; import * as helpCommand from './commands/geo/help'; -import { getServicePermissionPolicies } from './service-utils/resourceUtils'; +import { getServicePermissionPolicies, verifySupportedRegion, checkAnyGeoResourceExists } from './service-utils/resourceUtils'; import { ServiceName } from './service-utils/constants'; import { printer } from 'amplify-prompts'; @@ -32,9 +32,19 @@ export const executeAmplifyCommand = async (context: $TSContext) => { } }; -export const handleAmplifyEvent = (context: $TSContext, args: $TSObject) => { - printer.info(`${category} handleAmplifyEvent to be implemented`); - printer.info(`Received event args ${args}`); +export const handleAmplifyEvent = async (context: $TSContext, args: $TSAny) => { + switch (args.event) { + case 'PrePush': + if((await checkAnyGeoResourceExists()) && !verifySupportedRegion()) { + const errMessage = 'Failed to create Geo resources'; + await context.usageData.emitError(new Error(errMessage)); + printer.info('Remove Geo resources using "amplify remove geo" and retry "amplify push"'); + exitOnNextTick(1); + } + break; + default: + break; + } }; export const getPermissionPolicies = (context: $TSContext, resourceOpsMapping: $TSObject) => { diff --git a/packages/amplify-category-geo/src/service-utils/resourceUtils.ts b/packages/amplify-category-geo/src/service-utils/resourceUtils.ts index 5fb1104509c..b026ab269ba 100644 --- a/packages/amplify-category-geo/src/service-utils/resourceUtils.ts +++ b/packages/amplify-category-geo/src/service-utils/resourceUtils.ts @@ -219,8 +219,16 @@ export const getServicePermissionPolicies = ( export const verifySupportedRegion = (): boolean => { const currentRegion = stateManager.getMeta()?.providers[provider]?.Region; if(!supportedRegions.includes(currentRegion)) { - printer.error(`Geo category is not supported in your region: ${currentRegion}`); + printer.error(`Geo category is not supported in the region: [${currentRegion}]`); return false; } return true; }; + +/** + * Check if any Geo resource exists + */ + export const checkAnyGeoResourceExists = async (): Promise => { + const geoMeta = stateManager.getMeta()?.[category]; + return geoMeta && Object.keys(geoMeta) && Object.keys(geoMeta).length > 0; +}