Skip to content

Commit

Permalink
feat: sort sources by levels
Browse files Browse the repository at this point in the history
  • Loading branch information
yarastqt committed Jun 7, 2020
1 parent f05f3f1 commit 10cd78b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/core-v2/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { variablesWithPrefix } from './variable-with-prefix'
import { loadMappers } from './mappers'
import { loadThemes } from './themes'
import { dedupeProps } from './dedupe-props'
import { loadSources } from './load-sources'

const store = new Map()

Expand Down Expand Up @@ -56,14 +57,15 @@ export async function build(config: any): Promise<any> {
const normalizedConfig = Array.isArray(config) ? config : [config]
for (const themeConfig of normalizedConfig) {
for (const entryKey in themeConfig.entry) {
const { mappers, sources, whitepaper } = await loadThemes(themeConfig.entry[entryKey])
// prettier-ignore
const { mappers, sources: src, whitepaper, platform } =
await loadThemes(themeConfig.entry[entryKey])
const sources = await loadSources(src, platform)
// TODO: Load mappers in themes?
store.set('mapper', await loadMappers(mappers))
for (const _themeFileConfig of themeConfig.output.files) {
let styleDictionaryConfig: any = {}
store.set('whitepaper', whitepaper)
styleDictionaryConfig = createStyleDictionaryConfig({
// TODO: Add sort by platform for all sources.
const styleDictionaryConfig = createStyleDictionaryConfig({
source: sources,
theme: entryKey,
outDir: themeConfig.output.path,
Expand Down
27 changes: 27 additions & 0 deletions src/core-v2/load-sources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import glob from 'fast-glob'

import { Platforms, platforms } from '../core/platforms'
import { getPlatformFromFilePath } from './utils'

export async function loadSources(path: string[], platform: Platforms): Promise<string[]> {
const levels = platforms.get(platform)

if (levels === undefined) {
throw new Error(`Unexpected platform: ${platform}, please check configuration.`)
}

const files = await glob(path)

const result = files
.filter((file) => {
const filePlatform = getPlatformFromFilePath(file)
return levels.includes(filePlatform as Platforms)
})
.sort((a, b) => {
const a1 = getPlatformFromFilePath(a)
const b1 = getPlatformFromFilePath(b)
return levels.indexOf(a1) - levels.indexOf(b1)
})

return result
}
12 changes: 9 additions & 3 deletions src/core-v2/themes.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { join } from 'path'
import { readJSON } from 'fs-extra'

import { Platforms } from '../core/platforms'

type Themes = {
mappers: string[]
sources: string[]
whitepaper: {}
platform: Platforms
}

function resolveRootDir(filePath: string): string {
Expand All @@ -14,13 +17,16 @@ function resolveRootDir(filePath: string): string {
}

export async function loadThemes(sources: string | string[]): Promise<Themes> {
const result: Themes = { mappers: [], sources: [], whitepaper: {} }
const result: Themes = { mappers: [], sources: [], whitepaper: {}, platform: 'common' }
const normalizedSources = Array.isArray(sources) ? sources : [sources]
for (const sourcePath of normalizedSources) {
const { mappers = [], sources = [], whitepaper }: Themes = await readJSON(sourcePath)
const { mappers = [], sources = [], whitepaper, platform }: Themes = await readJSON(sourcePath)
result.mappers.push(...mappers.map(resolveRootDir))
result.sources.push(...sources.map(resolveRootDir))
result.whitepaper = whitepaper
result.whitepaper = { ...result.whitepaper, whitepaper }
if (platform !== undefined) {
result.platform = platform
}
}
return result
}
7 changes: 7 additions & 0 deletions src/core-v2/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { Platforms } from '../core/platforms'

export function getFolderWithPlatform(fileName: string): string {
const [name, platform] = fileName.split('@')
if (platform === undefined) {
return name
}
return `${name}/${platform}`
}

export function getPlatformFromFilePath(filePath: string): Platforms {
const matched = filePath.match(/@([\w|-]+)+\./)
return matched === null ? 'common' : (matched[1] as Platforms)
}

0 comments on commit 10cd78b

Please sign in to comment.