Skip to content

Commit

Permalink
feat: root stack override (#8276)
Browse files Browse the repository at this point in the history
* feat: added root stack transformation

* feat: adding root-stack-builder

* feat: added e2e and migration tests

* fix: minor fixes

* fix: fixes unit tests

* fix: address comments
  • Loading branch information
akshbhu authored and sachscode committed Nov 11, 2021
1 parent 75a942f commit 9e574ba
Show file tree
Hide file tree
Showing 41 changed files with 1,801 additions and 296 deletions.
24 changes: 24 additions & 0 deletions .circleci/config.base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,30 @@ jobs:
path: ~/repo/packages/amplify-migration-tests/amplify-migration-reports
working_directory: ~/repo

amplify_migration_tests_overrides:
<<: *defaults
environment:
AMPLIFY_PATH: /home/circleci/.npm-global/lib/node_modules/@aws-amplify/cli/bin/amplify
steps:
- attach_workspace:
at: ./
- restore_cache:
key: amplify-cli-yarn-deps-{{ .Branch }}-{{ checksum "yarn.lock" }}
- run:
name: Run tests migrating from CLI v6.0.1
command: |
source .circleci/local_publish_helpers.sh
changeNpmGlobalPath
cd packages/amplify-migration-tests
yarn run migration_v6.0.1 --maxWorkers=3 $TEST_SUITE
no_output_timeout: 90m
- run: *scan_e2e_test_artifacts
- store_test_results:
path: packages/amplify-migration-tests/
- store_artifacts:
path: ~/repo/packages/amplify-migration-tests/amplify-migration-reports
working_directory: ~/repo

amplify_migration_tests_v4_30_0:
<<: *defaults
environment:
Expand Down
4 changes: 2 additions & 2 deletions packages/amplify-cli-core/src/feature-flags/featureFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -724,13 +724,13 @@ export class FeatureFlags {
name: 'auth',
type: 'boolean',
defaultValueForExistingProjects: false,
defaultValueForNewProjects: false,
defaultValueForNewProjects: true,
},
{
name: 'project',
type: 'boolean',
defaultValueForExistingProjects: false,
defaultValueForNewProjects: false,
defaultValueForNewProjects: true,
},
]);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs-extra';
import { $TSContext, getPackageManager } from '../index';
import execa from 'execa';
import * as path from 'path';
import { printer } from 'amplify-prompts';
import { printer, prompter } from 'amplify-prompts';
import { JSONUtilities } from '../jsonUtilities';

export const generateOverrideSkeleton = async (context: $TSContext, srcResourceDirPath: string, destDirPath: string): Promise<void> => {
Expand All @@ -25,10 +25,13 @@ export const generateOverrideSkeleton = async (context: $TSContext, srcResourceD
await buildOverrideDir(backendDir, destDirPath);

printer.success(`Successfully generated "override.ts" folder at ${destDirPath}`);
await context.amplify.openEditor(context, overrideFile);
const isOpen = await prompter.confirmContinue('Do you want to edit override.ts file now?');
if (isOpen) {
await context.amplify.openEditor(context, overrideFile);
}
};

export async function buildOverrideDir(cwd: string, destDirPath: string) {
export async function buildOverrideDir(cwd: string, destDirPath: string): Promise<boolean> {
const packageManager = getPackageManager(cwd);

if (packageManager === null) {
Expand All @@ -41,35 +44,37 @@ export async function buildOverrideDir(cwd: string, destDirPath: string) {
stdio: 'pipe',
encoding: 'utf-8',
});
// run tsc build to build override.ts file
const tsConfigDir = path.join(destDirPath, 'build');
const tsConfigFilePath = path.join(tsConfigDir, 'tsconfig.resource.json');
execa.sync('tsc', [`--project`, `${tsConfigFilePath}`], {
cwd: tsConfigDir,
stdio: 'pipe',
encoding: 'utf-8',
});
return true;
} catch (error) {
if ((error as any).code === 'ENOENT') {
throw new Error(`Packaging overrides failed. Could not find ${packageManager} executable in the PATH.`);
} else {
throw new Error(`Packaging overrides failed with the error \n${error.message}`);
}
}

// run tsc build to build override.ts file
const tsConfigDir = path.join(destDirPath, 'build');
const tsConfigFilePath = path.join(tsConfigDir, 'tsconfig.resource.json');
execa.sync('tsc', [`--project`, `${tsConfigFilePath}`], {
cwd: tsConfigDir,
stdio: 'pipe',
encoding: 'utf-8',
});
}

export const generateAmplifyOverrideProjectBuildFiles = (backendDir: string, srcResourceDirPath: string) => {
const packageJSONFilePath = path.join(backendDir, 'package.json');
const tsConfigFilePath = path.join(backendDir, 'tsconfig.json');
// add package.json to amplofy backend
if (!fs.existsSync(packageJSONFilePath)) {
JSONUtilities.writeJson(packageJSONFilePath, JSONUtilities.readJson(path.join(srcResourceDirPath, 'package.json')));
const packageJson = JSONUtilities.readJson(path.join(srcResourceDirPath, 'package.json'));
JSONUtilities.writeJson(packageJSONFilePath, packageJson);
}

// add tsConfig.json to amplify backend
if (!fs.existsSync(tsConfigFilePath)) {
JSONUtilities.writeJson(tsConfigFilePath, JSONUtilities.readJson(path.join(srcResourceDirPath, 'tsconfig.json')));
const tsConfigJson = JSONUtilities.readJson(path.join(srcResourceDirPath, 'tsconfig.json'));
JSONUtilities.writeJson(tsConfigFilePath, tsConfigJson);
}
};

Expand Down
2 changes: 1 addition & 1 deletion packages/amplify-cli-core/src/state-manager/pathManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export class PathManager {
]);
};

