Skip to content

Commit

Permalink
chore(fuselage-tokens): Upgrade style-dictionary (#1480)
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoevan authored Nov 8, 2024
1 parent 5ac71a3 commit f064014
Show file tree
Hide file tree
Showing 10 changed files with 648 additions and 172 deletions.
3 changes: 3 additions & 0 deletions packages/fuselage-tokens/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@rocket.chat/eslint-config-alt/minimal"
}
3 changes: 0 additions & 3 deletions packages/fuselage-tokens/.eslintrc.js

This file was deleted.

26 changes: 14 additions & 12 deletions packages/fuselage-tokens/build.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const StyleDictionary = require('style-dictionary');
import StyleDictionary from 'style-dictionary';

console.log('Build started...');
console.log('\n==============================================');
Expand Down Expand Up @@ -41,12 +41,12 @@ const toScssValue = (chunk) => {

StyleDictionary.registerTransformGroup({
name: 'custom/mjs',
transforms: ['name/cti/camel'],
transforms: ['name/camel'],
});

StyleDictionary.registerFormat({
name: 'custom/colors-json',
formatter({ dictionary }) {
async format({ dictionary }) {
return `{${dictionary.allTokens.map(
(token) =>
`\n\t${encodeJson(token.path[1])}: ${encodeJson(token.original.value)}`,
Expand All @@ -56,7 +56,7 @@ StyleDictionary.registerFormat({

StyleDictionary.registerFormat({
name: 'custom/breakpoints-json',
formatter({ dictionary }) {
async format({ dictionary }) {
return `[${dictionary.allTokens.map(
(token) => `\n\t${encodeJson(token.original.value)}`,
)}\n]`;
Expand All @@ -65,7 +65,7 @@ StyleDictionary.registerFormat({

StyleDictionary.registerFormat({
name: 'cjsmodule',
formatter({ dictionary }) {
async format({ dictionary }) {
return `module.exports = {${dictionary.allTokens.map(
(token) =>
`\n\t${encodeJson(token.name)}: ${encodeJson(token.original.value)}`,
Expand All @@ -75,7 +75,7 @@ StyleDictionary.registerFormat({

StyleDictionary.registerFormat({
name: 'camelCase',
formatter({ dictionary }) {
async format({ dictionary }) {
// Get group name through folder name ./src/******
const exp = /[a-z]+\/([a-z]+)\/[a-z]+.json/i;
const [, group] = dictionary.allTokens[0].filePath.match(exp);
Expand All @@ -93,7 +93,7 @@ StyleDictionary.registerFormat({

StyleDictionary.registerFormat({
name: 'custom/mjs',
formatter({ dictionary }) {
async format({ dictionary }) {
return `export default {${dictionary.allTokens.map(
(token) =>
`\n\t${encodeJson(token.name)}: ${encodeJson(token.original.value)}`,
Expand All @@ -103,7 +103,7 @@ StyleDictionary.registerFormat({

StyleDictionary.registerFormat({
name: 'custom/colors-mjs',
formatter({ dictionary }) {
async format({ dictionary }) {
return `export default {${dictionary.allTokens.map(
(token) =>
`\n\t${encodeJson(token.path[1])}: ${encodeJson(token.original.value)}`,
Expand All @@ -113,7 +113,7 @@ StyleDictionary.registerFormat({

StyleDictionary.registerFormat({
name: 'custom/scss',
formatter({ dictionary }) {
async format({ dictionary }) {
// Get group name through folder name ./src/******
const exp = /[a-z]+\/([a-z]+)\/[a-z]+.json/i;
const [, group] = dictionary.allTokens[0].filePath.match(exp);
Expand Down Expand Up @@ -152,7 +152,7 @@ StyleDictionary.registerFormat({

StyleDictionary.registerFormat({
name: 'custom/typography-scss',
formatter({ dictionary }) {
async format({ dictionary }) {
return `${dictionary.allTokens
.map((token) => `$${token.name}: \n${toScssValue(token.value)};`)
.join('')}`;
Expand All @@ -161,10 +161,12 @@ StyleDictionary.registerFormat({

// APPLY THE CONFIGURATION
// needs to be done _before_ applying the configuration
const StyleDictionaryExtended = StyleDictionary.extend('./config.js');
const StyleDictionaryExtended = new StyleDictionary('./config.js');

await StyleDictionaryExtended.hasInitialized;

// Build all platforms
StyleDictionaryExtended.buildAllPlatforms();
await StyleDictionaryExtended.buildAllPlatforms();

console.log('\n==============================================');
console.log('\nBuild completed!');
4 changes: 2 additions & 2 deletions packages/fuselage-tokens/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const tokens = require('./src');
import tokens from './src/index.js';

module.exports = {
export default {
source: ['src/**/*.json'],
platforms: {
js: {
Expand Down
3 changes: 2 additions & 1 deletion packages/fuselage-tokens/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"publishConfig": {
"access": "public"
},
"type": "module",
"scripts": {
"build": "run-s .:build:clean .:build:legacy .:build",
".:build": "node ./build --config ./config.js",
Expand All @@ -50,7 +51,7 @@
"postcss-scss": "~4.0.6",
"prettier": "~3.3.3",
"rimraf": "^3.0.2",
"style-dictionary": "~3.7.2",
"style-dictionary": "~4.1.4",
"stylelint": "~16.10.0",
"stylelint-order": "~6.0.4",
"stylelint-prettier": "~5.0.2",
Expand Down
7 changes: 4 additions & 3 deletions packages/fuselage-tokens/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { readdirSync, statSync } = require('fs');
const { join } = require('path');
import { readdirSync, statSync } from 'fs';
import { join } from 'path';

const dirs = (p) =>
readdirSync(p).filter((f) => statSync(join(p, f)).isDirectory());
module.exports = dirs(__dirname);

export default dirs(import.meta.dirname);
1 change: 0 additions & 1 deletion packages/fuselage-tokens/typograhy.js

This file was deleted.

4 changes: 3 additions & 1 deletion tools/build-design-tokens/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ const buildTypography = async () => {
]);
};

await Promise.all([buildBreakpoints(), buildColors(), buildTypography()]);
buildBreakpoints();
buildColors();
buildTypography();
16 changes: 9 additions & 7 deletions tools/tools-utils/source.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { extname } from 'path';

import { ESLint } from 'eslint';
import stylelint from 'stylelint';
import prettier from 'prettier';
import stylelint from 'stylelint';

import { encodeJson } from './json.mjs';

export const runEslint = (path) => async (source) => {
Expand All @@ -24,13 +26,13 @@ export const runStylelint = (path) => async (source) => {
fix: true,
});

return results.output;
return results.code;
};

export const runPrettier = (path) => async (source) => {
const config = await prettier.resolveConfig(process.cwd());

return await prettier.format(source, {
return prettier.format(source, {
...config,
parser:
(extname(path) === '.json' && 'json') ||
Expand All @@ -41,22 +43,22 @@ export const runPrettier = (path) => async (source) => {

export const toJson = async (data) => {
const code = encodeJson(data);
return await runPrettier('index.json')(code);
return runPrettier('index.json')(code);
};

export const toCommonJsModule = async (data) => {
const code = `
'use strict';
module.exports = ${encodeJson(data)};
`;
return await runEslint('index.cjs')(code);
return runEslint('index.cjs')(code);
};

export const toEsmModule = async (data) => {
const code = `
export default ${encodeJson(data)};
`;
return await runEslint('index.mjs')(code);
return runEslint('index.mjs')(code);
};

export const toScssIdentifier = (string) =>
Expand Down Expand Up @@ -92,5 +94,5 @@ export const toScssVariables = async (data) => {
)
.join('');

return await runStylelint('index.scss')(code);
return runStylelint('index.scss')(code);
};
Loading

0 comments on commit f064014

Please sign in to comment.