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

Release Codegen Plugin #707

Merged
merged 3 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions .codebuild/e2e_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,27 @@ batch:
CLI_REGION: ap-northeast-1
depend-on:
- publish_to_local_registry
- identifier: build_app_ts
- identifier: >-
build_app_ts_uninitialized_project_codegen_js_uninitialized_project_modelgen_android_uninitialized_project_modelgen_flutter
buildspec: .codebuild/run_e2e_tests.yml
env:
compute-type: BUILD_GENERAL1_LARGE
variables:
TEST_SUITE: src/__tests__/build-app-ts.test.ts
TEST_SUITE: >-
src/__tests__/build-app-ts.test.ts|src/__tests__/uninitialized-project-codegen-js.test.ts|src/__tests__/uninitialized-project-modelgen-android.test.ts|src/__tests__/uninitialized-project-modelgen-flutter.test.ts
CLI_REGION: ap-southeast-1
depend-on:
- publish_to_local_registry
- identifier: uninitialized_project_modelgen_ios_uninitialized_project_modelgen_js
buildspec: .codebuild/run_e2e_tests.yml
env:
compute-type: BUILD_GENERAL1_LARGE
variables:
TEST_SUITE: >-
src/__tests__/uninitialized-project-modelgen-ios.test.ts|src/__tests__/uninitialized-project-modelgen-js.test.ts
CLI_REGION: ap-southeast-2
depend-on:
- publish_to_local_registry
- identifier: cleanup_e2e_resources
buildspec: .codebuild/cleanup_e2e_resources.yml
env:
Expand Down
112 changes: 94 additions & 18 deletions packages/amplify-codegen-e2e-core/src/categories/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,50 @@ export function generateModels(cwd: string, outputDir?: string, settings: { errM
});
}

export function generateStatementsAndTypes(cwd: string) : Promise<void> {
export const generateModelsWithOptions = (cwd: string, options: Record<string, any>): Promise<void> => new Promise((resolve, reject) => {
spawn(getCLIPath(), ['codegen', 'models', ...(Object.entries(options).flat())], { cwd, stripColors: true }).run((err: Error) => {
if (!err) {
resolve();
} else {
reject(err);
}
});
});

export function generateStatementsAndTypes(cwd: string, errorMessage?: string) : Promise<void> {
return new Promise((resolve, reject) => {
const chain = spawn(getCLIPath(), ['codegen'], { cwd, stripColors: true })

if (errorMessage) {
chain.wait(errorMessage);
}

return chain.run((err: Error) => {
if (!err) {
resolve();
} else {
reject(err);
}
})
});
}

export function generateStatements(cwd: string) : Promise<void> {
return new Promise((resolve, reject) => {
spawn(getCLIPath(), ['codegen'], { cwd, stripColors: true })
spawn(getCLIPath(), ['codegen', 'statements'], { cwd, stripColors: true })
.run((err: Error) => {
if (!err) {
resolve();
} else {
reject(err);
}
})
});
}

export function generateTypes(cwd: string) : Promise<void> {
return new Promise((resolve, reject) => {
spawn(getCLIPath(), ['codegen', 'types'], { cwd, stripColors: true })
.run((err: Error) => {
if (!err) {
resolve();
Expand All @@ -36,7 +77,10 @@ export function generateStatementsAndTypes(cwd: string) : Promise<void> {
// CLI workflow to add codegen to Amplify project
export function addCodegen(cwd: string, settings: any = {}): Promise<void> {
return new Promise((resolve, reject) => {
const chain = spawn(getCLIPath(), ['codegen', 'add'], { cwd, stripColors: true });
const params = settings.params
? ['codegen', 'add', ...settings.params]
: ['codegen', 'add'];
const chain = spawn(getCLIPath(), params, { cwd, stripColors: true });
if (settings.isAPINotAdded) {
chain.wait("There are no GraphQL APIs available.");
chain.wait("Add by running $amplify api add");
Expand Down Expand Up @@ -156,22 +200,26 @@ export function generateModelIntrospection(cwd: string, settings: { outputDir?:
}

// CLI workflow to add codegen to non-Amplify JS project
export function addCodegenNonAmplifyJS(cwd: string): Promise<void> {
export function addCodegenNonAmplifyJS(cwd: string, params: Array<string>, initialFailureMessage?: string): Promise<void> {
return new Promise((resolve, reject) => {
const cmdOptions = ['codegen', 'add', '--apiId', 'mockapiid'];
const chain = spawn(getCLIPath(), cmdOptions, { cwd, stripColors: true });
chain
.wait("Choose the type of app that you're building")
.sendCarriageReturn()
.wait('What javascript framework are you using')
.sendCarriageReturn()
.wait('Choose the code generation language target').sendCarriageReturn()
.wait('Enter the file name pattern of graphql queries, mutations and subscriptions')
.sendCarriageReturn()
.wait('Do you want to generate/update all possible GraphQL operations')
.sendLine('y')
.wait('Enter maximum statement depth [increase from default if your schema is deeply')
.sendCarriageReturn();
const chain = spawn(getCLIPath(), ['codegen', 'add', ...params], { cwd, stripColors: true });

if (initialFailureMessage) {
chain.wait(initialFailureMessage)
} else {
chain
.wait("Choose the type of app that you're building")
.sendCarriageReturn()
.wait('What javascript framework are you using')
.sendCarriageReturn()
.wait('Choose the code generation language target').sendCarriageReturn()
.wait('Enter the file name pattern of graphql queries, mutations and subscriptions')
.sendCarriageReturn()
.wait('Do you want to generate/update all possible GraphQL operations')
.sendLine('y')
.wait('Enter maximum statement depth [increase from default if your schema is deeply')
.sendCarriageReturn();
}

chain.run((err: Error) => {
if (!err) {
Expand All @@ -182,3 +230,31 @@ export function addCodegenNonAmplifyJS(cwd: string): Promise<void> {
});
});
}

export function addCodegenNonAmplifyTS(cwd: string, params: Array<string>, initialFailureMessage?: string): Promise<void> {
return new Promise((resolve, reject) => {
const chain = spawn(getCLIPath(), ['codegen', 'add', ...params], { cwd, stripColors: true });

if (initialFailureMessage) {
chain.wait(initialFailureMessage)
} else {
chain
.wait("Choose the type of app that you're building").sendCarriageReturn()
.wait('What javascript framework are you using').sendCarriageReturn()
.wait('Choose the code generation language target').sendKeyDown().sendCarriageReturn()
.wait('Enter the file name pattern of graphql queries, mutations and subscriptions').sendCarriageReturn()
.wait('Do you want to generate/update all possible GraphQL operations').sendLine('y')
.wait('Enter maximum statement depth [increase from default if your schema is deeply').sendCarriageReturn()
.wait('Enter the file name for the generated code').sendCarriageReturn()
.wait('Do you want to generate code for your newly created GraphQL API').sendCarriageReturn();
}

chain.run((err: Error) => {
if (!err) {
resolve();
} else {
reject(err);
}
});
});
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export enum AmplifyFrontend {
javascript = 'javascript',
typescript = 'typescript',
ios = 'ios',
android = 'android',
flutter = 'flutter'
Expand Down
11 changes: 11 additions & 0 deletions packages/amplify-codegen-e2e-tests/schemas/sdl/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type Query {
echo: String
}

type Mutation {
mymutation: String
}

type Subscription {
mysub: String
}
Loading