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