Skip to content

Commit

Permalink
fix: set awscloudformation flag to false in vscode settings after ove…
Browse files Browse the repository at this point in the history
…rride (#13310)

* fix: add method to remove awscloudformation flag from settings

* test: add asserts to make sure awscloudformation flag is set to false after override
  • Loading branch information
rtpascual authored Oct 6, 2023
1 parent f3c9c05 commit d60e505
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { printer, prompter } from '@aws-amplify/amplify-prompts';
import execa from 'execa';
import * as fs from 'fs-extra';
import * as path from 'path';
import { $TSContext, AmplifyError, getPackageManager, pathManager, skipHooks } from '../index';
import { $TSContext, AmplifyError, getPackageManager, pathManager, skipHooks, stateManager } from '../index';
import { JSONUtilities } from '../jsonUtilities';
import { merge } from 'lodash';

/**
* This method generates the default/template overrides file
Expand Down Expand Up @@ -63,6 +64,10 @@ export const buildOverrideDir = async (cwd: string, destDirPath: string): Promis
const overrideSampleTsconfigJsonPath = path.join(__dirname, '..', '..', 'resources', 'overrides-resource', 'tsconfig.json');
fs.writeFileSync(overrideBackendTsConfigJson, fs.readFileSync(overrideSampleTsconfigJsonPath));
}

// ensure awscloudformation folder is not excluded in vscode
setSettingsJsonAwscloudformationFlagFalse();

const packageManager = await getPackageManager(cwd);

if (packageManager === null) {
Expand Down Expand Up @@ -154,3 +159,27 @@ export const generateTsConfigforProject = (srcResourceDirPath: string, destDirPa
fs.writeFileSync(overrideFileName, fs.readFileSync(path.join(srcResourceDirPath, 'override.ts.sample')));
fs.writeFileSync(resourceTsConfigFileName, fs.readFileSync(path.join(srcResourceDirPath, 'tsconfig.resource.json')));
};

/**
* this method sets the flag to false in vscode settings.json to show awscloudformation folder in vscode
*/
const setSettingsJsonAwscloudformationFlagFalse = (): void => {
if (stateManager.getLocalEnvInfo().defaultEditor !== 'vscode') {
return;
}

const workspaceSettingsPath = '.vscode/settings.json';
const exclusionRules = {
'files.exclude': {
'amplify/backend/awscloudformation': false,
},
};

try {
// if settings file exists, safely add exclude settings to it
const settings = JSONUtilities.readJson(workspaceSettingsPath);
JSONUtilities.writeJson(workspaceSettingsPath, merge(settings, exclusionRules));
} catch (error) {
// workspace settings file does not exist, noop
}
};
9 changes: 9 additions & 0 deletions packages/amplify-e2e-tests/src/__tests__/init_e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,22 @@ describe('amplify init e', () => {
expect(meta.Region).toBeDefined();
const { AuthRoleName, UnauthRoleName, UnauthRoleArn, AuthRoleArn, DeploymentBucketName } = meta;

// test default vscode settings.json for awscloudformation folder
const editorSettingsPath = path.join(projRoot, '.vscode', 'settings.json');
const editorSettings = fs.readJSONSync(editorSettingsPath);
expect(editorSettings['files.exclude']['amplify/backend/awscloudformation']).toEqual(true);

await expect(UnauthRoleName).toBeIAMRoleWithArn(UnauthRoleArn);
await expect(AuthRoleName).toBeIAMRoleWithArn(AuthRoleArn);
await expect(DeploymentBucketName).toBeAS3Bucket(DeploymentBucketName);

// override new env
await amplifyOverrideRoot(projRoot, { testingWithLatestCodebase: false });

// test awscloudformation folder is not excluded in vscode settings.json after override
const editorSettingsAfterOverride = fs.readJSONSync(editorSettingsPath);
expect(editorSettingsAfterOverride['files.exclude']['amplify/backend/awscloudformation']).toEqual(false);

// this is where we will write overrides to
const destOverrideFilePath = path.join(projRoot, 'amplify', 'backend', 'awscloudformation', 'override.ts');

Expand Down

0 comments on commit d60e505

Please sign in to comment.