diff --git a/src/core-v2/build.ts b/src/core-v2/build.ts index 4a3f721..de38570 100644 --- a/src/core-v2/build.ts +++ b/src/core-v2/build.ts @@ -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() @@ -56,14 +57,15 @@ export async function build(config: any): Promise { 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, diff --git a/src/core-v2/load-sources.ts b/src/core-v2/load-sources.ts new file mode 100644 index 0000000..fa9847b --- /dev/null +++ b/src/core-v2/load-sources.ts @@ -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 { + 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 +} diff --git a/src/core-v2/themes.ts b/src/core-v2/themes.ts index c375f53..94a1893 100644 --- a/src/core-v2/themes.ts +++ b/src/core-v2/themes.ts @@ -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 { @@ -14,13 +17,16 @@ function resolveRootDir(filePath: string): string { } export async function loadThemes(sources: string | string[]): Promise { - 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 } diff --git a/src/core-v2/utils.ts b/src/core-v2/utils.ts index bbb12a0..6f78d80 100644 --- a/src/core-v2/utils.ts +++ b/src/core-v2/utils.ts @@ -1,3 +1,5 @@ +import { Platforms } from '../core/platforms' + export function getFolderWithPlatform(fileName: string): string { const [name, platform] = fileName.split('@') if (platform === undefined) { @@ -5,3 +7,8 @@ export function getFolderWithPlatform(fileName: string): string { } return `${name}/${platform}` } + +export function getPlatformFromFilePath(filePath: string): Platforms { + const matched = filePath.match(/@([\w|-]+)+\./) + return matched === null ? 'common' : (matched[1] as Platforms) +}