Files
cunningham/packages/react/cunningham.ts
Nathan Vasse 74abf9b8a7 🔧(react) format correctly sub folders components
Without this commit, the design tokens would contain the folders
"/" in their names, which is not compliant with most of generators
output we implement.
2023-04-25 11:28:32 +02:00

34 lines
1.2 KiB
TypeScript

import { defaultTokens } from "@openfun/cunningham-tokens";
import { globSync } from "glob";
/**
* This scripts dynamically imports all tokens.ts files from components and load them in a single object that will be
* exported as the local configuration of cunningham under the `components` key.
*
* Hence, any consumers of this package will be able to customize the tokens of the components they use by overriding
* them in their own local configuration file. ( cunningham.ts|js )
*/
const components: any = {};
const files = globSync("src/components/**/tokens.ts");
files.forEach((file) => {
const importPath = "./" + file.replace(/\.ts$/, "");
const matches = /^.+components\/(.+)\/tokens$/gm.exec(importPath);
let componentName = matches && matches[1];
if (!componentName) {
throw new Error("Could not find component name from file path " + file);
}
componentName = componentName.toLowerCase();
const res = require(importPath);
if (!res.tokens) {
throw new Error("Tokens file does not export tokens " + file);
}
componentName = componentName.replace("/", "-");
components[componentName] = res.tokens(defaultTokens);
});
export default {
components,
};