|
| 1 | +import { glob } from "glob"; |
| 2 | +import { writeFile } from "node:fs/promises"; |
| 3 | +import { join, resolve } from "node:path"; |
| 4 | +import { format } from "prettier"; |
| 5 | + |
| 6 | +import { alphaNumericSort } from "../src/utils/alphaNumericSort.js"; |
| 7 | +import { |
| 8 | + GENERATED_FILE_BANNER, |
| 9 | + PRIVATE_FILES, |
| 10 | + SYNCED_EXPORTS, |
| 11 | +} from "./constants.js"; |
| 12 | + |
| 13 | +const write = async (filePath: string, contents: string): Promise<void> => { |
| 14 | + await writeFile( |
| 15 | + filePath, |
| 16 | + await format( |
| 17 | + `${GENERATED_FILE_BANNER} |
| 18 | +
|
| 19 | +${contents} |
| 20 | +`, |
| 21 | + { filepath: filePath } |
| 22 | + ) |
| 23 | + ); |
| 24 | + console.log( |
| 25 | + `Wrote ${filePath.replace(resolve("../react-md"), "packages/react-md")}` |
| 26 | + ); |
| 27 | +}; |
| 28 | + |
| 29 | +const coreSrc = join(process.cwd(), "src"); |
| 30 | +const filesForBarrelFile = await glob("**/*.{ts,tsx}", { |
| 31 | + cwd: coreSrc, |
| 32 | + ignore: ["**/__tests__/**", "**/test-utils/**", ...PRIVATE_FILES], |
| 33 | +}); |
| 34 | + |
| 35 | +const barrelFileContents = alphaNumericSort(filesForBarrelFile) |
| 36 | + .map( |
| 37 | + (name) => `export * from "@react-md/core/${name.replace(/\.tsx?$/, "")}"` |
| 38 | + ) |
| 39 | + .join(";\n"); |
| 40 | +const indexFileName = resolve("../react-md/src/index.ts"); |
| 41 | +const configureMaterialSymbols = resolve( |
| 42 | + "../react-md/src/configureMaterialSymbols.ts" |
| 43 | +); |
| 44 | +await Promise.all([ |
| 45 | + write(indexFileName, barrelFileContents), |
| 46 | + ...SYNCED_EXPORTS.map((exportName) => { |
| 47 | + let prefix = "export * from"; |
| 48 | + if (exportName === "test-utils/data-testid") { |
| 49 | + prefix = "import"; |
| 50 | + } |
| 51 | + |
| 52 | + write( |
| 53 | + resolve(`../react-md/src/${exportName}.ts`), |
| 54 | + `${prefix} "@react-md/core/${exportName}"` |
| 55 | + ); |
| 56 | + }), |
| 57 | + write( |
| 58 | + configureMaterialSymbols, |
| 59 | + 'import "@react-md/core/icon/configureMaterialSymbols"' |
| 60 | + ), |
| 61 | +]); |
| 62 | + |
| 63 | +const packageJsonPath = resolve("../react-md/package.json"); |
| 64 | +const { default: packageJson } = await import(packageJsonPath, { |
| 65 | + with: { type: "json" }, |
| 66 | +}); |
| 67 | + |
| 68 | +packageJson.exports = { |
| 69 | + "./a11y": { |
| 70 | + sass: "./dist/_a11y.scss", |
| 71 | + }, |
| 72 | + "./colors": { |
| 73 | + sass: "./dist/_colors.scss", |
| 74 | + }, |
| 75 | + "./configureMaterialSymbols": "./dist/configureMaterialSymbols.js", |
| 76 | + "./test-utils": "./dist/test-utils.js", |
| 77 | + "./test-utils/*": "./dist/test-utils/*.js", |
| 78 | + ".": { |
| 79 | + sass: "./dist/_react-md.scss", |
| 80 | + types: "./dist/index.d.ts", |
| 81 | + default: "./dist/index.js", |
| 82 | + }, |
| 83 | + "./package.json": "./package.json", |
| 84 | +}; |
| 85 | + |
| 86 | +await writeFile( |
| 87 | + packageJsonPath, |
| 88 | + await format(JSON.stringify(packageJson), { filepath: packageJsonPath }) |
| 89 | +); |
0 commit comments