-
Notifications
You must be signed in to change notification settings - Fork 19
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
Shared config updates #452
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1e048da
feat: expose source namespace to plugin lifecycle methods
DavieReid 3ad8b71
feat: apply namespace shared config to sources that don't have their …
DavieReid e68f946
feat: namespace shared config is applied across sources
DavieReid e508870
test: update `$afterSource` tests for `SharedConfigPlugin`
DavieReid 5ad6393
test: add test for `afterUpdate` in `SharedConfigPlugin`
DavieReid e9bbdfb
chore: add changeset
DavieReid 5ddf549
feat: use unique id for namespace shared configs
DavieReid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
'@jpmorganchase/mosaic-core': patch | ||
'@jpmorganchase/mosaic-plugins': patch | ||
'@jpmorganchase/mosaic-site': patch | ||
'@jpmorganchase/mosaic-types': patch | ||
--- | ||
|
||
`SharedConfigPlugin` can now apply a shared config to a source that doesn't have one but shares a namespace with 1 that does. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,17 +43,31 @@ export interface SharedConfigPluginOptions { | |
* It then exports a JSON file (name: `options.filename`) into each directory with the merged config for that level | ||
*/ | ||
const SharedConfigPlugin: PluginType<SharedConfigPluginPage, SharedConfigPluginOptions> = { | ||
async $afterSource(pages, { ignorePages, pageExtensions }) { | ||
async $afterSource(pages, { ignorePages, pageExtensions, config, namespace }) { | ||
const isNonHiddenPage = createPageTest(ignorePages, pageExtensions); | ||
let finalSharedConfig; | ||
|
||
const indexPagesWithSharedConfig = pages.filter( | ||
const indexPages = pages.filter( | ||
page => | ||
path.posix.basename(page.fullPath, path.posix.extname(page.fullPath)) === 'index' && | ||
page.sharedConfig !== undefined && | ||
isNonHiddenPage(page.fullPath) | ||
); | ||
|
||
const indexPagesWithSharedConfig = indexPages.filter(page => page.sharedConfig !== undefined); | ||
|
||
if (indexPagesWithSharedConfig.length === 0 && indexPages.length > 0) { | ||
const rootPath = indexPages[0].fullPath; | ||
const applyNamespaceSharedConfig = { | ||
[`${namespace}~~${rootPath}`]: { | ||
paths: indexPages.map(indexPage => indexPage.fullPath), | ||
rootPath, | ||
namespace | ||
} | ||
}; | ||
config.setData({ applyNamespaceSharedConfig }); | ||
return pages; | ||
} | ||
|
||
for (const page of indexPagesWithSharedConfig) { | ||
if (finalSharedConfig === undefined) { | ||
// first shared config we have found so seed the finalSharedConfig | ||
|
@@ -113,24 +127,97 @@ const SharedConfigPlugin: PluginType<SharedConfigPluginPage, SharedConfigPluginO | |
} | ||
|
||
// apply closest shared config | ||
let closestSharedConfigIndex = 0; | ||
for (const pagePath of indexPagesWithoutConfig) { | ||
for (let i = 0; i < sharedConfigFiles.length; i++) { | ||
if (isWithin(sharedConfigFiles[i], pagePath)) { | ||
closestSharedConfigIndex = i; | ||
if (sharedConfigFiles.length > 0) { | ||
let closestSharedConfigIndex = 0; | ||
for (const pagePath of indexPagesWithoutConfig) { | ||
for (let i = 0; i < sharedConfigFiles.length; i++) { | ||
if (isWithin(sharedConfigFiles[i], pagePath)) { | ||
closestSharedConfigIndex = i; | ||
} | ||
} | ||
|
||
const sharedConfigFile = path.posix.join( | ||
path.posix.dirname(String(pagePath)), | ||
options.filename | ||
); | ||
|
||
const closestSharedConfig = path.posix.resolve( | ||
path.dirname(String(pagePath)), | ||
sharedConfigFiles[closestSharedConfigIndex] | ||
); | ||
config.setAliases(closestSharedConfig, [sharedConfigFile]); | ||
} | ||
} | ||
}, | ||
async afterUpdate(mutableFilesystem, { sharedFilesystem, globalConfig, namespace }, options) { | ||
const { applyNamespaceSharedConfig } = globalConfig.data; | ||
|
||
const sharedConfigFile = path.posix.join( | ||
path.posix.dirname(String(pagePath)), | ||
options.filename | ||
); | ||
if (applyNamespaceSharedConfig === undefined) { | ||
// there is no source that exists that has told us it needs to share a namespace shared-config | ||
return; | ||
} | ||
|
||
const closestSharedConfig = path.posix.resolve( | ||
path.dirname(String(pagePath)), | ||
sharedConfigFiles[closestSharedConfigIndex] | ||
); | ||
config.setAliases(closestSharedConfig, [sharedConfigFile]); | ||
// find all the entries that match the namespace the plugin is running against | ||
const namespaceSharedConfigs: { | ||
paths: string[]; | ||
rootPath: string; | ||
namespace: string; | ||
}[] = Object.keys(applyNamespaceSharedConfig) | ||
.filter(key => { | ||
const keyNamespace = key.split('~~')?.[0]; | ||
DavieReid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return keyNamespace === namespace; | ||
}) | ||
.map(key => applyNamespaceSharedConfig?.[key] || []); | ||
|
||
for (const namespaceSharedConfig of namespaceSharedConfigs) { | ||
if (await mutableFilesystem.promises.exists(namespaceSharedConfig.rootPath)) { | ||
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. This is to guard against the actual write of the shared config.json that is a copy. It might trigger afterUpdate again but not 100% sure |
||
// a source does need a namespace shared config but the source running this plugin is the source that needs it | ||
// so we don't need to do anything here | ||
continue; | ||
} | ||
|
||
for (const applyPath of namespaceSharedConfig.paths) { | ||
if (!(await sharedFilesystem.promises.exists(applyPath))) { | ||
sharedFilesystem.promises.mkdir(path.posix.dirname(String(applyPath)), { | ||
recursive: true | ||
}); | ||
} | ||
let parentDir = path.posix.join(path.posix.dirname(String(applyPath)), '../'); | ||
let closestSharedConfigPath = path.posix.join(parentDir, options.filename); | ||
|
||
while (parentDir !== path.posix.sep) { | ||
// walk up the directories in the path to find the closest shared config file | ||
closestSharedConfigPath = path.posix.join(parentDir, options.filename); | ||
if (await mutableFilesystem.promises.exists(closestSharedConfigPath)) { | ||
break; | ||
} | ||
parentDir = path.posix.join(path.posix.dirname(String(closestSharedConfigPath)), '../'); | ||
} | ||
|
||
const aliasSharedConfigPath = path.posix.join( | ||
path.posix.dirname(String(applyPath)), | ||
options.filename | ||
); | ||
|
||
if ( | ||
(await mutableFilesystem.promises.exists(closestSharedConfigPath)) && | ||
!(await sharedFilesystem.promises.exists(aliasSharedConfigPath)) | ||
) { | ||
console.log( | ||
`[Mosaic][Plugin] Source has no shared config. Root index page is: ${namespaceSharedConfig.rootPath}` | ||
); | ||
console.log( | ||
'[Mosaic][Plugin] Copying shared config ', | ||
closestSharedConfigPath, | ||
'-->', | ||
aliasSharedConfigPath | ||
); | ||
await sharedFilesystem.promises.writeFile( | ||
aliasSharedConfigPath, | ||
await mutableFilesystem.promises.readFile(closestSharedConfigPath) | ||
); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the config object is persisted across plugin lifecycle events. So this source is writing to the global config in the hope another source that does have a shared config will apply it.
This is picked up in the
afterUpdate
event