|
| 1 | +import * as os from 'os'; |
| 2 | +import * as fs_path from 'path'; |
| 3 | +import * as fs from 'fs-extra'; |
1 | 4 | import { Context, PROJECT_CONTEXT } from '../api/context'; |
2 | 5 | import { Settings } from '../api/settings'; |
3 | 6 | import type { Tag } from '../api/tags'; |
@@ -174,14 +177,71 @@ export class Configuration { |
174 | 177 | } |
175 | 178 |
|
176 | 179 | async function loadAndLog(fileName: string): Promise<Settings> { |
177 | | - const ret = new Settings(); |
178 | | - await ret.load(fileName); |
| 180 | + const ret = await settingsFromFile(fileName); |
179 | 181 | if (!ret.empty) { |
180 | 182 | debug(fileName + ':', JSON.stringify(ret.all, undefined, 2)); |
181 | 183 | } |
182 | 184 | return ret; |
183 | 185 | } |
184 | 186 |
|
| 187 | +async function settingsFromFile(fileName: string): Promise<Settings> { |
| 188 | + let settings; |
| 189 | + const expanded = expandHomeDir(fileName); |
| 190 | + if (await fs.pathExists(expanded)) { |
| 191 | + const data = await fs.readJson(expanded); |
| 192 | + settings = new Settings(data); |
| 193 | + } else { |
| 194 | + settings = new Settings(); |
| 195 | + } |
| 196 | + |
| 197 | + // See https://github.com/aws/aws-cdk/issues/59 |
| 198 | + prohibitContextKeys(settings, ['default-account', 'default-region'], fileName); |
| 199 | + warnAboutContextKey(settings, 'aws:', fileName); |
| 200 | + |
| 201 | + return settings; |
| 202 | +} |
| 203 | + |
| 204 | +function prohibitContextKeys(settings: Settings, keys: string[], fileName: string) { |
| 205 | + const context = settings.get(['context']); |
| 206 | + if (!context || typeof context !== 'object') { |
| 207 | + return; |
| 208 | + } |
| 209 | + |
| 210 | + for (const key of keys) { |
| 211 | + if (key in context) { |
| 212 | + throw new ToolkitError( |
| 213 | + `The 'context.${key}' key was found in ${fs_path.resolve( |
| 214 | + fileName, |
| 215 | + )}, but it is no longer supported. Please remove it.`, |
| 216 | + ); |
| 217 | + } |
| 218 | + } |
| 219 | +} |
| 220 | + |
| 221 | +function warnAboutContextKey(settings: Settings, prefix: string, fileName: string) { |
| 222 | + const context = settings.get(['context']); |
| 223 | + if (!context || typeof context !== 'object') { |
| 224 | + return; |
| 225 | + } |
| 226 | + |
| 227 | + for (const contextKey of Object.keys(context)) { |
| 228 | + if (contextKey.startsWith(prefix)) { |
| 229 | + warning( |
| 230 | + `A reserved context key ('context.${prefix}') key was found in ${fs_path.resolve( |
| 231 | + fileName, |
| 232 | + )}, it might cause surprising behavior and should be removed.`, |
| 233 | + ); |
| 234 | + } |
| 235 | + } |
| 236 | +} |
| 237 | + |
| 238 | +function expandHomeDir(x: string) { |
| 239 | + if (x.startsWith('~')) { |
| 240 | + return fs_path.join(os.homedir(), x.slice(1)); |
| 241 | + } |
| 242 | + return x; |
| 243 | +} |
| 244 | + |
185 | 245 | /** |
186 | 246 | * Parse CLI arguments into Settings |
187 | 247 | * |
|
0 commit comments