Skip to content

Commit

Permalink
feat: impl project theme layers
Browse files Browse the repository at this point in the history
  • Loading branch information
yarastqt committed Apr 21, 2020
1 parent f1ddd62 commit f90c3cf
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/core/theme-layers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { resolve, parse } from 'path'
import fg from 'fast-glob'
import deepmerge from 'deepmerge'

import { Platforms, platforms } from './platforms'
import { importModule } from './import-module'
import { Shape, TokensMap, ThemeTokens } from './token.h'

type ThemeLayers = Shape<
Shape<{
name: string
meta?: { css?: string }
tokens: TokensMap
}>
>

export async function getThemeLayers(
source: string,
options?: { platforms: Platforms; exclude: string[] },
): Promise<ThemeLayers> {
const result: ThemeLayers = {}
const files = await fg('**/*.{js,ts}', { cwd: source, ignore: options.exclude })
for (const fileName of files) {
const data = await importModule<ThemeTokens>(resolve(source, fileName))
const { name: layer } = parse(fileName)
for (const [platform, levels] of platforms) {
if (options !== undefined && !options.platforms.includes(platform)) {
continue
}
const composedLevels = []
for (const level of levels) {
if (data[level] !== undefined) {
composedLevels.push(data[level])
}
}
if (result[platform] === undefined) {
result[platform] = {}
}
result[platform][layer] = {
meta: data.meta,
name: layer,
tokens: deepmerge.all<TokensMap>(composedLevels),
}
}
}
return result
}
4 changes: 4 additions & 0 deletions src/core/token.h.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Platforms } from './platforms'

export type Shape<T> = { [key: string]: T }

export type Token = {
Expand All @@ -13,3 +15,5 @@ export type FlattenToken = Token & {
export type TokensMap = {
[key: string]: TokensMap | Token | string
}

export type ThemeTokens = { [key in Platforms]?: TokensMap } & { meta?: { css?: string } }

0 comments on commit f90c3cf

Please sign in to comment.