Skip to content

Commit

Permalink
feat: use unique id for namespace shared configs
Browse files Browse the repository at this point in the history
  • Loading branch information
DavieReid committed Aug 17, 2023
1 parent d3b8b3a commit f67a1b5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
10 changes: 4 additions & 6 deletions packages/plugins/src/SharedConfigPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import crypto from 'node:crypto';
import path from 'node:path';
import type { Page, Plugin as PluginType } from '@jpmorganchase/mosaic-types';
import { flatten, escapeRegExp } from 'lodash-es';
import path from 'path';
import deepmerge from 'deepmerge';

function createPageTest(ignorePages, pageExtensions) {
Expand Down Expand Up @@ -58,7 +59,7 @@ const SharedConfigPlugin: PluginType<SharedConfigPluginPage, SharedConfigPluginO
if (indexPagesWithSharedConfig.length === 0 && indexPages.length > 0) {
const rootPath = indexPages[0].fullPath;
const applyNamespaceSharedConfig = {
[`${namespace}~~${rootPath}`]: {
[`${crypto.randomUUID()}`]: {
paths: indexPages.map(indexPage => indexPage.fullPath),
rootPath,
namespace
Expand Down Expand Up @@ -163,10 +164,7 @@ const SharedConfigPlugin: PluginType<SharedConfigPluginPage, SharedConfigPluginO
rootPath: string;
namespace: string;
}[] = Object.keys(applyNamespaceSharedConfig)
.filter(key => {
const keyNamespace = key.split('~~')?.[0];
return keyNamespace === namespace;
})
.filter(key => applyNamespaceSharedConfig[key].namespace === namespace)
.map(key => applyNamespaceSharedConfig?.[key] || []);

for (const namespaceSharedConfig of namespaceSharedConfigs) {
Expand Down
36 changes: 15 additions & 21 deletions packages/plugins/src/__tests__/SharedConfigPlugin.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Page } from '@jpmorganchase/mosaic-types';
import SharedConfigPlugin from '../SharedConfigPlugin';

jest.mock('node:crypto', () => {
return {
randomUUID: jest.fn(() => '123')
};
});

type SharedConfigPage = Page & { sharedConfig?: any };
/**
*
Expand Down Expand Up @@ -209,7 +215,7 @@ describe('GIVEN the SharedConfigPlugin', () => {

expect(namespaceConfig).toEqual({
applyNamespaceSharedConfig: {
'test-ns~~/FolderA/index.mdx': {
'123': {
namespace: 'test-ns',
paths: [
'/FolderA/index.mdx',
Expand All @@ -228,47 +234,37 @@ describe('GIVEN the SharedConfigPlugin', () => {

const namespaceConfig = setDataMock.mock.calls[0][0];

expect(
namespaceConfig.applyNamespaceSharedConfig['test-ns~~/FolderA/index.mdx']
).toBeDefined();
expect(namespaceConfig.applyNamespaceSharedConfig['123']).toBeDefined();

expect(
namespaceConfig.applyNamespaceSharedConfig['test-ns~~/FolderA/index.mdx'].namespace
).toEqual(testNamespace);
expect(namespaceConfig.applyNamespaceSharedConfig['123'].namespace).toEqual(testNamespace);
});

test('THEN the namespace is included in the namespace shared config object', () => {
expect(setDataMock).toHaveBeenCalledTimes(1);

const namespaceConfig = setDataMock.mock.calls[0][0];

expect(
namespaceConfig.applyNamespaceSharedConfig['test-ns~~/FolderA/index.mdx'].namespace
).toEqual(testNamespace);
expect(namespaceConfig.applyNamespaceSharedConfig['123'].namespace).toEqual(testNamespace);
});

test('THEN the root path is included in the namespace shared config object', () => {
expect(setDataMock).toHaveBeenCalledTimes(1);

const namespaceConfig = setDataMock.mock.calls[0][0];

expect(
namespaceConfig.applyNamespaceSharedConfig['test-ns~~/FolderA/index.mdx'].rootPath
).toEqual('/FolderA/index.mdx');
expect(namespaceConfig.applyNamespaceSharedConfig['123'].rootPath).toEqual(
'/FolderA/index.mdx'
);
});

test('THEN all index pages in the source are included in namespace shared config object', () => {
expect(setDataMock).toHaveBeenCalledTimes(1);

const namespaceConfig = setDataMock.mock.calls[0][0];

expect(
namespaceConfig.applyNamespaceSharedConfig['test-ns~~/FolderA/index.mdx'].paths.length
).toEqual(4);
expect(namespaceConfig.applyNamespaceSharedConfig['123'].paths.length).toEqual(4);

expect(
namespaceConfig.applyNamespaceSharedConfig['test-ns~~/FolderA/index.mdx'].paths
).toEqual([
expect(namespaceConfig.applyNamespaceSharedConfig['123'].paths).toEqual([
'/FolderA/index.mdx',
'/FolderA/SubfolderA/index.mdx',
'/FolderA/SubfolderB/index.mdx',
Expand Down Expand Up @@ -505,8 +501,6 @@ describe('GIVEN the SharedConfigPlugin', () => {
expect(sharedFilesystem.promises.exists.mock.calls[0][0]).toEqual('/FolderY/index.mdx');
expect(sharedFilesystem.promises.mkdir.mock.calls[0][0]).toEqual('/FolderY');

console.log('HELLO', volume.promises.exists.mock.calls);

expect(volume.promises.exists.mock.calls[1][0]).toEqual('/test-shared-config.json');

expect(sharedFilesystem.promises.writeFile).toBeCalledTimes(1);
Expand Down

1 comment on commit f67a1b5

@vercel
Copy link

@vercel vercel bot commented on f67a1b5 Aug 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

mosaic – ./

mosaic-git-main-mosaic-dev-team.vercel.app
mosaic-mosaic-dev-team.vercel.app

Please sign in to comment.