Skip to content

Commit

Permalink
feat: impl presets registration
Browse files Browse the repository at this point in the history
  • Loading branch information
yarastqt committed Nov 28, 2020
1 parent 798112a commit 3c70d79
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/core/style-dictionary-config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Platform, Config } from 'style-dictionary'

import { Api } from '../index'

type Options = {
sources: string[]
entry: string
Expand All @@ -12,6 +14,17 @@ export function createStyleDictionaryConfig({ sources, entry, platform, output }
// prettier-ignore
.reduce<Record<string, Platform>>((acc, [key, value]) => {
const target = { ...value }

if (target.preset !== undefined) {
const maybePrese = Api.presets.get(target.preset)
if (maybePrese === undefined) {
throw new Error(`Used unexpected preset "${target.preset}" for "${key}" platform.`)
} else {
target.transforms = maybePrese.transforms || target.transforms
target.actions = maybePrese.actions || target.actions
}
}

// Normalize path for style-dictionary specifics.
target.buildPath = target.buildPath.endsWith('/') ? target.buildPath : `${target.buildPath}/`
target.files = target.files.map((file) => ({
Expand Down
14 changes: 14 additions & 0 deletions src/index.h.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export type Preset = {
/**
* Preset name
*/
name: string
/**
* Predefined transforms
*/
transforms?: string[]
/**
* Predefined actions
*/
actions?: string[]
}
18 changes: 18 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
import StyleDictionary from 'style-dictionary'

import { Preset } from './index.h'

export const Api = {
registerFormat: StyleDictionary.registerFormat.bind(StyleDictionary),
registerTransform: StyleDictionary.registerTransform.bind(StyleDictionary),
registerAction: StyleDictionary.registerAction.bind(StyleDictionary),
registerFilter: StyleDictionary.registerFilter.bind(StyleDictionary),

/**
* Map with registered presets
*
* @internal
*/
presets: new Map<string, Preset>(),

/**
* Add new or override existing preset
*
* @param preset - Preset settings
*/
registerPreset: (preset: Preset): void => {
Api.presets.set(preset.name, preset)
},
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/modules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ declare module 'style-dictionary' {
}

export type Platform = {
preset?: string
buildPath: string
transforms?: string[]
actions?: string[]
Expand Down

0 comments on commit 3c70d79

Please sign in to comment.