From 4a4e43e306f2ec38a4f5937074109974fbf61bd2 Mon Sep 17 00:00:00 2001 From: Eugene Date: Fri, 29 May 2020 16:44:54 +0300 Subject: [PATCH] feat: add whitepaper format for files --- src/core-v2/build.ts | 16 ++++++++++++++++ src/core-v2/variable-with-prefix.ts | 20 ++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/core-v2/variable-with-prefix.ts diff --git a/src/core-v2/build.ts b/src/core-v2/build.ts index ddda073..45bbc7a 100644 --- a/src/core-v2/build.ts +++ b/src/core-v2/build.ts @@ -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] @@ -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) diff --git a/src/core-v2/variable-with-prefix.ts b/src/core-v2/variable-with-prefix.ts new file mode 100644 index 0000000..0db8d18 --- /dev/null +++ b/src/core-v2/variable-with-prefix.ts @@ -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') +}