Skip to content

Commit

Permalink
feat: add whitepaper format for files
Browse files Browse the repository at this point in the history
  • Loading branch information
yarastqt committed May 29, 2020
1 parent 39a85f1 commit 4a4e43e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/core-v2/build.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import StyleDictionaryApi from 'style-dictionary'

import { createWhitepaperConfig } from './whitepaper-config'
import { variablesWithPrefix } from './variable-with-prefix'
import { getCssModifierWithPlatform } from './utils'

const store = new Map()

StyleDictionaryApi.registerFormat({
name: 'css/whitepaper',
formatter: (dictionary) => {
const group = dictionary.allProperties.length ? dictionary.allProperties[0].group : 'unknown'
const value = store.get('theme')
const selector = `.Theme_${group}_${value}`
return `${selector} {\n${variablesWithPrefix(' --', dictionary.allProperties)} \n}\n`
},
})

export function build(config: any): any {
const normalizedConfig = Array.isArray(config) ? config : [config]
Expand All @@ -9,9 +23,11 @@ export function build(config: any): any {
for (const themeFileConfig of themeConfig.files) {
let styleDictionaryConfig: any = {}
if (themeFileConfig.format === 'css/whitepaper') {
store.set('theme', getCssModifierWithPlatform(themeConfig.name))
styleDictionaryConfig = createWhitepaperConfig({
source: themeConfig.source,
theme: themeConfig.name,
outDir: themeConfig.outDir,
})
}
const StyleDictionary = StyleDictionaryApi.extend(styleDictionaryConfig)
Expand Down
20 changes: 20 additions & 0 deletions src/core-v2/variable-with-prefix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// copy-paste from https://github.com/amzn/style-dictionary/blob/master/lib/common/formats.js
export function variablesWithPrefix(prefix: any, properties: any): any {
return properties
.map((prop: any) => {
let nextProp =
prefix +
prop.name +
': ' +
(prop.attributes.category === 'asset' ? '"' + prop.value + '"' : prop.value) +
';'

if (prop.comment) {
nextProp = nextProp.concat(' /* ' + prop.comment + ' */')
}

return nextProp
})
.filter((strVal: any) => Boolean(strVal))
.join('\n')
}

0 comments on commit 4a4e43e

Please sign in to comment.