Skip to content

Commit 6df1cfc

Browse files
authored
Add support for output dir arg on models (#478)
* Add support for output dir arg on models * Fix a null check to be more careful * Check appropriately for provided output dir
1 parent ae2c24d commit 6df1cfc

File tree

8 files changed

+40
-18
lines changed

8 files changed

+40
-18
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { AmplifyFrontend } from '../utils';
22
import { getCLIPath, nspawn as spawn } from '..';
33

4-
export function generateModels(cwd: string): Promise<void> {
4+
export function generateModels(cwd: string, outputDir?: string): Promise<void> {
55
return new Promise((resolve, reject) => {
6-
spawn(getCLIPath(), ['codegen', 'models'], { cwd, stripColors: true })
6+
const params = ['codegen', 'models', ...(outputDir ? ['--output-dir', outputDir] : [])]
7+
spawn(getCLIPath(), params, { cwd, stripColors: true })
78
.run((err: Error) => {
89
if (!err) {
910
resolve();

packages/amplify-codegen-e2e-tests/src/__tests__/datastore-modelgen-android.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createNewProjectDir, DEFAULT_ANDROID_CONFIG } from "@aws-amplify/amplify-codegen-e2e-core";
1+
import { createNewProjectDir, DEFAULT_ANDROID_CONFIG } from '@aws-amplify/amplify-codegen-e2e-core';
22
import { deleteAmplifyProject, testCodegenModels } from '../codegen-tests-base';
33

44
const schema = 'modelgen/model_gen_schema_with_aws_scalars.graphql';
@@ -17,4 +17,8 @@ describe('Datastore Modelgen tests - Android', () => {
1717
it(`should generate files at desired location and not delete src files`, async () => {
1818
await testCodegenModels(DEFAULT_ANDROID_CONFIG, projectRoot, schema);
1919
});
20-
});
20+
21+
it('Should generate files at overridden output path', async () => {
22+
await testCodegenModels(DEFAULT_ANDROID_CONFIG, projectRoot, schema, 'app/src/main/guava');
23+
});
24+
});

packages/amplify-codegen-e2e-tests/src/__tests__/datastore-modelgen-flutter.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createNewProjectDir, DEFAULT_FLUTTER_CONFIG } from "@aws-amplify/amplify-codegen-e2e-core";
1+
import { createNewProjectDir, DEFAULT_FLUTTER_CONFIG } from '@aws-amplify/amplify-codegen-e2e-core';
22
import { deleteAmplifyProject, testCodegenModels } from '../codegen-tests-base';
33

44
const schema = 'modelgen/model_gen_schema_with_aws_scalars.graphql';
@@ -17,4 +17,8 @@ describe('Datastore Modelgen tests - Flutter', () => {
1717
it(`should generate files at desired location and not delete src files`, async () => {
1818
await testCodegenModels(DEFAULT_FLUTTER_CONFIG, projectRoot, schema);
1919
});
20-
});
20+
21+
it(`should generate files at overridden location`, async () => {
22+
await testCodegenModels(DEFAULT_FLUTTER_CONFIG, projectRoot, schema, 'lib/blueprints');
23+
});
24+
});

packages/amplify-codegen-e2e-tests/src/__tests__/datastore-modelgen-ios.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createNewProjectDir, DEFAULT_IOS_CONFIG } from "@aws-amplify/amplify-codegen-e2e-core";
1+
import { createNewProjectDir, DEFAULT_IOS_CONFIG } from '@aws-amplify/amplify-codegen-e2e-core';
22
import { deleteAmplifyProject, testCodegenModels } from '../codegen-tests-base';
33

44
const schema = 'modelgen/model_gen_schema_with_aws_scalars.graphql';
@@ -17,4 +17,8 @@ describe('Datastore Modelgen tests - iOS', () => {
1717
it(`should generate files at desired location and not delete src files`, async () => {
1818
await testCodegenModels(DEFAULT_IOS_CONFIG, projectRoot, schema);
1919
});
20-
});
20+
21+
it(`should generate files at overridden location`, async () => {
22+
await testCodegenModels(DEFAULT_IOS_CONFIG, projectRoot, schema, 'amplification/manufactured/models');
23+
});
24+
});

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createNewProjectDir, DEFAULT_JS_CONFIG } from "@aws-amplify/amplify-codegen-e2e-core";
1+
import { createNewProjectDir, DEFAULT_JS_CONFIG } from '@aws-amplify/amplify-codegen-e2e-core';
22
import { deleteAmplifyProject, testCodegenModels } from '../codegen-tests-base';
33

