Skip to content

Commit

Permalink
feat: ensure modelgen does not throw an error when running in an unin…
Browse files Browse the repository at this point in the history
…itialized directory (#13221)
  • Loading branch information
alharris-at authored Sep 15, 2023
1 parent 52aea18 commit 8b5d433
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
17 changes: 17 additions & 0 deletions packages/amplify-cli/src/__tests__/execution-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { CLIInput as CommandLineInput } from '../domain/command-input';
import { Context } from '../domain/context';
import { PluginInfo } from '@aws-amplify/amplify-cli-core';
import { executeCommand } from '../execution-manager';
import { printer } from '@aws-amplify/amplify-prompts';

jest.mock('@aws-amplify/amplify-prompts');

const handleAmplifyEventMock = jest.fn();
jest.mock('../../__mocks__/faked-plugin', () => ({
Expand Down Expand Up @@ -66,6 +69,7 @@ describe('execution manager', () => {

beforeEach(() => {
jest.clearAllMocks();
mockContext.parameters = { options: {} };
});

it.each([
Expand Down Expand Up @@ -93,4 +97,17 @@ describe('execution manager', () => {
await executeCommand(mockContext);
expect(handleAmplifyEventMock).toBeCalledWith(mockContext, args);
});

it.each([[AmplifyEvent.PreCodegenModels], [AmplifyEvent.PostCodegenModels]])(
'executeCommand skips %s when target and model-schema parameters are provided',
async (event) => {
mockFs.readdirSync.mockReturnValue([]);
mockFs.existsSync.mockReturnValue(true);
mockContext.input.command = 'models';
mockContext.parameters.options = { target: 'javascript', 'model-schema': 'schema.graphql' };
await executeCommand(mockContext);
expect(printer.info).toBeCalledWith(expect.stringContaining(`Skipping ${event}`));
expect(handleAmplifyEventMock).not.toBeCalledWith(mockContext, { event, data: {} });
},
);
});
20 changes: 19 additions & 1 deletion packages/amplify-cli/src/execution-manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from 'fs-extra';
import * as path from 'path';
import { prompter } from '@aws-amplify/amplify-prompts';
import { printer, prompter } from '@aws-amplify/amplify-prompts';
import { twoStringSetsAreEqual, twoStringSetsAreDisjoint } from './utils/set-ops';
import { Context } from './domain/context';
import { scan, getPluginsWithNameAndCommand, getPluginsWithEventHandler } from './plugin-manager';
Expand Down Expand Up @@ -269,6 +269,9 @@ const raisePreExportEvent = async (context: Context): Promise<void> => {
};

const raisePreCodegenModelsEvent = async (context: Context): Promise<void> => {
if (shouldSkipCodegenModelsEvents(context, AmplifyEvent.PreCodegenModels)) {
return;
}
await raiseEvent(context, { event: AmplifyEvent.PreCodegenModels, data: {} });
};

Expand Down Expand Up @@ -309,7 +312,22 @@ const raisePostPullEvent = async (context: Context): Promise<void> => {
await raiseEvent(context, { event: AmplifyEvent.PostPull, data: {} });
};

const shouldSkipCodegenModelsEvents = (context: Context, event: AmplifyEvent): boolean => {
const optionsIndicatingUninitializedModelgen = ['target', 'model-schema'];
const cliOptions = context?.parameters?.options ? new Set(Object.keys(context.parameters.options)) : new Set();
const skipEvents = optionsIndicatingUninitializedModelgen.every((option) => cliOptions.has(option));
if (skipEvents) {
printer.info(
`Skipping ${event} lifecycle event, due to presence of ${JSON.stringify(optionsIndicatingUninitializedModelgen)} in context options`,
);
}
return skipEvents;
};

const raisePostCodegenModelsEvent = async (context: Context): Promise<void> => {
if (shouldSkipCodegenModelsEvents(context, AmplifyEvent.PostCodegenModels)) {
return;
}
await raiseEvent(context, { event: AmplifyEvent.PostCodegenModels, data: {} });
};

Expand Down

0 comments on commit 8b5d433

Please sign in to comment.