Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

formgen: add type declaration generation #430

Merged
merged 9 commits into from
Oct 24, 2023
5 changes: 5 additions & 0 deletions .changeset/famous-rockets-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@aws-amplify/form-generator': patch
---

Generate type declaration files for rendered components
2 changes: 1 addition & 1 deletion packages/create-amplify/src/get_project_root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { logger } from './logger.js';
export const getProjectRoot = async () => {
const useDefault = process.env.npm_config_yes === 'true';
const defaultProjectRoot = '.';
let projectRoot = useDefault
let projectRoot: string = useDefault
? defaultProjectRoot
: await AmplifyPrompter.input({
message: 'Where should we create your project?',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,35 @@ void describe('LocalCodegenGraphqlFormGenerator', () => {
);
await assert.rejects(() => l.generateForms({ models: ['Author'] }));
});
void it('type declaration files are created for each model', async () => {
const models = ['Post', 'Author', 'Foo'];
const schema = createMockSchema(models);
const l = new LocalGraphqlFormGenerator(
async () => schema as unknown as GenericDataSchema,
{
graphqlDir: '../graphql',
},
(map) => {
return new CodegenGraphqlFormGeneratorResult(map);
}
);
const output = await l.generateForms();
const fsMock = mock.method(fs, 'open');
fsMock.mock.mockImplementation(async () => ({
writeFile: async () => undefined,
stat: async () => ({}),
close: async () => undefined,
}));
await output.writeToDirectory('./');
const writeArgs = fsMock.mock.calls.flatMap((c) => c.arguments[0]);
assert(
models.every((m) => {
return writeArgs.some((arg) =>
new RegExp(`${m}(Update|Create)Form.d.ts`).test(arg.toString())
);
})
);
});
void it('when an undefined filter is passed, all models are generated', async () => {
const models = ['Post', 'Author', 'Foo'];
const schema = createMockSchema(models);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ScriptKind,
ScriptTarget,
UtilTemplateType,
getDeclarationFilename,
} from '@aws-amplify/codegen-ui-react';
import {
FormGenerationOptions,
Expand Down Expand Up @@ -125,11 +126,20 @@ export class LocalGraphqlFormGenerator implements GraphqlFormGenerator {
formFeatureFlags
);
const { componentText, declaration } = renderer.renderComponentInternal();
return {
componentText,
fileName: renderer.fileName,
declaration,
};
const files = [
{
componentText,
fileName: renderer.fileName,
},
];
if (declaration) {
files.push({
componentText: declaration,
fileName: getDeclarationFilename(renderer.fileName),
});
}

return files;
};
sdstolworthy marked this conversation as resolved.
Show resolved Hide resolved
private filterModelsByName = (
filteredModelNames: string[],
Expand Down Expand Up @@ -262,8 +272,10 @@ export class LocalGraphqlFormGenerator implements GraphqlFormGenerator {
);
const forms = baseForms.reduce<Record<string, string>>(
(prev, formSchema) => {
const result = this.codegenForm(dataSchema, formSchema);
prev[result.fileName] = result.componentText;
const results = this.codegenForm(dataSchema, formSchema);
results.forEach((result) => {
prev[result.fileName] = result.componentText;
});
return prev;
},
{}
Expand Down