Skip to content

Commit

Permalink
feat: use new format for config
Browse files Browse the repository at this point in the history
  • Loading branch information
yarastqt committed Jun 11, 2020
1 parent 3c258f8 commit 6e6e6da
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 51 deletions.
43 changes: 20 additions & 23 deletions src/core/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,26 @@ StyleDictionaryApi.registerAction({
})

export async function build(config: Config): Promise<void> {
const normalizedConfig: Config[] = Array.isArray(config) ? config : [config]
for (const themeConfig of normalizedConfig) {
for (const entryKey in themeConfig.entry) {
const theme = await loadTheme(themeConfig.entry[entryKey])
for (const platform of theme.platforms) {
// TODO: Load sources in themes?
const sources = await loadSources(theme.sources, platform)
// TODO: Load mappers in themes?
store.set('mapper', await loadMappers(theme.mappers))
for (const _themeFileConfig of themeConfig.output.files) {
store.set('whitepaper', theme.whitepaper)
const styleDictionaryConfig = createStyleDictionaryConfig({
platform: platform,
source: sources,
theme: entryKey,
outDir: themeConfig.output.path,
whitepaper: theme.whitepaper,
})
const StyleDictionary = StyleDictionaryApi.extend(styleDictionaryConfig)
StyleDictionary.properties = dedupeProps(StyleDictionary.properties)
StyleDictionary.buildPlatform('css')
}
}
for (const entry in config.entry) {
const theme = await loadTheme(config.entry[entry])
for (const platform of theme.platforms) {
// TODO: Load sources in themes?
const sources = await loadSources(theme.sources, platform)

// TODO: Load mappers in themes?
store.set('mapper', await loadMappers(theme.mappers))
store.set('whitepaper', theme.whitepaper)

const styleDictionaryConfig = createStyleDictionaryConfig({
platform: platform,
sources: sources,
entry: entry,
output: config.output,
})
const StyleDictionary = StyleDictionaryApi.extend(styleDictionaryConfig)

StyleDictionary.properties = dedupeProps(StyleDictionary.properties)
StyleDictionary.buildAllPlatforms()
}
}
}
10 changes: 3 additions & 7 deletions src/core/config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { Platform } from 'style-dictionary'

export type Config = {
entry: Record<string, string>
output: {
path: string
files: Array<{
filename: string
format: string
}>
}
output: Record<string, Platform>
}

export async function loadConfig(path: string): Promise<Config> {
Expand Down
52 changes: 31 additions & 21 deletions src/core/style-dictionary-config.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
export function createStyleDictionaryConfig({
source,
theme,
outDir,
platform,
whitepaper,
}: any): any {
const themeFolder = platform === 'common' ? theme : `${theme}/${platform}`
import { Platform, Config } from 'style-dictionary'

type Options = {
sources: string[]
entry: string
platform: string
output: Record<string, Platform>
}

export function createStyleDictionaryConfig({ sources, entry, platform, output }: Options): Config {
const platforms = Object.entries<Platform>(output)
// prettier-ignore
.reduce<Record<string, Platform>>((acc, [key, value]) => {
const target = { ...value }
// Normalize path for style-dictionary specifics.
target.buildPath = target.buildPath.endsWith('/') ? target.buildPath : `${target.buildPath}/`
target.files = target.files.map((file) => ({
...file,
// Replace placeholders for multiple themes and platforms.
destination: file.destination
.replace(/\[entry\]/, entry)
.replace(/\[platform\]/, platform)
// Remove common level, because is root.
.replace(/common\/?/, ''),
}))
acc[key] = target
return acc
}, {})

return {
include: source,
platforms: {
css: {
buildPath: outDir.endsWith('/') ? outDir : `${outDir}/`,
transforms: ['attribute/cti', 'time/seconds', 'color/css', 'name/cti/kebab', 'name/mapper'],
actions: ['process-color'],
files: Object.keys(whitepaper).map((file: any) => ({
destination: `${themeFolder}/${file}.css`,
format: 'css/whitepaper',
filter: (token: any) => token.group === file,
})),
},
},
include: sources,
platforms: platforms,
}
}

0 comments on commit 6e6e6da

Please sign in to comment.