getRootStackDirPath = (projectPath: string): string => {
getRootStackBuildDirPath = (projectPath: string): string => {
return this.constructPath(projectPath, [
PathConstants.AmplifyDirName,
PathConstants.BackendDirName,
Expand Down
3 changes: 2 additions & 1 deletion packages/amplify-cli-overrides-helper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"clean": "rimraf ./lib"
},
"dependencies": {
"amplify-prompts": "1.1.2"
"amplify-prompts": "1.1.2",
"amplify-provider-awscloudformation": "4.60.1"
},
"devDependencies": {},
"jest": {
Expand Down
2 changes: 0 additions & 2 deletions packages/amplify-cli-overrides-helper/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import { printer } from 'amplify-prompts';

function getProjectInfo(): void {
printer.info('Hello from the skeleton of get project info');
return;
}

function addDependency(): void {
printer.info('Hello from the skeleton of add dependency');
return;
}

export { getProjectInfo, addDependency };
8 changes: 6 additions & 2 deletions packages/amplify-cli-overrides-helper/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"compilerOptions": {
"outDir": "./lib",
"rootDir": "./src",
}
}
"module": "ESNext"
},
"references": [
{ "path": "../amplify-provider-awscloudformation" },
]
}

94 changes: 90 additions & 4 deletions packages/amplify-cli/src/__tests__/commands/build-override.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jest.mock('amplify-cli-core');
jest.mock('amplify-provider-awscloudformation');

describe('run build-override command', () => {
it('runs command successfully', async () => {
it('runs override command for only a resource', async () => {
const context_stub = {
amplify: {
getResourceStatus: jest.fn().mockResolvedValue({
Expand Down Expand Up @@ -38,15 +38,100 @@ describe('run build-override command', () => {
confirmPrompt: jest.fn().mockResolvedValue(true),
invokePluginMethod: jest.fn(),
},
input: {
subCommands: ['mockcategory1', 'mockResourceName1'],
},
};

const context_stub_typed = context_stub as unknown as $TSContext;
await run(context_stub_typed);
expect(context_stub_typed.amplify.invokePluginMethod).toBeCalledTimes(1);
});

it('runs override command for only all resources in a category', async () => {
const context_stub = {
amplify: {
getResourceStatus: jest.fn().mockResolvedValue({
resourcesToBeCreated: [
{
category: 'mockcategory1',
service: 'mockservice1',
resourceName: 'mockResourceName1',
},
{
category: 'mockcategory1',
service: 'mockservice2',
resourceName: 'mockResourceName2',
},
],
resourcesToBeUpdated: [
{
category: 'mockcategory1',
service: 'mockservice3',
resourceName: 'mockResourceName3',
},
{
category: 'mockcategory4',
service: 'mockservice4',
resourceName: 'mockResourceName4',
},
],
}),

confirmPrompt: jest.fn().mockResolvedValue(true),
invokePluginMethod: jest.fn(),
},
input: {
subCommands: ['mockcategory1'],
},
};

const context_stub_typed = context_stub as unknown as $TSContext;
await run(context_stub_typed);
expect(context_stub_typed.amplify.invokePluginMethod).toBeCalledTimes(4);
expect(context_stub_typed.amplify.invokePluginMethod).toBeCalledTimes(3);
});

it('runs override command successfully for all resources in all categories', async () => {
const context_stub = {
amplify: {
getResourceStatus: jest.fn().mockResolvedValue({
resourcesToBeCreated: [
{
category: 'mockcategory1',
service: 'mockservice1',
resourceName: 'mockResourceName1',
},
{
category: 'mockcategory2',
service: 'mockservice2',
resourceName: 'mockResourceName2',
},
],
resourcesToBeUpdated: [
{
category: 'mockcategory3',
service: 'mockservice3',
resourceName: 'mockResourceName3',
},
{
category: 'mockcategory4',
service: 'mockservice4',
resourceName: 'mockResourceName4',
},
],
}),

confirmPrompt: jest.fn().mockResolvedValue(true),
invokePluginMethod: jest.fn(),
},
};

const context_stub_typed = context_stub as unknown as $TSContext;
await run(context_stub_typed);
expect(context_stub_typed.amplify.invokePluginMethod).toBeCalledTimes(5);
});

it('runs command successfully empty Arrays', async () => {
jest.clearAllMocks();
const context_stub = {
amplify: {
getResourceStatus: jest.fn().mockResolvedValue({
Expand All @@ -55,11 +140,12 @@ describe('run build-override command', () => {
}),

confirmPrompt: jest.fn().mockResolvedValue(true),
invokePluginMethod: jest.fn(),
},
};

const context_stub_typed = context_stub as unknown as $TSContext;
await run(context_stub_typed);
expect(context_stub_typed.amplify.invokePluginMethod).toBeUndefined();
expect(context_stub_typed.amplify.invokePluginMethod).toBeCalledTimes(1);
});
});
Loading

0 comments on commit 9e574ba

Please sign in to comment.