Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: keeps hooks intact when attaching backend #11179

Merged
merged 6 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/amplify-cli/src/attach-backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ export const attachBackend = async (context: $TSContext, inputParams): Promise<v

const onSuccess = async (context: $TSContext): Promise<void> => {
const { inputParams } = context.exeInfo;
const projectPath = process.cwd();
const backupAmplifyDirPath = path.join(projectPath, backupAmplifyDirName);

if (inputParams.amplify.noOverride) {
const projectPath = process.cwd();
const backupAmplifyDirPath = path.join(projectPath, backupAmplifyDirName);
// eslint-disable-next-line spellcheck/spell-checker
const backupBackendDirPath = path.join(backupAmplifyDirPath, context.amplify.constants.BackendAmplifyCLISubDirName);

Expand Down Expand Up @@ -100,7 +100,13 @@ const onSuccess = async (context: $TSContext): Promise<void> => {
} else if (stateManager.currentMetaFileExists()) {
await initializeEnv(context, stateManager.getCurrentMeta());
}

// move Hooks folder from backup to original amplify folder
const hooksDirPath = pathManager.getHooksDirPath(projectPath);
const hooksBackupDirPath = path.join(backupAmplifyDirPath, 'hooks');
// hooks folder shouldnt be present , if it is then we overrite with the given Customer folder from amplify backup
if (fs.existsSync(hooksBackupDirPath)) {
fs.moveSync(hooksBackupDirPath, hooksDirPath, { overwrite: true });
}
removeBackupAmplifyFolder();
};

Expand Down
7 changes: 5 additions & 2 deletions packages/amplify-e2e-core/src/utils/getAppId.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { getBackendAmplifyMeta } from './projectMeta';

export function getAppId(projRoot: string): string {
/**
* fetches appId from amplify meta
*/
export const getAppId = (projRoot: string): string => {
const meta = getBackendAmplifyMeta(projRoot);
return meta.providers.awscloudformation.AmplifyAppId;
}
};
43 changes: 43 additions & 0 deletions packages/amplify-e2e-tests/src/__tests__/hooks-a.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {
addFunction,
amplifyPullNonInteractive,
amplifyPushAuth,
createNewProjectDir,
deleteProject,
deleteProjectDir,
getBackendAmplifyMeta,
getHooksDirPath,
initJSProjectWithProfile,
transformCurrentProjectToGitPulledProject,
} from '@aws-amplify/amplify-e2e-core';
import * as fs from 'fs-extra';
import * as path from 'path';

describe('runtime hooks', () => {
let projRoot: string;
beforeEach(async () => {
projRoot = await createNewProjectDir('hooks');
});

afterEach(async () => {
await deleteProject(projRoot);
deleteProjectDir(projRoot);
});

it('hooks should not get deleted when pulling a project from git and running amplify pull', async () => {
await initJSProjectWithProfile(projRoot, { envName: 'staging', disableAmplifyAppCreation: false });
const appId = getBackendAmplifyMeta(projRoot)?.providers?.awscloudformation?.AmplifyAppId;
expect(appId).toBeDefined();
const hooksDirPath = getHooksDirPath(projRoot);
expect(fs.existsSync(hooksDirPath)).toBe(true);
fs.removeSync(path.join(hooksDirPath, 'pre-push.js.sample'));
fs.removeSync(path.join(hooksDirPath, 'post-push.sh.sample'));
fs.writeFileSync(path.join(hooksDirPath, 'pre-push.js'), `console.log('hello');`);
await addFunction(projRoot, { functionTemplate: 'Hello World' }, 'nodejs');
await amplifyPushAuth(projRoot);
// grab the appId from the meta file
transformCurrentProjectToGitPulledProject(projRoot);
await amplifyPullNonInteractive(projRoot, { appId, envName: 'staging' });
expect(fs.existsSync(hooksDirPath)).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
import {
addFunction,
amplifyPull,
amplifyPushWithNoChanges,
createNewProjectDir,
deleteProject,
deleteProjectDir,
getAppId,
getBucketKeys,
getHooksDirPath,
getProjectMeta,
initJSProjectWithProfile,
removeFunction,
} from '@aws-amplify/amplify-e2e-core';
import * as fs from 'fs-extra';
import * as path from 'path';
import { addEnvironment, checkoutEnvironment } from '../environment/env';

const checkForFiles = (toCheckFiles: string[], inFiles: string[], prefix?: string): void => {
toCheckFiles.forEach(toCheckFile => {
expect(inFiles).toContain(prefix ? prefix.concat(toCheckFile) : toCheckFile);
});
};

describe('runtime hooks', () => {
let projRoot: string;
Expand All @@ -34,6 +21,7 @@ describe('runtime hooks', () => {
});

it('test hook scripts with non zero exit code', async () => {
// eslint-disable-next-line spellcheck/spell-checker
await initJSProjectWithProfile(projRoot, { envName: 'enva' });
const hooksDirPath = getHooksDirPath(projRoot);
expect(fs.existsSync(hooksDirPath)).toBe(true);
Expand Down
2 changes: 1 addition & 1 deletion scripts/split-e2e-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const WINDOWS_TEST_ALLOWLIST: string[] = [
'schema-auth-11-c_pkg',
'auth_6_pkg',
'frontend_config_drift_pkg',
'hooks_pkg',
'hooks-b_pkg',
aws-eddy marked this conversation as resolved.
Show resolved Hide resolved
'plugin_pkg',
'schema-versioned_pkg',
'schema-auth-3_pkg',
Expand Down