|
| 1 | +import * as os from 'os'; |
| 2 | +import * as fs_path from 'path'; |
| 3 | +import * as fs from 'fs-extra'; |
| 4 | +import { warning } from '../logging'; |
| 5 | +import { ToolkitError } from '../toolkit/error'; |
| 6 | +import * as util from '../util/objects'; |
| 7 | + |
| 8 | +export type SettingsMap = {[key: string]: any}; |
| 9 | + |
| 10 | +/** |
| 11 | + * If a context value is an object with this key set to a truthy value, it won't be saved to cdk.context.json |
| 12 | + */ |
| 13 | +export const TRANSIENT_CONTEXT_KEY = '$dontSaveContext'; |
| 14 | + |
| 15 | +/** |
| 16 | + * A single bag of settings |
| 17 | + */ |
| 18 | +export class Settings { |
| 19 | + public static mergeAll(...settings: Settings[]): Settings { |
| 20 | + let ret = new Settings(); |
| 21 | + for (const setting of settings) { |
| 22 | + ret = ret.merge(setting); |
| 23 | + } |
| 24 | + return ret; |
| 25 | + } |
| 26 | + |
| 27 | + constructor(private settings: SettingsMap = {}, public readonly readOnly = false) {} |
| 28 | + |
| 29 | + public async load(fileName: string): Promise<this> { |
| 30 | + if (this.readOnly) { |
| 31 | + throw new ToolkitError(`Can't load ${fileName}: settings object is readonly`); |
| 32 | + } |
| 33 | + this.settings = {}; |
| 34 | + |
| 35 | + const expanded = expandHomeDir(fileName); |
| 36 | + if (await fs.pathExists(expanded)) { |
| 37 | + this.settings = await fs.readJson(expanded); |
| 38 | + } |
| 39 | + |
| 40 | + // See https://github.com/aws/aws-cdk/issues/59 |
| 41 | + this.prohibitContextKey('default-account', fileName); |
| 42 | + this.prohibitContextKey('default-region', fileName); |
| 43 | + this.warnAboutContextKey('aws:', fileName); |
| 44 | + |
| 45 | + return this; |
| 46 | + } |
| 47 | + |
| 48 | + public async save(fileName: string): Promise<this> { |
| 49 | + const expanded = expandHomeDir(fileName); |
| 50 | + await fs.writeJson(expanded, stripTransientValues(this.settings), { spaces: 2 }); |
| 51 | + return this; |
| 52 | + } |
| 53 | + |
| 54 | + public get all(): any { |
| 55 | + return this.get([]); |
| 56 | + } |
| 57 | + |
| 58 | + public merge(other: Settings): Settings { |
| 59 | + return new Settings(util.deepMerge(this.settings, other.settings)); |
| 60 | + } |
| 61 | + |
| 62 | + public subSettings(keyPrefix: string[]) { |
| 63 | + return new Settings(this.get(keyPrefix) || {}, false); |
| 64 | + } |
| 65 | + |
| 66 | + public makeReadOnly(): Settings { |
| 67 | + return new Settings(this.settings, true); |
| 68 | + } |
| 69 | + |
| 70 | + public clear() { |
| 71 | + if (this.readOnly) { |
| 72 | + throw new ToolkitError('Cannot clear(): settings are readonly'); |
| 73 | + } |
| 74 | + this.settings = {}; |
| 75 | + } |
| 76 | + |
| 77 | + public get empty(): boolean { |
| 78 | + return Object.keys(this.settings).length === 0; |
| 79 | + } |
| 80 | + |
| 81 | + public get(path: string[]): any { |
| 82 | + return util.deepClone(util.deepGet(this.settings, path)); |
| 83 | + } |
| 84 | + |
| 85 | + public set(path: string[], value: any): Settings { |
| 86 | + if (this.readOnly) { |
| 87 | + throw new ToolkitError(`Can't set ${path}: settings object is readonly`); |
| 88 | + } |
| 89 | + if (path.length === 0) { |
| 90 | + // deepSet can't handle this case |
| 91 | + this.settings = value; |
| 92 | + } else { |
| 93 | + util.deepSet(this.settings, path, value); |
| 94 | + } |
| 95 | + return this; |
| 96 | + } |
| 97 | + |
| 98 | + public unset(path: string[]) { |
| 99 | + this.set(path, undefined); |
| 100 | + } |
| 101 | + |
| 102 | + private prohibitContextKey(key: string, fileName: string) { |
| 103 | + if (!this.settings.context) { return; } |
| 104 | + if (key in this.settings.context) { |
| 105 | + // eslint-disable-next-line max-len |
| 106 | + throw new ToolkitError(`The 'context.${key}' key was found in ${fs_path.resolve(fileName)}, but it is no longer supported. Please remove it.`); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private warnAboutContextKey(prefix: string, fileName: string) { |
| 111 | + if (!this.settings.context) { return; } |
| 112 | + for (const contextKey of Object.keys(this.settings.context)) { |
| 113 | + if (contextKey.startsWith(prefix)) { |
| 114 | + // eslint-disable-next-line max-len |
| 115 | + warning(`A reserved context key ('context.${prefix}') key was found in ${fs_path.resolve(fileName)}, it might cause surprising behavior and should be removed.`); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +function expandHomeDir(x: string) { |
| 122 | + if (x.startsWith('~')) { |
| 123 | + return fs_path.join(os.homedir(), x.slice(1)); |
| 124 | + } |
| 125 | + return x; |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Return all context value that are not transient context values |
| 130 | + */ |
| 131 | +function stripTransientValues(obj: {[key: string]: any}) { |
| 132 | + const ret: any = {}; |
| 133 | + for (const [key, value] of Object.entries(obj)) { |
| 134 | + if (!isTransientValue(value)) { |
| 135 | + ret[key] = value; |
| 136 | + } |
| 137 | + } |
| 138 | + return ret; |
| 139 | +} |
| 140 | + |
| 141 | +/** |
| 142 | + * Return whether the given value is a transient context value |
| 143 | + * |
| 144 | + * Values that are objects with a magic key set to a truthy value are considered transient. |
| 145 | + */ |
| 146 | +function isTransientValue(value: any) { |
| 147 | + return typeof value === 'object' && value !== null && (value as any)[TRANSIENT_CONTEXT_KEY]; |
| 148 | +} |
| 149 | + |
0 commit comments