Skip to content

Commit bc73678

Browse files
authored
Merge pull request #594 from aws-amplify/add-codegen-config-apis
feat: add codegen config related apis
2 parents f96e002 + 8e3544d commit bc73678

23 files changed

+190
-33
lines changed

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/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');

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const loadConfig = require('../codegen-config');
1+
const { loadConfig } = require('../codegen-config');
22
const configureProjectWalkThrough = require('../walkthrough/configure');
33
const add = require('./add');
44

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { join } = require('path');
22
const { AmplifyCodeGenNoAppSyncAPIAvailableError: NoAppSyncAPIAvailableError } = require('../errors');
33
const generateTypes = require('./types');
44
const generateStatements = require('./statements');
5-
const loadConfig = require('../codegen-config');
5+
const { loadConfig } = require('../codegen-config');
66
const constants = require('../constants');
77
const { ensureIntrospectionSchema, getAppSyncAPIDetails } = require('../utils');
88
const path = require('path');

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const inquirer = require('inquirer');
22

3-
const loadConfig = require('../codegen-config');
3+
const { loadConfig } = require('../codegen-config');
44
const constants = require('../constants');
55

66
async function remove(context) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const path = require('path');
22
const fs = require('fs-extra');
33
const Ora = require('ora');
44

5-
const loadConfig = require('../codegen-config');
5+
const { loadConfig } = require('../codegen-config');
66
const constants = require('../constants');
77
const { ensureIntrospectionSchema, getFrontEndHandler, getAppSyncAPIDetails, readSchemaFromFile, GraphQLStatementsFormatter } = require('../utils');
88
const { generateGraphQLDocuments } = require('@aws-amplify/graphql-docs-generator');

0 commit comments

Comments
 (0)