Skip to content

Commit a901362

Browse files
author
Dane Pilcher
authored
fix: multiple swift file names (#713)
1 parent 756c375 commit a901362

File tree

2 files changed

+68
-33
lines changed

2 files changed

+68
-33
lines changed

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

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const constants = require('../constants');
77
const { loadConfig } = require('../codegen-config');
88
const { ensureIntrospectionSchema, getFrontEndHandler, getAppSyncAPIDetails, getAppSyncAPIInfoFromProject } = require('../utils');
99
const { generateTypes: generateTypesHelper } = require('@aws-amplify/graphql-generator');
10-
const { extractDocumentFromJavascript } = require('@aws-amplify/graphql-types-generator');
10+
const { generate, extractDocumentFromJavascript } = require('@aws-amplify/graphql-types-generator');
1111

1212
async function generateTypes(context, forceDownloadSchema, withoutInit = false, decoupleFrontend = '') {
1313
let frontend = decoupleFrontend;
@@ -57,25 +57,24 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false,
5757
const target = cfg.amplifyExtension.codeGenTarget;
5858

5959
const excludes = cfg.excludes.map(pattern => `!${pattern}`);
60-
const queryFiles = glob
61-
.sync([...includeFiles, ...excludes], {
62-
cwd: projectPath,
63-
absolute: true,
64-
})
65-
.map(queryFilePath => {
66-
const fileContents = fs.readFileSync(queryFilePath, 'utf8');
67-
if (
68-
queryFilePath.endsWith('.jsx') ||
69-
queryFilePath.endsWith('.js') ||
70-
queryFilePath.endsWith('.tsx') ||
71-
queryFilePath.endsWith('.ts')
72-
) {
73-
return extractDocumentFromJavascript(fileContents, '');
74-
}
75-
return fileContents;
76-
});
60+
const queryFilePaths = glob.sync([...includeFiles, ...excludes], {
61+
cwd: projectPath,
62+
absolute: true,
63+
});
64+
const queryFiles = queryFilePaths.map(queryFilePath => {
65+
const fileContents = fs.readFileSync(queryFilePath, 'utf8');
66+
if (
67+
queryFilePath.endsWith('.jsx') ||
68+
queryFilePath.endsWith('.js') ||
69+
queryFilePath.endsWith('.tsx') ||
70+
queryFilePath.endsWith('.ts')
71+
) {
72+
return extractDocumentFromJavascript(fileContents, '');
73+
}
74+
return fileContents;
75+
});
7776
if (queryFiles.length === 0) {
78-
throw new Error('No queries found to generate types for, you may need to run \'codegen statements\' first');
77+
throw new Error("No queries found to generate types for, you may need to run 'codegen statements' first");
7978
}
8079
const queries = queryFiles.join('\n');
8180

@@ -96,22 +95,30 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false,
9695
const introspection = path.extname(schemaPath) === '.json';
9796

9897
try {
99-
const output = await generateTypesHelper({
100-
schema,
101-
queries,
102-
target,
103-
introspection,
104-
});
105-
const outputs = Object.entries(output);
106-
107-
const outputPath = path.join(projectPath, generatedFileName);
108-
if (outputs.length === 1) {
109-
const [[, contents]] = outputs;
110-
fs.outputFileSync(path.resolve(outputPath), contents);
98+
if (target === 'swift' && fs.existsSync(outputPath) && fs.statSync(outputPath).isDirectory()) {
99+
generate(queryFilePaths, schemaPath, outputPath, '', target, '', {
100+
addTypename: true,
101+
complexObjectSupport: 'auto',
102+
});
111103
} else {
112-
outputs.forEach(([filepath, contents]) => {
113-
fs.outputFileSync(path.resolve(path.join(outputPath, filepath)), contents);
104+
const output = await generateTypesHelper({
105+
schema,
106+
queries,
107+
target,
108+
introspection,
109+
multipleSwiftFiles: false,
114110
});
111+
const outputs = Object.entries(output);
112+
113+
const outputPath = path.join(projectPath, generatedFileName);
114+
if (outputs.length === 1) {
115+
const [[, contents]] = outputs;
116+
fs.outputFileSync(path.resolve(outputPath), contents);
117+
} else {
118+
outputs.forEach(([filepath, contents]) => {
119+
fs.outputFileSync(path.resolve(path.join(outputPath, filepath)), contents);
120+
});
121+
}
115122
}
116123
codeGenSpinner.succeed(`${constants.INFO_MESSAGE_CODEGEN_GENERATE_SUCCESS} ${path.relative(path.resolve('.'), outputPath)}`);
117124
} catch (err) {

packages/amplify-codegen/tests/commands/types.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { sync } = require('glob-all');
22
const path = require('path');
33
const { generateTypes: generateTypesHelper } = require('@aws-amplify/graphql-generator');
4+
const { generate: legacyGenerate } = require('@aws-amplify/graphql-types-generator');
45
const fs = require('fs-extra');
56

67
const { loadConfig } = require('../../src/codegen-config');
@@ -20,6 +21,7 @@ const MOCK_CONTEXT = {
2021

2122
jest.mock('glob-all');
2223
jest.mock('@aws-amplify/graphql-generator');
24+
jest.mock('@aws-amplify/graphql-types-generator');
2325
jest.mock('../../src/codegen-config');
2426
jest.mock('../../src/utils');
2527
jest.mock('fs-extra');
@@ -81,9 +83,35 @@ describe('command - types', () => {
8183
schema: 'schema',
8284
target: 'TYPE_SCRIPT_OR_FLOW_OR_ANY_OTHER_LANGUAGE',
8385
introspection: false,
86+
multipleSwiftFiles: false,
8487
});
8588
});
8689

90+
it('should use legacy types generation when generating multiple swift files', async () => {
91+
MOCK_PROJECT.amplifyExtension.codeGenTarget = 'swift';
92+
MOCK_PROJECT.amplifyExtension.generatedFileName = 'typesDirectory';
93+
const forceDownload = false;
94+
fs.readFileSync
95+
.mockReturnValueOnce('query 1')
96+
.mockReturnValueOnce('query 2')
97+
.mockReturnValueOnce('schema');
98+
fs.existsSync.mockReturnValueOnce(true);
99+
fs.statSync.mockReturnValueOnce({
100+
isDirectory: jest.fn().mockReturnValue(true),
101+
});
102+
await generateTypes(MOCK_CONTEXT, forceDownload);
103+
expect(generateTypesHelper).not.toHaveBeenCalled();
104+
expect(legacyGenerate).toHaveBeenCalledWith(
105+
['q1.gql', 'q2.gql'],
106+
'MOCK_PROJECT_ROOT/INTROSPECTION_SCHEMA.JSON',
107+
'MOCK_PROJECT_ROOT/typesDirectory',
108+
'',
109+
'swift',
110+
'',
111+
{ addTypename: true, complexObjectSupport: 'auto' },
112+
);
113+
});
114+
87115
it('should not generate type if the frontend is android', async () => {
88116
const forceDownload = false;
89117
getFrontEndHandler.mockReturnValue('android');

0 commit comments

Comments
 (0)