Skip to content

Commit

Permalink
feat: add mapper for token names
Browse files Browse the repository at this point in the history
  • Loading branch information
yarastqt committed May 29, 2020
1 parent 67ac472 commit 08db800
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/core-v2/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import StyleDictionaryApi from 'style-dictionary'
import { createWhitepaperConfig } from './whitepaper-config'
import { variablesWithPrefix } from './variable-with-prefix'
import { getCssModifierWithPlatform } from './utils'
import { loadMappers } from './mappers'

const store = new Map()

Expand All @@ -16,15 +17,26 @@ StyleDictionaryApi.registerFormat({
},
})

export function build(config: any): any {
StyleDictionaryApi.registerTransform({
name: 'name/mapper',
type: 'name',
transformer: (prop) => {
const mapper = store.get('mapper') || {}
return mapper[prop.name] || prop.name
},
})

export async function build(config: any): Promise<any> {
const normalizedConfig = Array.isArray(config) ? config : [config]

for (const themeConfig of normalizedConfig) {
store.set('mapper', await loadMappers(themeConfig.mappers))
for (const themeFileConfig of themeConfig.files) {
let styleDictionaryConfig: any = {}
if (themeFileConfig.format === 'css/whitepaper') {
store.set('theme', getCssModifierWithPlatform(themeConfig.name))
styleDictionaryConfig = createWhitepaperConfig({
// TODO: Add sort by platform for all sources.
source: themeConfig.source,
theme: themeConfig.name,
outDir: themeConfig.outDir,
Expand Down
10 changes: 10 additions & 0 deletions src/core-v2/mappers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import glob from 'fast-glob'
import { readJSON } from 'fs-extra'

export async function loadMappers(path: string): Promise<any> {
const result = {}
for (const file of await glob(path)) {
Object.assign(result, await readJSON(file))
}
return result
}
9 changes: 8 additions & 1 deletion src/core-v2/whitepaper-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ export function createWhitepaperConfig({ source, theme, outDir }: any): any {
platforms: {
css: {
buildPath: outDir.endsWith('/') ? outDir : `${outDir}/`,
transformGroup: 'css',
transforms: [
'attribute/cti',
'name/cti/kebab',
'time/seconds',
'size/rem',
'color/css',
'name/mapper',
],
files: Object.keys(whitepaper).map((file: any) => ({
destination: `${getFolderWithPlatform(theme)}/${file}.css`,
format: 'css/whitepaper',
Expand Down

0 comments on commit 08db800

Please sign in to comment.