-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
116 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |