(react) add tokens.ts files handling

These files will be used to define the custom design tokens per components.
They are automatically aggregated by the packages/react/cunningham.ts file,
this is why handling typescript config file was important.
This commit is contained in:
Nathan Vasse
2023-01-04 15:52:24 +01:00
committed by NathanVss
parent 67dd0048d0
commit be1c9d000b
19 changed files with 147 additions and 29 deletions

View File

@@ -0,0 +1,32 @@
import { glob } from "glob";
import { defaultTokens } from "@openfun/cunningham-tokens";
/**
* 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 = glob.sync("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);
}
components[componentName] = res.tokens(defaultTokens);
});
export default {
components,
};