-
Notifications
You must be signed in to change notification settings - Fork 177
feat: add support for YAML file includes and nested includes in context #1252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
5fa8642
c67cf02
68080a5
3e9734b
d745315
b7746e8
f8b4fb4
da204f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,52 @@ import { Assets, Config, Auth0APIClient, AssetTypes, KeywordMappings } from '../ | |
| import { filterOnlyIncludedResourceTypes } from '..'; | ||
| import { preserveKeywords } from '../../keywordPreservation'; | ||
|
|
||
| // Custom YAML type for file includes | ||
| const includeType = new yaml.Type('!include', { | ||
| kind: 'scalar', | ||
| resolve: (data) => typeof data === 'string', | ||
| construct: (data) => { | ||
| // This will be handled during the actual loading process | ||
| return { __include: data }; | ||
| } | ||
| }); | ||
|
|
||
| const schema = yaml.DEFAULT_SCHEMA.extend([includeType]); | ||
|
|
||
| // Function to resolve includes | ||
| function resolveIncludes(obj, basePath, mappings?: KeywordMappings, disableKeywordReplacement?: boolean) { | ||
| if (Array.isArray(obj)) { | ||
| return obj.map(item => resolveIncludes(item, basePath, mappings, disableKeywordReplacement)); | ||
| } | ||
|
|
||
| if (obj && typeof obj === 'object') { | ||
| if (obj.__include) { | ||
| const filePath = path.resolve(basePath, obj.__include); | ||
| if (fs.existsSync(filePath)) { | ||
| let content = fs.readFileSync(filePath, 'utf8'); | ||
|
|
||
| // Apply keyword replacement to included file content if mappings are provided | ||
| if (mappings && !disableKeywordReplacement) { | ||
| content = keywordReplace(content, mappings); | ||
| } else if (mappings && disableKeywordReplacement) { | ||
| content = wrapArrayReplaceMarkersInQuotes(content, mappings); | ||
| } | ||
|
|
||
| return resolveIncludes(yaml.load(content, { schema }), path.dirname(filePath), mappings, disableKeywordReplacement); | ||
| } | ||
| throw new Error(`Include file not found: ${filePath}`); | ||
| } | ||
|
|
||
| const result = {}; | ||
| for (const [key, value] of Object.entries(obj)) { | ||
| result[key] = resolveIncludes(value, basePath, mappings, disableKeywordReplacement); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| return obj; | ||
| } | ||
|
|
||
| export default class YAMLContext { | ||
| basePath: string; | ||
| configFile: string; | ||
|
|
@@ -58,6 +104,10 @@ export default class YAMLContext { | |
| if (!isFile(toLoad)) { | ||
| // try load not relative to yaml file | ||
| toLoad = f; | ||
| if (!isFile(toLoad)) { | ||
| // try absolute path resolution | ||
| toLoad = path.resolve(f); | ||
| } | ||
| } | ||
| return loadFileAndReplaceKeywords(path.resolve(toLoad), { | ||
| mappings: this.mappings, | ||
|
|
@@ -74,13 +124,16 @@ export default class YAMLContext { | |
| try { | ||
| const fPath = path.resolve(this.configFile); | ||
| log.debug(`Loading YAML from ${fPath}`); | ||
| const loadedYaml = yaml.load( | ||
| opts.disableKeywordReplacement | ||
| ? wrapArrayReplaceMarkersInQuotes(fs.readFileSync(fPath, 'utf8'), this.mappings) | ||
| : keywordReplace(fs.readFileSync(fPath, 'utf8'), this.mappings), | ||
| { schema } | ||
| ) || {}; | ||
|
|
||
| Object.assign( | ||
| this.assets, | ||
| yaml.load( | ||
| opts.disableKeywordReplacement | ||
| ? wrapArrayReplaceMarkersInQuotes(fs.readFileSync(fPath, 'utf8'), this.mappings) | ||
| : keywordReplace(fs.readFileSync(fPath, 'utf8'), this.mappings) | ||
| ) || {} | ||
| resolveIncludes(loadedYaml, path.dirname(fPath), this.mappings, opts.disableKeywordReplacement) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to use so that
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. refactored the resolveIncludes function to follow the single responsibility principle. resolveIncludes() now handles the initial content parsing and delegates to resolveIncludesInObject() resolveIncludesInObject() focuses solely on processing the parsed object structure
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now, Ideally, one function A better approach, I think.
let me know if it's possible to do it this way. |
||
| ); | ||
| } catch (err) { | ||
| log.debug(err.stack); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.