Skip to content
Prev Previous commit
Next Next commit
refactor(build/to-style-dictionary.js): maintains code consistency wi…
…th others
  • Loading branch information
indaco committed Oct 23, 2023
commit 77c6534b72e4b6d9c42424d2ab3016c93bf471fb
6 changes: 3 additions & 3 deletions build/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ const JSONtokens = fs.createWriteStream('../open-props.tokens.json')
JSONtokens.end(JSON.stringify(Object.fromEntries(designtokens), null, 2))

// gen style-dictionary tokens
const styledictionary = toStyleDictionary(designtokens)
const JSONStyleDictionaryTokens = fs.createWriteStream('../open-props.style-dictionary-tokens.json')
JSONStyleDictionaryTokens.end(JSON.stringify(styledictionary, null, 2))
const styledictionarytokens = toStyleDictionary(jsonbundle)
const StyleDictionaryTokens = fs.createWriteStream('../open-props.style-dictionary-tokens.json')
StyleDictionaryTokens.end(JSON.stringify(styledictionarytokens, null, 2))

// gen figma tokens
const figmatokens = toFigmaTokens(jsonbundle)
Expand Down
119 changes: 101 additions & 18 deletions build/to-style-dictionary.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,110 @@
export const toStyleDictionary = (designtokens) => {
const tokens = Object.fromEntries(designtokens);
import * as Colors from '../src/props.colors.js'

const transformedJson = {};
const dictionaryMap = {
"size-relative": "relative",
"size-fluid": "fluid",
"size-header": "header",
"size-content": "content",
"border-size": "size",
"radius-conditional": "conditional",
"radius-blob": "blob"
};

for (const item in tokens) {
const tokenInfo = tokens[item];
const mapToDictionaryKey = (value) => dictionaryMap[value] || value;

const keyWithoutPrefix = item.replace('--', '');
const keyParts = keyWithoutPrefix.split('-');
const mainKey =
keyParts.length > 1 ? keyParts.slice(0, -1).join('-') : keyParts[0];
const index = keyParts.length > 1 ? keyParts[keyParts.length - 1] : 0;
const getTypeKey = (metaType) => {
if (metaType === "size" || metaType === "border-radius") {
return metaType === "size" ? "size" : "radius"
} else if (metaType === "border-width") {
return "border"
}
return metaType
};

const { $value, $type, ...meta } = tokenInfo;
const value = $value;
const type = $type;
const createTokenObject = ({
baseObj,
mainKey,
metaType,
dictionarykey,
index,
token
}) => {
// Determine if the main key should be handled separately
const shouldHandleMainKey = mainKey !== dictionarykey
// Determine the type key based on metaType
const typeKey = getTypeKey(metaType)

if (!transformedJson[mainKey]) {
transformedJson[mainKey] = {};
}
// Initialize the typeKey in the baseObj if it doesn't exist
baseObj[typeKey] = baseObj[typeKey] || {}
// Define the target object
const targetObj = baseObj[typeKey]

transformedJson[mainKey][index] = { value, type, ...meta };
if(typeKey === "size" || typeKey === "radius") {
if(shouldHandleMainKey){
// Handle main key separately
targetObj[dictionarykey] = targetObj[dictionarykey] || {}
targetObj[dictionarykey][index] = {
value: token,
type: metaType
}
} else {
// Handle main key directly
targetObj[index] = {
value: token,
type: metaType
}
}
} else {
// Handle all other types
targetObj[dictionarykey] = targetObj[dictionarykey] || {}
targetObj[dictionarykey][index] = {
value: token,
type: metaType
}
}

return transformedJson;
return baseObj
};

export const toStyleDictionary = props => {
const styledictionarytokens = {}

const colors = Object.keys(Colors)
.filter(exportName => exportName !== "default")
.map(hueName => hueName.toLowerCase())

props.forEach(([key, token]) => {
const meta = {}

const isLength = key.includes('size') && !key.includes('border-size')
const isBorder = key.includes('border-size')
const isRadius = key.includes('radius')
const isShadow = key.includes('shadow')
const isColor = colors.some(color => key.includes(color))

if (isLength) meta.type = 'size'
else if (isBorder) meta.type = 'border-width'
else if (isRadius) meta.type = 'border-radius'
else if (isShadow) meta.type = 'box-shadow'
else if (isColor) meta.type = 'color'
else meta.type = 'other'

const keyWithoutPrefix = key.replace('--', '')
const keyParts = keyWithoutPrefix.split('-')
const mainKey = keyParts.length > 1 ? keyParts.slice(0, -1).join('-') : keyParts[0]
const index = keyParts.length > 1 ? keyParts[keyParts.length - 1] : 0

const dictionarykey = mapToDictionaryKey(mainKey)

createTokenObject({
baseObj: styledictionarytokens,
mainKey: mainKey,
metaType: meta.type,
dictionarykey: dictionarykey,
index: index,
token: token
})
})

return styledictionarytokens
}
⌂ 换网址