Skip to content

Commit 957169c

Browse files
authored
Fix add codegen from a non-amplify application (#607)
* fix: check without init case * test: fix unit test * fix: add codegen test setup * test: refactor test helper * test: fix prompts * test: minor cleanup
1 parent 21bbff8 commit 957169c

File tree

5 files changed

+68
-12
lines changed

5 files changed

+68
-12
lines changed

packages/amplify-codegen-e2e-core/src/categories/codegen.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,31 @@ export function generateModelIntrospection(cwd: string, settings: { outputDir?:
147147
});
148148
}
149149

150+
// CLI workflow to add codegen to non-Amplify JS project
151+
export function addCodegenNonAmplifyJS(cwd: string): Promise<void> {
152+
return new Promise((resolve, reject) => {
153+
const cmdOptions = ['codegen', 'add', '--apiId', 'mockapiid'];
154+
const chain = spawn(getCLIPath(), cmdOptions, { cwd, stripColors: true });
155+
chain
156+
.wait("Choose the type of app that you're building")
157+
.sendCarriageReturn()
158+
.wait('What javascript framework are you using')
159+
.sendCarriageReturn()
160+
.wait('Choose the code generation language target').sendCarriageReturn()
161+
.wait('Enter the file name pattern of graphql queries, mutations and subscriptions')
162+
.sendCarriageReturn()
163+
.wait('Do you want to generate/update all possible GraphQL operations')
164+
.sendLine('y')
165+
.wait('Enter maximum statement depth [increase from default if your schema is deeply')
166+
.sendCarriageReturn();
167+
168+
chain.run((err: Error) => {
169+
if (!err) {
170+
resolve();
171+
} else {
172+
reject(err);
173+
}
174+
});
175+
});
176+
}
177+

packages/amplify-codegen-e2e-tests/src/__tests__/add-codegen-js.test.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import {
55
DEFAULT_JS_CONFIG,
66
createRandomName,
77
addApiWithoutSchema,
8-
updateApiSchema
8+
updateApiSchema,
9+
addCodegenNonAmplifyJS
910
} from "@aws-amplify/amplify-codegen-e2e-core";
10-
import { existsSync } from "fs";
11+
import { existsSync, writeFileSync } from "fs";
1112
import path from 'path';
1213
import { isNotEmptyDir } from '../utils';
1314
import { deleteAmplifyProject, testAddCodegen, testSetupBeforeAddCodegen,
@@ -72,4 +73,36 @@ describe('codegen add tests - JS', () => {
7273
it(`Adding codegen works as expected`, async () => {
7374
await testAddCodegen(config, projectRoot, schema);
7475
});
76+
77+
it(`Adding codegen outside of Amplify project`, async () => {
78+
// init project and add API category
79+
const testSchema = `
80+
type Query {
81+
echo: String
82+
}
83+
84+
type Mutation {
85+
mymutation: String
86+
}
87+
88+
type Subscription {
89+
mysub: String
90+
}
91+
`;
92+
93+
// Setup the non-amplify project with schema and pre-existing files
94+
const userSourceCodePath = testSetupBeforeAddCodegen(projectRoot, config);
95+
const schemaPath = path.join(projectRoot, 'schema.graphql');
96+
writeFileSync(schemaPath, testSchema);
97+
98+
// add codegen without init
99+
await expect(addCodegenNonAmplifyJS(projectRoot)).resolves.not.toThrow();
100+
101+
// pre-existing file should still exist
102+
expect(existsSync(userSourceCodePath)).toBe(true);
103+
// GraphQL statements are generated
104+
expect(isNotEmptyDir(path.join(projectRoot, config.graphqlCodegenDir))).toBe(true);
105+
// graphql configuration should be added
106+
expect(existsSync(getGraphQLConfigFilePath(projectRoot))).toBe(true);
107+
});
75108
});

packages/amplify-codegen/src/codegen-config/AmplifyCodeGenConfig.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,13 @@ const fs = require('fs-extra');
77
class AmplifyCodeGenConfig {
88
static configFileName = '.graphqlconfig.yml';
99

10-
constructor(projectPath, withoutInit = false) {
10+
constructor(projectPath) {
1111
try {
1212
this.gqlConfig = graphQLConfig.getGraphQLConfig();
1313
this.fixOldConfig();
1414
} catch (e) {
1515
if (e instanceof graphQLConfig.ConfigNotFoundError) {
16-
let projectRoot;
17-
if (!withoutInit) {
18-
projectRoot = projectPath || process.cwd();
19-
} else {
20-
projectRoot = process.cwd();
21-
}
16+
const projectRoot = projectPath || process.cwd();
2217
const configPath = join(projectRoot, '.graphqlconfig.yml');
2318
if(fs.existsSync(configPath)) {
2419
this.gqlConfig = graphQLConfig.getGraphQLConfig(projectRoot);

packages/amplify-codegen/src/codegen-config/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ let config = null;
66

77
function loadConfig(context, withoutInit = false) {
88
if (!config) {
9-
const projectPath = context.amplify.getEnvInfo().projectPath;
10-
config = new AmplifyCodeGenConfig(projectPath, withoutInit);
9+
const projectPath = withoutInit ? undefined : context.amplify.getEnvInfo().projectPath;
10+
config = new AmplifyCodeGenConfig(projectPath);
1111
}
1212
return config;
1313
}

packages/amplify-codegen/tests/codegen-config/index.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const MOCK_CONTEXT = {
1313
describe('codegen-config', () => {
1414
it('is singleton', () => {
1515
loadConfig(MOCK_CONTEXT);
16-
expect(AmplifyCodeGenConfig).toHaveBeenCalledWith(MOCK_PROJECT_ROOT, false);
16+
expect(AmplifyCodeGenConfig).toHaveBeenCalledWith(MOCK_PROJECT_ROOT);
1717
loadConfig(MOCK_CONTEXT);
1818
expect(AmplifyCodeGenConfig).toHaveBeenCalledTimes(1);
1919
});

0 commit comments

Comments
 (0)