diff --git a/packages/react-core/package.json b/packages/react-core/package.json index 032902b4ed1..9e8a7633e9d 100644 --- a/packages/react-core/package.json +++ b/packages/react-core/package.json @@ -42,7 +42,7 @@ "build:umd": "rollup -c --environment IS_PRODUCTION", "clean": "rimraf dist", "generate": "node scripts/copyStyles.js", - "subpaths": "node scripts/copySubpaths.js" + "subpaths": "node scripts/createSubpaths.js" }, "dependencies": { "@patternfly/react-icons": "^5.0.0-alpha.6", diff --git a/packages/react-core/scripts/copySubpaths.js b/packages/react-core/scripts/copySubpaths.js deleted file mode 100644 index cffb00dd7ce..00000000000 --- a/packages/react-core/scripts/copySubpaths.js +++ /dev/null @@ -1,12 +0,0 @@ -/** - * Copy subpath modules into the package root directory for ease of access. - */ -const { copySync } = require('fs-extra'); -const { resolve, dirname } = require('path'); - -['next', 'deprecated', 'components', 'layouts', 'helpers'].forEach(subPathName => { - const source = dirname(require.resolve(`@patternfly/react-core/dist/esm/${subPathName}`)); - const destination = resolve(__dirname, `../${subPathName}`); - - copySync(source, destination); -}); diff --git a/packages/react-core/scripts/createSubpaths.js b/packages/react-core/scripts/createSubpaths.js new file mode 100644 index 00000000000..01e75eeab44 --- /dev/null +++ b/packages/react-core/scripts/createSubpaths.js @@ -0,0 +1,32 @@ +/** + * Re-export subpath modules into the package root directory for ease of access. + */ +const { readdirSync, readFileSync, writeFileSync, mkdirSync } = require('fs'); +const { resolve, join } = require('path'); + +const isIndex = (fileName) => fileName.match(/^index\./); + +const updatePaths = (filePath, subPathName) => { + const contents = readFileSync(filePath, { encoding: 'utf8' }); + if (filePath.includes('.map')) { + return contents.replaceAll('../../../', `../`); + } else { + return contents.replaceAll('./', `../dist/esm/${subPathName}/`); + } +}; + +['next', 'deprecated', 'components', 'layouts', 'helpers'].forEach((subPathName) => { + const distPath = resolve(__dirname, `../dist/esm/${subPathName}`); + const distFileNames = readdirSync(distPath); + const distIndexFileNames = distFileNames.filter((fileName) => isIndex(fileName)); + + const destinationDir = resolve(__dirname, `../${subPathName}`); + mkdirSync(destinationDir, { recursive: true }); + + distIndexFileNames.forEach((fileName) => { + const filePath = join(__dirname, '../dist/esm', subPathName, fileName); + const newFileContent = updatePaths(filePath, subPathName); + const fileDestination = join(destinationDir, fileName); + writeFileSync(fileDestination, newFileContent); + }); +}); diff --git a/packages/react-core/src/deprecated/components/OptionsMenu/__tests__/Generated/OptionsMenuItem.test.tsx b/packages/react-core/src/deprecated/components/OptionsMenu/__tests__/Generated/OptionsMenuItem.test.tsx index df7d91ce3de..b7f03895110 100644 --- a/packages/react-core/src/deprecated/components/OptionsMenu/__tests__/Generated/OptionsMenuItem.test.tsx +++ b/packages/react-core/src/deprecated/components/OptionsMenu/__tests__/Generated/OptionsMenuItem.test.tsx @@ -3,7 +3,7 @@ import React from 'react'; import { render } from '@testing-library/react'; import { OptionsMenuItem } from '../../OptionsMenuItem'; -import { DropdownArrowContext } from '../../../../../../components/Dropdown'; +import { DropdownArrowContext } from '../../../../../components/Dropdown'; describe('OptionsMenuItem', () => { it('should match snapshot', () => { diff --git a/packages/react-table/package.json b/packages/react-table/package.json index ca90cb358b6..1a69479d077 100644 --- a/packages/react-table/package.json +++ b/packages/react-table/package.json @@ -36,7 +36,7 @@ "scripts": { "build:umd": "rollup -c --environment IS_PRODUCTION", "clean": "rimraf dist", - "subpaths": "node scripts/copySubpaths.js" + "subpaths": "node scripts/createSubpaths.js" }, "dependencies": { "@patternfly/react-core": "^5.0.0-alpha.42", diff --git a/packages/react-table/scripts/copySubpaths.js b/packages/react-table/scripts/copySubpaths.js deleted file mode 100644 index ff6ef28fc38..00000000000 --- a/packages/react-table/scripts/copySubpaths.js +++ /dev/null @@ -1,12 +0,0 @@ -/** - * Copy subpath modules into the package root directory for ease of access. - */ -const { copySync } = require('fs-extra'); -const { resolve, dirname } = require('path'); - -['deprecated', 'components'].forEach(subPathName => { - const source = dirname(require.resolve(`@patternfly/react-table/dist/esm/${subPathName}`)); - const destination = resolve(__dirname, `../${subPathName}`); - - copySync(source, destination); -}); diff --git a/packages/react-table/scripts/createSubpaths.js b/packages/react-table/scripts/createSubpaths.js new file mode 100644 index 00000000000..dfab6cb0255 --- /dev/null +++ b/packages/react-table/scripts/createSubpaths.js @@ -0,0 +1,32 @@ +/** + * Re-export subpath modules into the package root directory for ease of access. + */ +const { readdirSync, readFileSync, writeFileSync, mkdirSync } = require('fs'); +const { resolve, join } = require('path'); + +const isIndex = (fileName) => fileName.match(/^index\./); + +const updatePaths = (filePath, subPathName) => { + const contents = readFileSync(filePath, { encoding: 'utf8' }); + if (filePath.includes('.map')) { + return contents.replaceAll('../../../', `../`); + } else { + return contents.replaceAll('./', `../dist/esm/${subPathName}/`); + } +}; + +['deprecated', 'components'].forEach((subPathName) => { + const distPath = resolve(__dirname, `../dist/esm/${subPathName}`); + const distFileNames = readdirSync(distPath); + const distIndexFileNames = distFileNames.filter((fileName) => isIndex(fileName)); + + const destinationDir = resolve(__dirname, `../${subPathName}`); + mkdirSync(destinationDir, { recursive: true }); + + distIndexFileNames.forEach((fileName) => { + const filePath = join(__dirname, '../dist/esm', subPathName, fileName); + const newFileContent = updatePaths(filePath, subPathName); + const fileDestination = join(destinationDir, fileName); + writeFileSync(fileDestination, newFileContent); + }); +});