44
const schema = 'modelgen/model_gen_schema_with_aws_scalars.graphql';
@@ -17,4 +17,8 @@ describe('Datastore Modelgen tests - JS', () => {
1717
it(`should generate files at desired location and not delete src files`, async () => {
1818
await testCodegenModels(DEFAULT_JS_CONFIG, projectRoot, schema);
1919
});
20-
});
20+
21+
it(`should generate files at desired location and not delete src files`, async () => {
22+
await testCodegenModels(DEFAULT_JS_CONFIG, projectRoot, schema, 'src/backmodels');
23+
});
24+
});

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import {
55
createRandomName,
66
generateModels,
77
AmplifyFrontendConfig
8-
} from "@aws-amplify/amplify-codegen-e2e-core";
8+
} from '@aws-amplify/amplify-codegen-e2e-core';
99
import { existsSync } from "fs";
1010
import path from 'path';
1111
import { isNotEmptyDir, generateSourceCode } from '../utils';
1212

13-
export async function testCodegenModels(config: AmplifyFrontendConfig, projectRoot: string, schema: string) {
13+
export async function testCodegenModels(config: AmplifyFrontendConfig, projectRoot: string, schema: string, outputDir?: string) {
1414
const name = createRandomName();
1515

1616
// init project and add API category
@@ -24,10 +24,10 @@ export async function testCodegenModels(config: AmplifyFrontendConfig, projectRo
2424
const userSourceCodePath = generateSourceCode(projectRoot, config.srcDir);
2525

2626
//generate models
27-
await expect(generateModels(projectRoot)).resolves.not.toThrow();
27+
await expect(generateModels(projectRoot, outputDir)).resolves.not.toThrow();
2828

2929
// pre-existing file should still exist
3030
expect(existsSync(userSourceCodePath)).toBe(true);
3131
// datastore models are generated at correct location
32-
expect(isNotEmptyDir(path.join(projectRoot, config.modelgenDir))).toBe(true);
33-
}
32+
expect(isNotEmptyDir(outputDir ? outputDir : path.join(projectRoot, config.modelgenDir))).toBe(true);
33+
}

packages/amplify-codegen/src/commands/model-intropection.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ const path = require('path');
33

44
async function generateModelIntrospection(context) {
55
// Verify override path flag is provided
6-
const outputDirParam = context.parameters.options ? context.parameters.options['output-dir'] : null;
6+
const outputDirParam = context.parameters?.options?.['output-dir'];
77
if ( !outputDirParam || typeof(outputDirParam) !== 'string' ) {
88
throw new Error('Expected --output-dir flag with value to be set for model introspection command.');
99
}
1010
const outputDirPath = path.isAbsolute(outputDirParam) ? outputDirParam : path.join(context.amplify.getEnvInfo().projectPath, outputDirParam);
1111
await generateModels(context, outputDirPath, true);
1212
}
1313

14-
module.exports = generateModelIntrospection;
14+
module.exports = generateModelIntrospection;

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,12 @@ async function generateModels(context, outputDirPath = null, isIntrospection = f
7676
});
7777

7878
const schemaContent = loadSchema(apiResourcePath);
79-
const outputPath = outputDirPath || path.join(projectRoot, getModelOutputPath(context));
79+
80+
const outputDirParam = context.parameters?.options?.['output-dir'];
81+
if ( !outputDirPath && outputDirParam && typeof(outputDirParam) !== 'string' ) {
82+
throw new Error('Expected provided --output-dir flag to be given output location as input.');
83+
}
84+
const outputPath = outputDirPath ?? outputDirParam ?? path.join(projectRoot, getModelOutputPath(context));
8085
const schema = parse(schemaContent);
8186
const projectConfig = context.amplify.getProjectConfig();
8287

0 commit comments

Comments
 (0)