-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(variables-scss): Formatting color tokens #DS-1461
- Loading branch information
Showing
18 changed files
with
447 additions
and
124 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 46 additions & 38 deletions
84
exporters/variables-scss/src/generators/cssObjectGenerator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,59 @@ | ||
import { Token, TokenGroup } from '@supernovaio/sdk-exporters'; | ||
import { toPlural } from '../helpers/stringHelper'; | ||
import { Token, TokenGroup, TokenType } from '@supernovaio/sdk-exporters'; | ||
import { tokenVariableName } from '../helpers/tokenHelper'; | ||
import { toPlural } from '../helpers/stringHelper'; | ||
|
||
export const generateObjectContent = ( | ||
tokens: Array<Token>, | ||
tokenGroups: Array<TokenGroup>, | ||
withParent: boolean, | ||
): string => { | ||
return tokens.reduce((result, token) => { | ||
const name = tokenVariableName(token, tokenGroups, withParent); | ||
const numericPart = name.match(/\d+/)?.[0]; | ||
const prefix = `${token.origin?.name?.split('/')[0].toLowerCase()}-`; | ||
const nonNumericPart = name.replace(prefix, ''); | ||
|
||
if (numericPart) { | ||
result += `${numericPart}: $${name},\n`; | ||
} else if (nonNumericPart) { | ||
result += `${nonNumericPart}: $${name},\n`; | ||
} | ||
export type CssObjectType = { [key: string]: string | object }; | ||
|
||
// TODO : add comments to this function | ||
// Handle invariant token aliases eg radius-full -> full | ||
const invariantTokenAlias: { [key: string]: string } = { | ||
'radius-full': 'full', | ||
}; | ||
|
||
export const getNonNumericPart = (tokenName: string): string => { | ||
if (invariantTokenAlias[tokenName]) { | ||
return invariantTokenAlias[tokenName]; | ||
} | ||
|
||
return result; | ||
}, ''); | ||
return tokenName.toLowerCase(); | ||
}; | ||
|
||
export const generateCssObjectFromTokens = ( | ||
tokens: Array<Token>, | ||
mappedTokens: Map<string, Token>, | ||
tokenGroups: Array<TokenGroup>, | ||
hasParentPrefix: boolean, | ||
): string => { | ||
const originNameMap = new Map<string, Array<Token>>(); | ||
tokens.forEach((token) => { | ||
const originName = token.origin?.name; | ||
|
||
if (originName) { | ||
const nameParts = originName.split('/'); | ||
nameParts.pop(); | ||
const objectName = toPlural(nameParts.join('-').toLowerCase()); | ||
originNameMap.set(objectName, [...(originNameMap.get(objectName) || []), token]); | ||
} | ||
}); | ||
): CssObjectType => { | ||
// TODO: rename cssObject to cssObjectAcumulator | ||
return tokens.reduce((cssObject, token) => { | ||
const nameParts = token.origin?.name?.split('/'); | ||
|
||
return Array.from(originNameMap.entries()) | ||
.map(([objectName, token]) => { | ||
const objectContent = generateObjectContent(token, tokenGroups, hasParentPrefix); | ||
if (nameParts) { | ||
let currentObject: CssObjectType = cssObject; | ||
|
||
nameParts.forEach((part, index) => { | ||
if (index === 0) { | ||
part = token.tokenType === TokenType.color ? `$${part}-colors` : `$${toPlural(part.toLowerCase())}`; | ||
} | ||
if (index === nameParts.length - 1) { | ||
const value = tokenVariableName(token, tokenGroups, hasParentPrefix); | ||
let result; | ||
const numericPart = token.name.match(/\d+/)?.[0]; | ||
const nonNumericPart = getNonNumericPart(token.name); | ||
|
||
if (token.tokenType !== TokenType.color && numericPart) { | ||
result = numericPart; | ||
} else { | ||
result = nonNumericPart; | ||
} | ||
currentObject[result] = `$${value}`; | ||
} else { | ||
currentObject[part] = currentObject[part] || {}; | ||
currentObject = currentObject[part] as CssObjectType; | ||
} | ||
}); | ||
} | ||
|
||
return objectContent.trim() && `$${objectName}: (\n${objectContent}) !default;\n\n`; | ||
}) | ||
.join(''); | ||
return cssObject; | ||
}, {}); | ||
}; |
Oops, something went wrong.