Skip to content

Commit

Permalink
chore(tokens): Improve design tokens import script (#1288)
Browse files Browse the repository at this point in the history
Sample execution via github actions:
https://github.com/Telefonica/mistica-web/actions/runs/11702860436

---------

Co-authored-by: Pedro Ladaria <[email protected]>
  • Loading branch information
pladaria and Pedro Ladaria authored Nov 8, 2024
1 parent 8d91924 commit 007f4f8
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/generate-design-tokens/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.yarn
Binary file removed packages/generate-design-tokens/.yarn/install-state.gz
Binary file not shown.
53 changes: 43 additions & 10 deletions packages/generate-design-tokens/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path';
import url from 'url';
// eslint-disable-next-line import/extensions
import {generateSkinCssSrc, buildRadius, generateCommonCssSrc} from './css-generator.js';
import prettier from 'prettier';

/*
By default, this script will look for the design tokens inside .github folder but you may want to clone the mistica-design repo elsewhere.
Expand All @@ -16,12 +17,15 @@ To run this script locally using a custom path for the tokens, you can do the fo
DESIGN_TOKENS_FOLDER="/path/to/mistica-design/tokens" node index.js
*/

const DESIGN_TOKENS_FOLDER = process.env.DESIGN_TOKENS_FOLDER || '../../../mistica-design/tokens/';

// in node >= 20 we could use import.meta.dirname instead
// @ts-ignore
const currentDir = url.fileURLToPath(new URL('.', import.meta.url));

const DESIGN_TOKENS_FOLDER =
process.env.DESIGN_TOKENS_FOLDER || path.join(currentDir, '../../.github/mistica-design/tokens/');

console.log('Using design tokens from:', DESIGN_TOKENS_FOLDER);

const SKINS_FOLDER = path.join(currentDir, '..', '..', 'src', 'skins');
const CSS_FOLDER = path.join(currentDir, '..', '..', 'css');

Expand All @@ -31,6 +35,23 @@ const capitalize = (str) => str.charAt(0).toUpperCase() + str.slice(1);
const toCamelCase = (str) => str.replace(/-([a-z])/g, (g) => g[1].toUpperCase());
const toPascalCase = (str) => capitalize(toCamelCase(str));

const jsonSort = (obj) => {
if (Array.isArray(obj)) {
return obj.map(jsonSort);
}

if (typeof obj !== 'object' || obj === null) {
return obj;
}

return Object.keys(obj)
.sort((a, b) => a.localeCompare(b, undefined, {numeric: true, sensitivity: 'base'}))
.reduce((acc, key) => {
acc[key] = jsonSort(obj[key]);
return acc;
}, {});
};

/**
* @param {{angle: number, colors: Array<{
* value: string,
Expand Down Expand Up @@ -123,7 +144,7 @@ export const get${toPascalCase(skinName)}Skin: GetKnownSkin = () => {
)
.join(',')}
},
textPresets: ${JSON.stringify(textTokens)},
textPresets: ${JSON.stringify(jsonSort(textTokens))},
};
return skin;
};
Expand All @@ -142,31 +163,43 @@ export type Colors = {
};`;
};

const generateSkinFiles = () => {
const formatCss = async (source) =>
prettier.format(source, {
...(await prettier.resolveConfig('.')),
parser: 'css',
});

const formatTs = async (source) =>
prettier.format(source, {
...(await prettier.resolveConfig('.')),
parser: 'typescript',
});

const generateSkinFiles = async () => {
let anyGeneratedSkin;

KNOWN_SKINS.forEach((skinName) => {
for (const skinName of KNOWN_SKINS) {
console.log('Generating tokens for skin', skinName);

if (!fs.existsSync(path.join(DESIGN_TOKENS_FOLDER, `${skinName}.json`))) {
console.error(`Missing ${skinName}.json file`);
return;
}

const skinSrc = generateSkinSrc(skinName);
const skinSrc = await formatTs(generateSkinSrc(skinName));
fs.writeFileSync(path.join(SKINS_FOLDER, `${skinName}.tsx`), skinSrc);

const skinCssSrc = generateSkinCssSrc(skinName, DESIGN_TOKENS_FOLDER);
const skinCssSrc = await formatCss(generateSkinCssSrc(skinName, DESIGN_TOKENS_FOLDER));
fs.writeFileSync(path.join(CSS_FOLDER, `${skinName}.css`), skinCssSrc);

anyGeneratedSkin = skinName;
});
}

if (anyGeneratedSkin) {
const typesSrc = generateColorTypesSrc(anyGeneratedSkin);
const typesSrc = await formatTs(generateColorTypesSrc(anyGeneratedSkin));
fs.writeFileSync(path.join(SKINS_FOLDER, 'types', 'colors.tsx'), typesSrc);

const commonCssSrc = generateCommonCssSrc(DESIGN_TOKENS_FOLDER);
const commonCssSrc = await formatCss(generateCommonCssSrc(DESIGN_TOKENS_FOLDER));
fs.writeFileSync(path.join(CSS_FOLDER, `mistica-common.css`), commonCssSrc);
}
};
Expand Down
7 changes: 6 additions & 1 deletion packages/generate-design-tokens/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"name": "generate-design-tokens",
"type": "module",
"packageManager": "[email protected]"
"packageManager": "[email protected]",
"dependencies": {
"@telefonica/prettier-config": "^2.0.0",
"prettier": "^3.3.3"
},
"prettier": "@telefonica/prettier-config"
}
22 changes: 22 additions & 0 deletions packages/generate-design-tokens/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,31 @@

__metadata:
version: 6
cacheKey: 8

"@telefonica/prettier-config@npm:^2.0.0":
version: 2.0.0
resolution: "@telefonica/prettier-config@npm:2.0.0"
peerDependencies:
prettier: ^3.0.0
checksum: b7fe4ee936195f26b1b1977236b97ec241afa01b12d72d801272788bf9699cd4b9a6e3feccde86bd88b311335f0cfd39ec0b53a9bb44dacf5c01b5627729f174
languageName: node
linkType: hard

"generate-design-tokens@workspace:.":
version: 0.0.0-use.local
resolution: "generate-design-tokens@workspace:."
dependencies:
"@telefonica/prettier-config": ^2.0.0
prettier: ^3.3.3
languageName: unknown
linkType: soft

"prettier@npm:^3.3.3":
version: 3.3.3
resolution: "prettier@npm:3.3.3"
bin:
prettier: bin/prettier.cjs
checksum: bc8604354805acfdde6106852d14b045bb20827ad76a5ffc2455b71a8257f94de93f17f14e463fe844808d2ccc87248364a5691488a3304f1031326e62d9276e
languageName: node
linkType: hard

1 comment on commit 007f4f8

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for mistica-web ready!

✅ Preview
https://mistica-md41a3mzm-flows-projects-65bb050e.vercel.app

Built with commit 007f4f8.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.