Skip to content
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
52 changes: 0 additions & 52 deletions packages/aws-cdk/lib/api/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as os from 'os';
import * as fs_path from 'path';
import * as fs from 'fs-extra';
import { ToolkitError } from '../../../@aws-cdk/tmp-toolkit-helpers/src/api';
import { warning } from '../logging';
import * as util from '../util';

export type SettingsMap = { [key: string]: any };
Expand Down Expand Up @@ -30,27 +29,6 @@ export class Settings {
) {
}

public async load(fileName: string): Promise<this> {
if (this.readOnly) {
throw new ToolkitError(
`Can't load ${fileName}: settings object is readonly`,
);
}
this.settings = {};

const expanded = expandHomeDir(fileName);
if (await fs.pathExists(expanded)) {
this.settings = await fs.readJson(expanded);
}

// See https://github.com/aws/aws-cdk/issues/59
this.prohibitContextKey('default-account', fileName);
this.prohibitContextKey('default-region', fileName);
this.warnAboutContextKey('aws:', fileName);

return this;
}

public async save(fileName: string): Promise<this> {
const expanded = expandHomeDir(fileName);
await fs.writeJson(expanded, stripTransientValues(this.settings), {
Expand Down Expand Up @@ -106,36 +84,6 @@ export class Settings {
public unset(path: string[]) {
this.set(path, undefined);
}

private prohibitContextKey(key: string, fileName: string) {
if (!this.settings.context) {
return;
}
if (key in this.settings.context) {
// eslint-disable-next-line max-len
throw new ToolkitError(
`The 'context.${key}' key was found in ${fs_path.resolve(
fileName,
)}, but it is no longer supported. Please remove it.`,
);
}
}

private warnAboutContextKey(prefix: string, fileName: string) {
if (!this.settings.context) {
return;
}
for (const contextKey of Object.keys(this.settings.context)) {
if (contextKey.startsWith(prefix)) {
// eslint-disable-next-line max-len
warning(
`A reserved context key ('context.${prefix}') key was found in ${fs_path.resolve(
fileName,
)}, it might cause surprising behavior and should be removed.`,
);
}
}
}
}

function expandHomeDir(x: string) {
Expand Down
64 changes: 62 additions & 2 deletions packages/aws-cdk/lib/cli/user-configuration.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import * as os from 'os';
import * as fs_path from 'path';
import * as fs from 'fs-extra';
import { ToolkitError } from '../../../@aws-cdk/tmp-toolkit-helpers/src/api';
import { Context, PROJECT_CONTEXT } from '../api/context';
import { Settings } from '../api/settings';
Expand Down Expand Up @@ -174,14 +177,71 @@ export class Configuration {
}

async function loadAndLog(fileName: string): Promise<Settings> {
const ret = new Settings();
await ret.load(fileName);
const ret = await settingsFromFile(fileName);
if (!ret.empty) {
debug(fileName + ':', JSON.stringify(ret.all, undefined, 2));
}
return ret;
}

async function settingsFromFile(fileName: string): Promise<Settings> {
let settings;
const expanded = expandHomeDir(fileName);
if (await fs.pathExists(expanded)) {
const data = await fs.readJson(expanded);
settings = new Settings(data);
} else {
settings = new Settings();
}

// See https://github.com/aws/aws-cdk/issues/59
prohibitContextKeys(settings, ['default-account', 'default-region'], fileName);
warnAboutContextKey(settings, 'aws:', fileName);

return settings;
}

function prohibitContextKeys(settings: Settings, keys: string[], fileName: string) {
const context = settings.get(['context']);
if (!context || typeof context !== 'object') {
return;
}

for (const key of keys) {
if (key in context) {
throw new ToolkitError(
`The 'context.${key}' key was found in ${fs_path.resolve(
fileName,
)}, but it is no longer supported. Please remove it.`,
);
}
}
}

function warnAboutContextKey(settings: Settings, prefix: string, fileName: string) {
const context = settings.get(['context']);
if (!context || typeof context !== 'object') {
return;
}

for (const contextKey of Object.keys(context)) {
if (contextKey.startsWith(prefix)) {
warning(
`A reserved context key ('context.${prefix}') key was found in ${fs_path.resolve(
fileName,
)}, it might cause surprising behavior and should be removed.`,
);
}
}
}

function expandHomeDir(x: string) {
if (x.startsWith('~')) {
return fs_path.join(os.homedir(), x.slice(1));
}
return x;
}

/**
* Parse CLI arguments into Settings
*
Expand Down