-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Patternfly dynamic-modules.json where possible (#2030)
* Click on toggle element in BulkSelect tests * Remove caching from old code * Add caching to new lookup code * Update Patternfly to latest version with dynamic-modules.json * Update snapshots from new Patternfly version * Click on toggle element in BulkSelect tests * Use default export for guessComponentModule * Update gitignore * Use findFirstGlob in getPossibleLocations * Use findFirstGlob in getModuleExplicitLocation
- Loading branch information
1 parent
98b0948
commit b1f5bb5
Showing
9 changed files
with
233 additions
and
164 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
index.js | ||
index.d.ts | ||
index.js.map | ||
directories.js | ||
directories.d.ts | ||
guess-module.js | ||
guess-module.d.ts |
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,33 @@ | ||
import path from 'path'; | ||
import * as glob from 'glob'; | ||
|
||
const MODULES_ROOT = process.env.MODULES_ROOT; | ||
|
||
const PACKAGES_ROOT = path.resolve(process.cwd(), 'packages'); | ||
|
||
export const CORE_DIRECTORIES = [ | ||
glob.sync(`${process.cwd()}/node_modules/@patternfly/react-core`), | ||
glob.sync(`${PACKAGES_ROOT}/*/node_modules/@patternfly/react-core`), | ||
].flat(); | ||
|
||
export const ICONS_DIRECTORIES = [ | ||
glob.sync(`${process.cwd()}/node_modules/@patternfly/react-icons`), | ||
glob.sync(`${PACKAGES_ROOT}/*/node_modules/@patternfly/react-icons`), | ||
].flat(); | ||
|
||
if (MODULES_ROOT) { | ||
// comma separated list of roots | ||
MODULES_ROOT.split(',').forEach((root) => { | ||
CORE_DIRECTORIES.push(...glob.sync(`${path.resolve(__dirname, root)}/node_modules/@patternfly/react-core`.replace(/\/\//, '/'))); | ||
ICONS_DIRECTORIES.push(...glob.sync(`${path.resolve(__dirname, root)}/node_modules/@patternfly/react-icons`.replace(/\/\//, '/'))); | ||
}); | ||
} | ||
|
||
export function findFirstGlob(roots: string[], suffix: string, filter?: (path: string) => boolean): string | undefined { | ||
const adjustedSuffix = suffix.startsWith('/') ? suffix.substring(1) : suffix; | ||
|
||
return roots.flatMap((root) => { | ||
const found = glob.sync(`${root}/${adjustedSuffix}`); | ||
return filter !== undefined ? found.filter(filter) : found; | ||
})[0]; | ||
} |
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,99 @@ | ||
import * as glob from 'glob'; | ||
import { CORE_DIRECTORIES, findFirstGlob } from './directories'; | ||
|
||
const PROPS_MATCH = /Props$/g; | ||
const VARIANT_MATCH = /Variants?$/g; | ||
const POSITION_MATCH = /Position$/g; | ||
const SIZE_MATCH = /Sizes?$/g; | ||
|
||
function filterNonStableLocation(location: string) { | ||
return !location.includes('next') && !location.includes('deprecated'); | ||
} | ||
|
||
function getPossibleLocations(roots: string[], nameBinding: string) { | ||
let moduleLocation = findFirstGlob(roots, `dist/esm/**/${nameBinding}.js`, filterNonStableLocation); | ||
|
||
if (!moduleLocation && nameBinding.match(PROPS_MATCH)) { | ||
moduleLocation = findFirstGlob(roots, `dist/esm/**/${nameBinding.replace(PROPS_MATCH, '')}.js`, filterNonStableLocation); | ||
} | ||
|
||
if (!moduleLocation && nameBinding.match(VARIANT_MATCH)) { | ||
moduleLocation = findFirstGlob(roots, `dist/esm/**/${nameBinding.replace(VARIANT_MATCH, '')}.js`, filterNonStableLocation); | ||
} | ||
|
||
if (!moduleLocation && nameBinding.match(POSITION_MATCH)) { | ||
moduleLocation = findFirstGlob(roots, `dist/esm/**/${nameBinding.replace(POSITION_MATCH, '')}.js`, filterNonStableLocation); | ||
} | ||
|
||
if (!moduleLocation && nameBinding.match(SIZE_MATCH)) { | ||
moduleLocation = findFirstGlob(roots, `dist/esm/**/${nameBinding.replace(SIZE_MATCH, '')}.js`, filterNonStableLocation); | ||
} | ||
|
||
return moduleLocation; | ||
} | ||
|
||
function getModuleExplicitLocation(roots: string[], relativePath: string) { | ||
const defaultLocation = findFirstGlob(roots, `dist/dynamic/**/${relativePath}`, filterNonStableLocation)?.split('/dynamic/').pop(); | ||
|
||
if (defaultLocation) { | ||
return defaultLocation; | ||
} | ||
|
||
throw new Error(`Could not find source file for ${relativePath} in any of ${roots}!`); | ||
} | ||
|
||
// Prefilled with modules which name bindings do not match the import specifier | ||
let HARDCODED_COMPONENTS: { | ||
[nameBinding: string]: string; | ||
} = {}; | ||
|
||
if (CORE_DIRECTORIES.length > 0) { | ||
HARDCODED_COMPONENTS = { | ||
getResizeObserver: getModuleExplicitLocation(CORE_DIRECTORIES, 'helpers/resizeObserver'), | ||
useOUIAProps: getModuleExplicitLocation(CORE_DIRECTORIES, 'helpers/OUIA/ouia'), | ||
OUIAProps: getModuleExplicitLocation(CORE_DIRECTORIES, 'helpers/OUIA/ouia'), | ||
getDefaultOUIAId: getModuleExplicitLocation(CORE_DIRECTORIES, 'helpers/OUIA/ouia'), | ||
useOUIAId: getModuleExplicitLocation(CORE_DIRECTORIES, 'helpers/OUIA/ouia'), | ||
handleArrows: getModuleExplicitLocation(CORE_DIRECTORIES, 'helpers/KeyboardHandler'), | ||
setTabIndex: getModuleExplicitLocation(CORE_DIRECTORIES, 'helpers/KeyboardHandler'), | ||
IconComponentProps: getModuleExplicitLocation(CORE_DIRECTORIES, 'components/Icon'), | ||
TreeViewDataItem: getModuleExplicitLocation(CORE_DIRECTORIES, 'components/TreeView'), | ||
Popper: getModuleExplicitLocation(CORE_DIRECTORIES, 'helpers/Popper/Popper'), | ||
clipboardCopyFunc: getModuleExplicitLocation(CORE_DIRECTORIES, 'components/ClipboardCopy'), | ||
ToolbarChipGroup: getModuleExplicitLocation(CORE_DIRECTORIES, 'components/Toolbar'), | ||
DatePickerRef: getModuleExplicitLocation(CORE_DIRECTORIES, 'components/DatePicker'), | ||
ButtonType: getModuleExplicitLocation(CORE_DIRECTORIES, 'components/Button'), | ||
PaginationTitles: getModuleExplicitLocation(CORE_DIRECTORIES, 'components/Pagination'), | ||
ProgressMeasureLocation: getModuleExplicitLocation(CORE_DIRECTORIES, 'components/Progress'), | ||
isValidDate: getModuleExplicitLocation(CORE_DIRECTORIES, 'helpers/datetimeUtils'), | ||
ValidatedOptions: getModuleExplicitLocation(CORE_DIRECTORIES, 'helpers/constants'), | ||
capitalize: getModuleExplicitLocation(CORE_DIRECTORIES, 'helpers/util'), | ||
WizardFooterWrapper: getModuleExplicitLocation(CORE_DIRECTORIES, 'components/Wizard'), | ||
WizardFooter: getModuleExplicitLocation(CORE_DIRECTORIES, 'components/Wizard'), | ||
WizardContextProvider: getModuleExplicitLocation(CORE_DIRECTORIES, 'components/Wizard'), | ||
useWizardContext: getModuleExplicitLocation(CORE_DIRECTORIES, 'components/Wizard'), | ||
DataListWrapModifier: getModuleExplicitLocation(CORE_DIRECTORIES, 'components/DataList'), | ||
MenuToggleElement: getModuleExplicitLocation(CORE_DIRECTORIES, 'components/MenuToggle'), | ||
}; | ||
} | ||
|
||
function guessComponentModule(nameBinding: string) { | ||
let modulePath = HARDCODED_COMPONENTS[nameBinding]; | ||
if (modulePath) { | ||
return modulePath; | ||
} | ||
|
||
const sourceGlob = getPossibleLocations(CORE_DIRECTORIES, nameBinding); | ||
const sourceFile = sourceGlob ? glob.sync(sourceGlob) : []; | ||
if (sourceFile.length < 1) { | ||
throw new Error( | ||
`Unable to find source file for module ${nameBinding}! The module likely does not have unique file as is included within another file. Please add the entry into the COMPONENTS_CACHE in FEC repository` | ||
); | ||
} | ||
const moduleSource: string[] = sourceFile[0].split('esm').pop()?.split('/') || []; | ||
moduleSource?.pop(); | ||
modulePath = moduleSource?.join('/').replace(/^\//, ''); | ||
return modulePath; | ||
} | ||
|
||
export default guessComponentModule; |
Oops, something went wrong.