|
| 1 | +#!/usr/bin/env node |
| 2 | +(async function () { |
| 3 | + const StyleDictionary = require('style-dictionary'); |
| 4 | + const path = require('path'); |
| 5 | + const fs = require('fs'); |
| 6 | + const { getConfig } = require('./_util'); |
| 7 | + |
| 8 | + let packageRootPath; |
| 9 | + try { |
| 10 | + packageRootPath = |
| 11 | + path.dirname(require.resolve('@chanzuckerberg/eds')) + '/tokens/json/'; |
| 12 | + } catch (e) { |
| 13 | + console.error('EDS package not installed. Using local path...'); |
| 14 | + packageRootPath = |
| 15 | + path.dirname(require.main.path) + '/src/tokens-dist/json/'; |
| 16 | + } |
| 17 | + |
| 18 | + // Read the config to sort out where to read JSON from and where to write the CSS file |
| 19 | + const config = await getConfig(); |
| 20 | + |
| 21 | + // read and parse JSON files on disk |
| 22 | + const localTheme = JSON.parse( |
| 23 | + fs.readFileSync(`${config.json}app-theme.json`, 'utf8'), |
| 24 | + ); |
| 25 | + const baseTheme = JSON.parse( |
| 26 | + fs.readFileSync(`${packageRootPath}theme-base.json`, 'utf8'), |
| 27 | + ); |
| 28 | + |
| 29 | + // define the header to use in the resulting CSS file so people know not to edit it directly |
| 30 | + StyleDictionary.registerFileHeader({ |
| 31 | + name: 'cssOverrideHeader', |
| 32 | + fileHeader: (defaultMessage) => [ |
| 33 | + ...defaultMessage, |
| 34 | + 'To update, edit app-theme.json, then run `npx eds-apply-theme`', |
| 35 | + ], |
| 36 | + }); |
| 37 | + |
| 38 | + const EDSStyleDictionary = StyleDictionary.extend({ |
| 39 | + source: [config.json + 'app-theme.json'], |
| 40 | + platforms: { |
| 41 | + css: { |
| 42 | + transforms: [...StyleDictionary.transformGroup.css, 'name/cti/kebab'], |
| 43 | + buildPath: config.css, |
| 44 | + files: [ |
| 45 | + { |
| 46 | + format: 'css/variables', |
| 47 | + destination: 'app-theme.css', |
| 48 | + options: { |
| 49 | + fileHeader: 'cssOverrideHeader', |
| 50 | + }, |
| 51 | + filter: function (token) { |
| 52 | + // don't allow theming on legacy tokens |
| 53 | + return token.attributes.category !== 'legacy'; |
| 54 | + }, |
| 55 | + }, |
| 56 | + ], |
| 57 | + }, |
| 58 | + }, |
| 59 | + }); |
| 60 | + |
| 61 | + /** |
| 62 | + * Determine if the given theme file is a subset of what's in the base theme file. |
| 63 | + * If it isnt, throw an error: |
| 64 | + * - If keys are in base that are missing in the theme file, that's OK (no need to override everything) |
| 65 | + * - If keys are in theme that aren't in base, throw (you can't theme tokens that don't exist in EDS) |
| 66 | + * @param {object} base The tokens theme file stored in the EDS project |
| 67 | + * @param {object} theme The project theme file stored in the app code (same format as bas) |
| 68 | + * @param {Array} path The base path, stored as an array of object key names (default []) |
| 69 | + * @throws Error when there are tokens in theme that aren't in base |
| 70 | + */ |
| 71 | + function isStrictSubset(base, theme, path = []) { |
| 72 | + for (const name in theme) { |
| 73 | + if (typeof theme[name] === 'object') { |
| 74 | + if (base[name] === undefined) { |
| 75 | + throw new Error( |
| 76 | + `Local themeable value does not exist in base theme: --${path.join( |
| 77 | + '-', |
| 78 | + )}.${name}"`, |
| 79 | + ); |
| 80 | + } |
| 81 | + isStrictSubset(base[name], theme[name], path.concat(name)); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + try { |
| 87 | + // Keys in the theme file must be a strict subset of those in the base file |
| 88 | + isStrictSubset(baseTheme, localTheme); |
| 89 | + EDSStyleDictionary.buildAllPlatforms(); |
| 90 | + } catch (error) { |
| 91 | + // TODO: if theme has things not in base, error showing where the conflict |
| 92 | + console.error('EDS theming error:', error.message); |
| 93 | + return; |
| 94 | + } |
| 95 | +})(); |
0 commit comments