Skip to content

Commit

Permalink
feat: Better support for multiple themes
Browse files Browse the repository at this point in the history
This change adds support for two CLI options to the build-tokens
command.

The first, --all-themes makes the build-tokens command process all
themes in the source directory as opposed to specifying all the names
via --theme.

Secondly, --base-theme allows you to have multiple themes derived from
the same common base. If specified, you can have a derived theme that
still loads tokens from the light theme. For example, you can have a
site theme called 'theme-one' and set the base theme to light so it will
reuse the core light theme tokens and layer on changes from 'theme-one'.
  • Loading branch information
xitij2000 committed Nov 9, 2023
1 parent 56836f0 commit 3574ba7
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions lib/build-tokens.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const fs = require('fs');
const path = require('path');
const minimist = require('minimist');
const { StyleDictionary, colorTransform, createCustomCSSVariables } = require('../tokens/style-dictionary');
Expand All @@ -9,13 +10,17 @@ const { createIndexCssFile } = require('../tokens/utils');
* @param {string[]} commandArgs - Command line arguments for building tokens.
* @param {string} [commandArgs.build-dir='./build/'] - The directory where the build output will be placed.
* @param {string} [commandArgs.source] - The source directory containing JSON token files.
* @param {string} [commandArgs.base-theme] - The base theme to use from Paragon if named differently than the theme name.

Check failure on line 13 in lib/build-tokens.js

View workflow job for this annotation

GitHub Actions / tests

This line has a length of 122. Maximum allowed is 120
* @param {boolean} [commandArgs.source-tokens-only=false] - Indicates whether to include only source tokens.
* @param {string|string[]} [commandArgs.themes=['light']] - The themes (variants) for which to build tokens.
* @param {boolean} [commandArgs.all-themes] - Indicated whether to process all themes.
*/
async function buildTokensCommand(commandArgs) {
const defaultParams = {
themes: ['light'],
themes: 'light',
'base-theme': null,
'build-dir': './build/',
'all-themes': false,
};

const alias = {
Expand All @@ -28,7 +33,20 @@ async function buildTokensCommand(commandArgs) {
source: tokensSource,
'source-tokens-only': hasSourceTokensOnly,
themes,
} = minimist(commandArgs, { alias, default: defaultParams, boolean: 'source-tokens-only' });
'base-theme': baseTheme,
'all-themes': allThemes,
} = minimist(commandArgs, { alias, default: defaultParams, boolean: ['source-tokens-only', 'all-themes'] });

let themesToProcess = null;

if (allThemes) {
themesToProcess = fs
.readdirSync(`${tokensSource}/themes/`, { withFileTypes: true })
.filter(entry => entry.isDirectory())
.map(entry => entry.name);
} else {
themesToProcess = themes.split(' ');
}

const coreConfig = {
include: [path.resolve(__dirname, '../tokens/src/core/**/*.json')],
Expand Down Expand Up @@ -65,9 +83,9 @@ async function buildTokensCommand(commandArgs) {
},
};

const getStyleDictionaryConfig = (themeVariant) => ({
const getStyleDictionaryConfig = (themeVariant, baseThemeVariant) => ({
...coreConfig,
include: [...coreConfig.include, path.resolve(__dirname, `../tokens/src/themes/${themeVariant}/**/*.json`)],
include: [...coreConfig.include, path.resolve(__dirname, `../tokens/src/themes/${baseThemeVariant}/**/*.json`)],
source: tokensSource ? [`${tokensSource}/themes/${themeVariant}/**/*.json`] : [],
transform: {
'color/sass-color-functions': {
Expand Down Expand Up @@ -109,8 +127,8 @@ async function buildTokensCommand(commandArgs) {
StyleDictionary.extend(coreConfig).buildAllPlatforms();
createIndexCssFile({ buildDir, isTheme: false });

themes.forEach((themeVariant) => {
const config = getStyleDictionaryConfig(themeVariant);
themesToProcess.forEach((themeVariant) => {
const config = getStyleDictionaryConfig(themeVariant, baseTheme || themeVariant);
StyleDictionary.extend(config).buildAllPlatforms();
createIndexCssFile({ buildDir, isTheme: true, themeVariant });
});
Expand Down

0 comments on commit 3574ba7

Please sign in to comment.