Skip to content

Commit

Permalink
feat: throw error if module return invalid format
Browse files Browse the repository at this point in the history
  • Loading branch information
yarastqt committed May 9, 2020
1 parent 50b88e7 commit bb066b3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/core/import-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export function esModuleInterop<T>(box?: T): T {
* @return Promise with data.
*/
export async function importModule<T = any>(path: string): Promise<T> {
// TODO: Source possibly empty or have invalid format.
// TODO: Add diagnostic.
const result = await import(path)
return esModuleInterop(result)
Expand Down
5 changes: 4 additions & 1 deletion src/core/theme-layers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import deepmerge from 'deepmerge'
import { Platforms, platforms } from './platforms'
import { importModule } from './import-module'
import { Shape, TokensMap, ThemeTokens, Meta } from './types'
import { deepInvoke } from './utils'
import { deepInvoke, throwError } from './utils'

type ThemeLayers = Shape<
Shape<
Expand All @@ -24,6 +24,9 @@ export async function getThemeLayers(
const files = await fg(source)
for (const fileName of files) {
const source = await importModule(resolve(fileName))
if (typeof source !== 'function') {
throwError('Theme should return "withTokens" call.')
}
const themeLayer = deepInvoke<ThemeTokens>(source)
const { name } = parse(fileName)
for (const [platform, levels] of platforms) {
Expand Down
4 changes: 4 additions & 0 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ export function withPrefix(value: string, prefix?: string): string {
}
return `${prefix}_${value}`
}

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

0 comments on commit bb066b3

Please sign in to comment.