Skip to content

Commit

Permalink
fixup! Feat(exporters): Variables exporter export new token structure…
Browse files Browse the repository at this point in the history
… #DS-1435
  • Loading branch information
curdaj committed Aug 26, 2024
1 parent 4eaa8bb commit db01c9a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
4 changes: 2 additions & 2 deletions exporters/variables-scss/generated/exporter.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Token, TokenGroup, TokenType } from '@supernovaio/sdk-exporters';
import { exportConfiguration } from '../../index';
import { generateCssFromTokens, generateCssObjectFromTokens } from './cssGenerator';

// add disclaimer to top of the content
const addDisclaimer = (content: string): string => {
if (exportConfiguration.generateDisclaimer) {
return `/* This file was generated by Supernova, don't change manually */\n${content}`;
Expand All @@ -17,7 +18,7 @@ export const generateFileContent = (
tokenTypes: TokenType | TokenType[],
groupNames: string | string[],
withCssObject: boolean,
withParent: boolean = false,
hasParentPrefix: boolean = false,
) => {
let cssTokens = '';
let cssObject = '';
Expand All @@ -35,11 +36,11 @@ export const generateFileContent = (
);

// generate css tokens
cssTokens += generateCssFromTokens(filteredTokens, mappedTokens, tokenGroups, withParent);
cssTokens += generateCssFromTokens(filteredTokens, mappedTokens, tokenGroups, hasParentPrefix);
cssTokens += '\n\n';

// generate css object
const tempCssObject = generateCssObjectFromTokens(filteredTokens, mappedTokens, tokenGroups, withParent);
const tempCssObject = generateCssObjectFromTokens(filteredTokens, mappedTokens, tokenGroups, hasParentPrefix);
if (tempCssObject !== null) {
cssObject += tempCssObject;
}
Expand Down
10 changes: 5 additions & 5 deletions exporters/variables-scss/src/content/generators/cssGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ export const generateCssFromTokens = (
tokens: Token[],
mappedTokens: Map<string, Token>,
tokenGroups: Array<TokenGroup>,
withParent: boolean,
hasParentPrefix: boolean,
): string => {
return tokens
.map((token) => tokenToCSSByType(token, mappedTokens, tokenGroups, withParent))
.map((token) => tokenToCSSByType(token, mappedTokens, tokenGroups, hasParentPrefix))
.filter(Boolean)
.join('\n');
};
Expand All @@ -47,7 +47,7 @@ const generateObjectContent = (tokens: Array<Token>, tokenGroups: Array<TokenGro
tokens.forEach((token) => {
const name = tokenVariableName(token, tokenGroups, withParent);
const numericPart = name.match(/\d+/)?.[0];
const prefix = `${token.origin?.name?.split('/')[0].toLowerCase()}-`; // Use template literal
const prefix = `${token.origin?.name?.split('/')[0].toLowerCase()}-`;
const nonNumericPart = name.replace(prefix, '');
if (numericPart) {
result += `${numericPart}: $${name},\n`;
Expand All @@ -63,7 +63,7 @@ export const generateCssObjectFromTokens = (
tokens: Array<Token>,
mappedTokens: Map<string, Token>,
tokenGroups: Array<TokenGroup>,
withParent: boolean,
hasParentPrefix: boolean,
): string | null => {
// Create a map where the key is the origin name and the value is an array of tokens
const originNameMap = new Map<string, Array<Token>>();
Expand All @@ -83,7 +83,7 @@ export const generateCssObjectFromTokens = (

// For each key in the map, generate an object and add it to the result css
originNameMap.forEach((token, objectName) => {
const objectContent = generateObjectContent(token, tokenGroups, withParent);
const objectContent = generateObjectContent(token, tokenGroups, hasParentPrefix);
if (objectContent.trim() !== '') {
result += `$${objectName}: (\n${objectContent}) !default;\n\n`;
}
Expand Down
12 changes: 6 additions & 6 deletions exporters/variables-scss/src/content/generators/fileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ const filesData = [
tokenTypes: TokenType.dimension,
groupNames: 'Spacing',
withCssObject: true,
withParent: false,
hasParentPrefix: false,
},
{
fileName: '_radii.scss',
tokenTypes: TokenType.dimension,
groupNames: 'Radius',
withCssObject: true,
withParent: false,
hasParentPrefix: false,
},
{
fileName: '_borders.scss',
tokenTypes: TokenType.dimension,
groupNames: 'Border',
withCssObject: false,
withParent: true,
hasParentPrefix: true,
},
{
fileName: '_other.scss',
tokenTypes: [TokenType.dimension, TokenType.string],
groupNames: ['Grid', 'Container', 'Breakpoint'],
withCssObject: true,
withParent: true,
hasParentPrefix: true,
},
];

Expand All @@ -37,15 +37,15 @@ export const generateFiles = (
mappedTokens: Map<string, Token>,
tokenGroups: Array<TokenGroup>,
) => {
return filesData.map(({ fileName, tokenTypes, groupNames, withCssObject, withParent }) => {
return filesData.map(({ fileName, tokenTypes, groupNames, withCssObject, hasParentPrefix }) => {
const fileContent = generateFileContent(
tokens,
mappedTokens,
tokenGroups,
tokenTypes,
groupNames,
withCssObject,
withParent,
hasParentPrefix,
);

return {
Expand Down

0 comments on commit db01c9a

Please sign in to comment.