Skip to content

Commit

Permalink
fix: fix resolving nested themes
Browse files Browse the repository at this point in the history
  • Loading branch information
yarastqt committed Jan 4, 2021
1 parent 7b6d1e5 commit 2cd439f
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 85 deletions.
2 changes: 1 addition & 1 deletion src/cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import chalk from 'chalk'

import { loadConfig } from '../core/config'
import { build } from '../core/build'
import { loadTheme } from '../core/load-theme'
import { loadTheme } from '../core/loadTheme'
import { debounce, flatten } from '../core/utils'

type Flags = {
Expand Down
2 changes: 1 addition & 1 deletion src/core/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Api, InternalApi } from '../index'
import { createStyleDictionaryConfig } from './style-dictionary-config'
import { variablesWithPrefix } from './variablesWithPrefix'
import { loadMappers } from './mappers'
import { loadTheme } from './load-theme'
import { loadTheme } from './loadTheme'
import { dedupeProps } from './dedupe-props'
import { loadSources } from './load-sources'
import { Config } from './config'
Expand Down
79 changes: 0 additions & 79 deletions src/core/load-theme.ts

This file was deleted.

70 changes: 70 additions & 0 deletions src/core/loadTheme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { join, resolve, dirname } from 'path'
import { readJSON, existsSync } from 'fs-extra'
import merge from 'deepmerge'

import { resolveFrom } from '../resolveFrom'
import { findPackageRoot } from '../findPackageRoot'
import { Platforms } from './platforms'

export async function loadTheme(
source: string,
root: string = process.cwd(),
): Promise<OutputTheme> {
let derivedTheme: OutputTheme = {
mappers: [],
sources: [],
whitepaper: {},
platforms: ['common'],
}
const theme: InputTheme = await readJSON(source)

// In case with node_modules in cwd need resolve all sources from package root.
root = root.match(/node_modules/) === null ? root : findPackageRoot(root)

if (theme.extends !== undefined) {
const parentThemePath = resolveFrom(root, theme.extends)
if (parentThemePath && existsSync(parentThemePath)) {
const parentThemeCwd = dirname(parentThemePath)
const parentTheme = await loadTheme(parentThemePath, parentThemeCwd)
// Platforms should be defined at project theme.
derivedTheme = merge(derivedTheme, { ...parentTheme, platforms: [] })
} else {
throw new Error(`Cannot load theme: "${theme.extends}".`)
}
}

if (theme.platforms !== undefined) {
derivedTheme.platforms = theme.platforms
}

if (theme.mappers !== undefined) {
derivedTheme.mappers.push(...theme.mappers.map((filePath) => join(root, filePath)))
}

if (theme.whitepaper !== undefined) {
derivedTheme.whitepaper = { ...derivedTheme.whitepaper, ...theme.whitepaper }
}

for (const source of theme.sources) {
// Makes array of arrays with each source for save order after glob.
derivedTheme.sources.push([resolve(root, source)])
}

return derivedTheme
}

type InputTheme = {
mappers: string[]
sources: string[]
whitepaper: Record<string, string>
platforms: Platforms[]
extends?: string
}

type OutputTheme = {
mappers: string[]
// Uses nested array with paths, cuz glob not save orders with using patterns for path.
sources: string[][]
whitepaper: Record<string, string>
platforms: Platforms[]
}
4 changes: 0 additions & 4 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ import normalize from 'normalize-path'

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

export function throwError(messag: string): void {
throw new Error(messag)
}

export function getPlatformFromFilePath(filePath: string): Platforms {
const matched = filePath.match(/@([\w|-]+)+\./)
return matched === null ? 'common' : (matched[1] as Platforms)
Expand Down
14 changes: 14 additions & 0 deletions src/findPackageRoot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { dirname, join } from 'path'
import readPkgUp from 'read-pkg-up'

export function findPackageRoot(path: string): string {
const data = readPkgUp.sync({ cwd: path })
if (data === undefined) {
throw new Error('Cannot find package root, please check exists package.json.')
}
if (data.packageJson.version !== '' && data.packageJson.name !== '') {
return dirname(data.path)
}
const prevDir = join(dirname(data.path), '..')
return findPackageRoot(prevDir)
}
30 changes: 30 additions & 0 deletions src/resolveFrom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Module from 'module'
import path from 'path'
import fs from 'fs'

export function resolveFrom(fromDirectory: string, moduleId: string): string | undefined {
try {
fromDirectory = fs.realpathSync(fromDirectory)
} catch (error) {
if (error.code === 'ENOENT') {
fromDirectory = path.resolve(fromDirectory)
} else {
return
}
}

const fromFile = path.join(fromDirectory, 'noop.js')

const resolveFileName = () =>
(Module as any)._resolveFilename(moduleId, {
id: fromFile,
filename: fromFile,
paths: (Module as any)._nodeModulePaths(fromDirectory),
})

try {
return resolveFileName()
} catch (error) {
return
}
}

0 comments on commit 2cd439f

Please sign in to comment.