Skip to content

Commit ee4f11b

Browse files
authored
Merge pull request #598 from aws-amplify/main
chore(release): Amplify Codegen Plugin
2 parents f96e002 + 60e00fc commit ee4f11b

27 files changed

+227
-38
lines changed

packages/amplify-codegen-e2e-core/src/init/amplifyPull.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ function initializeFrontend(chain: ExecutionContext, config: AmplifyFrontendConf
116116
switch (config.frontendType) {
117117
case AmplifyFrontend.android:
118118
chain
119-
.send('j')
120-
.sendCarriageReturn()
119+
.sendLine('android')
121120
.wait('Where is your Res directory')
122121
.sendCarriageReturn()
123122
return;

packages/amplify-codegen-e2e-core/src/init/initProjectHelper.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ export function initAndroidProjectWithProfile(cwd: string, settings: Object): Pr
108108
.wait('Choose your default editor:')
109109
.sendLine(s.editor)
110110
.wait("Choose the type of app that you're building")
111-
.send('j')
112-
.sendCarriageReturn()
111+
.sendLine('android')
113112
.wait('Where is your Res directory')
114113
.sendLine(s.srcDir)
115114
.wait('Select the authentication method you want to use:')
@@ -190,8 +189,7 @@ export function initFlutterProjectWithProfile(cwd: string, settings: Object): Pr
190189
.wait('Choose your default editor:')
191190
.sendLine(s.editor)
192191
.wait("Choose the type of app that you're building")
193-
.sendKeyDown(2)
194-
.sendCarriageReturn()
192+
.sendLine('flutter')
195193
.wait('Where do you want to store your configuration file')
196194
.sendLine(s.srcDir)
197195
.wait('Using default provider awscloudformation')

packages/amplify-codegen-e2e-tests/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"graphql-tag": "^2.10.1",
3535
"lodash": "^4.17.19",
3636
"uuid": "^3.4.0",
37-
"yargs": "^15.1.0"
37+
"yargs": "^15.1.0",
38+
"js-yaml": "^4.0.0"
3839
},
3940
"devDependencies": {
4041
"@types/jest": "^27.0.0",

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ import {
1717
getAdminApp,
1818
amplifyPullSandbox,
1919
getProjectSchema,
20+
AmplifyFrontend,
2021
} from '@aws-amplify/amplify-codegen-e2e-core';
2122
import { existsSync } from 'fs';
2223
import path from 'path';
2324
import { isNotEmptyDir, generateSourceCode } from '../utils';
2425
import { JSONUtilities } from '@aws-amplify/amplify-cli-core';
2526
import { SandboxApp } from '../types/SandboxApp';
27+
import { createPubspecLockFile } from '../codegen-tests-base';
2628

2729
const schema = 'simple_model.graphql';
2830
const envName = 'pulltest';
@@ -72,6 +74,10 @@ describe('Amplify pull in amplify app with codegen tests', () => {
7274
it(`should generate models and do not delete user files by amplify pull in an empty folder of ${config.frontendType} app`, async () => {
7375
//generate pre existing user file
7476
const userSourceCodePath = generateSourceCode(emptyProjectRoot, config.srcDir);
77+
// Flutter projects need min dart version to be met for modelgen to succeed.
78+
if (config?.frontendType === AmplifyFrontend.flutter) {
79+
createPubspecLockFile(emptyProjectRoot);
80+
};
7581
//amplify pull in a new project
7682
await amplifyPull(emptyProjectRoot, { emptyDir: true, appId, frontendConfig: config });
7783
expect(existsSync(userSourceCodePath)).toBe(true);
@@ -110,6 +116,10 @@ describe('Amplify pull in sandbox app with codegen tests', () => {
110116
it(`should pull sandbox, download schema and generate models without deleting user files in ${config.frontendType} project`, async () => {
111117
//generate pre existing user file
112118
const userSourceCodePath = generateSourceCode(projectRoot, config.srcDir);
119+
// Flutter projects need min dart version to be met for modelgen to succeed.
120+
if (config?.frontendType === AmplifyFrontend.flutter) {
121+
createPubspecLockFile(projectRoot);
122+
};
113123
//pull sandbox app
114124
await amplifyPullSandbox(projectRoot, {
115125
appType: config.frontendType,

packages/amplify-codegen-e2e-tests/src/codegen-tests-base/datastore-modelgen.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import {
44
updateApiSchema,
55
createRandomName,
66
generateModels,
7-
AmplifyFrontendConfig
7+
AmplifyFrontendConfig,
8+
AmplifyFrontend
89
} from '@aws-amplify/amplify-codegen-e2e-core';
9-
import { existsSync } from "fs";
10+
import { existsSync, writeFileSync } from "fs";
1011
import path from 'path';
1112
import { isNotEmptyDir, generateSourceCode } from '../utils';
13+
const yaml = require('js-yaml');
1214

1315
export async function testCodegenModels(config: AmplifyFrontendConfig, projectRoot: string, schema: string, outputDir?: string) {
1416
const name = createRandomName();
@@ -23,14 +25,34 @@ export async function testCodegenModels(config: AmplifyFrontendConfig, projectRo
2325
//generate pre existing user file
2426
const userSourceCodePath = generateSourceCode(projectRoot, config.srcDir);
2527

28+
// For flutter frontend, we need to have a pubspec lock file with supported dart version
29+
if (config?.frontendType === AmplifyFrontend.flutter) {
30+
createPubspecLockFile(projectRoot);
31+
}
32+
2633
//generate models
2734
await expect(generateModels(projectRoot, outputDir)).resolves.not.toThrow();
2835

2936
// pre-existing file should still exist
3037
expect(existsSync(userSourceCodePath)).toBe(true);
38+
3139
// datastore models are generated at correct location
3240
const dirToCheck = outputDir
3341
? path.join(projectRoot, outputDir)
3442
: path.join(projectRoot, config.modelgenDir);
3543
expect(isNotEmptyDir(dirToCheck)).toBe(true);
3644
}
45+
46+
export const createPubspecLockFile = (projectRoot: string) => {
47+
const lockFile = {
48+
packages: {
49+
amplify_flutter: {
50+
version: '2.0.0'
51+
},
52+
},
53+
};
54+
const pubspecPath = path.join(projectRoot, 'pubspec.lock');
55+
if (!existsSync(pubspecPath)) {
56+
writeFileSync(pubspecPath, yaml.dump(lockFile));
57+
}
58+
};

packages/amplify-codegen/src/callbacks/postPushCallback.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const path = require('path');
22
const { pathManager } = require('@aws-amplify/amplify-cli-core');
33

4-
const loadConfig = require('../codegen-config');
4+
const { loadConfig } = require('../codegen-config');
55
const generateStatements = require('../commands/statements');
66
const generateTypes = require('../commands/types');
77
const generateModels = require('../commands/models');

packages/amplify-codegen/src/callbacks/prePushUpdateCallback.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { normalizeInputParams } = require('../utils/input-params-manager');
22
const constants = require('../constants');
3-
const loadConfig = require('../codegen-config');
3+
const { loadConfig } = require('../codegen-config');
44
const askShouldUpdateCode = require('../walkthrough/questions/updateCode');
55
const askShouldUpdateDocs = require('../walkthrough/questions/updateDocs');
66
const path = require('path');

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
const graphQLConfig = require('graphql-config');
2-
const { join, isAbsolute, relative } = require('path');
2+
const { isAbsolute, relative } = require('path');
33
const slash = require('slash');
44
const { graphQlToAmplifyConfig } = require('./utils');
55

66
class AmplifyCodeGenConfig {
7-
constructor(context, withoutInit = false) {
7+
static configFileName = '.graphqlconfig.yml';
8+
9+
constructor(projectPath, withoutInit = false) {
810
try {
911
this.gqlConfig = graphQLConfig.getGraphQLConfig();
1012
this.fixOldConfig();
1113
} catch (e) {
1214
if (e instanceof graphQLConfig.ConfigNotFoundError) {
13-
const { amplify } = context;
1415
let projectRoot;
1516
if (!withoutInit) {
16-
projectRoot = amplify.getEnvInfo().projectPath || process.cwd();
17+
projectRoot = projectPath || process.cwd();
1718
} else {
1819
projectRoot = process.cwd();
1920
}
20-
const configPath = join(projectRoot, '.graphqlconfig.yml');
21-
this.gqlConfig = new graphQLConfig.GraphQLConfig(null, configPath);
22-
this.gqlConfig.config = {};
21+
this.gqlConfig = graphQLConfig.getGraphQLConfig(projectRoot);
2322
} else {
2423
throw e;
2524
}
Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,69 @@
11
const AmplifyCodeGenConfig = require('./AmplifyCodeGenConfig');
2+
const fs = require('fs-extra');
3+
const path = require('path');
24

35
let config = null;
46

57
function loadConfig(context, withoutInit = false) {
68
if (!config) {
7-
config = new AmplifyCodeGenConfig(context, withoutInit);
9+
const projectPath = context.amplify.getEnvInfo().projectPath;
10+
config = new AmplifyCodeGenConfig(projectPath, withoutInit);
811
}
912
return config;
1013
}
1114

12-
module.exports = loadConfig;
15+
function getCodegenConfig(projectPath) {
16+
if (!projectPath) {
17+
throw new Error('Invalid projectPath provided to get codegen configuration');
18+
}
19+
20+
if (!fs.existsSync(projectPath)) {
21+
throw new Error(`Provided projectPath for getting the codegen configuration does not exist: ${projectPath}`);
22+
}
23+
24+
if (!fs.existsSync(path.join(projectPath, AmplifyCodeGenConfig.configFileName))) {
25+
throw new Error(`Cannot find the codegen configuration file at provided projectPath: ${projectPath}`);
26+
}
27+
28+
const codegenConfig = new AmplifyCodeGenConfig(projectPath);
29+
if (!codegenConfig) {
30+
throw new Error(`Fetched codegen configuration is empty: ${codegenConfig}`);
31+
}
32+
33+
const projects = codegenConfig.getProjects();
34+
if (!projects.length > 0) {
35+
throw new Error(`No projects were found in fetched codegen configuration: ${codegenConfig}`);
36+
}
37+
38+
const cfg = projects[0];
39+
const includeFiles = cfg.includes[0];
40+
41+
const opsGenDirectory = cfg.amplifyExtension.docsFilePath
42+
? cfg.amplifyExtension.docsFilePath
43+
: path.dirname(path.dirname(includeFiles));
44+
45+
const { generatedFileName } = cfg.amplifyExtension || {};
46+
47+
return {
48+
getGeneratedQueriesPath: function() {
49+
return path.join(opsGenDirectory, 'queries');
50+
},
51+
getGeneratedMutationsPath: function() {
52+
return path.join(opsGenDirectory, 'mutations');
53+
},
54+
getGeneratedSubscriptionsPath: function() {
55+
return path.join(opsGenDirectory, 'subscriptions');
56+
},
57+
getGeneratedFragmentsPath: function() {
58+
return path.join(opsGenDirectory, 'fragments');
59+
},
60+
getGeneratedTypesPath: function() {
61+
if (!generatedFileName || generatedFileName === '') {
62+
return;
63+
}
64+
return path.normalize(generatedFileName);
65+
}
66+
}
67+
}
68+
69+
module.exports = { loadConfig, getCodegenConfig };

packages/amplify-codegen/src/commands/add.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const Ora = require('ora');
2-
const loadConfig = require('../codegen-config');
2+
const { loadConfig } = require('../codegen-config');
33
const constants = require('../constants');
44
const generateStatements = require('./statements');
55
const generateTypes = require('./types');

0 commit comments

Comments
 (0)