Skip to content

Commit

Permalink
fix: use normalize for glob paths
Browse files Browse the repository at this point in the history
  • Loading branch information
yarastqt committed Jul 28, 2020
1 parent 305488f commit 5db41d4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
7 changes: 5 additions & 2 deletions src/core/load-sources.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import glob from 'fast-glob'

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

export async function loadSources(paths: string[][], platform: Platforms): Promise<string[]> {
const levels = platforms.get(platform)
Expand All @@ -12,7 +12,10 @@ export async function loadSources(paths: string[][], platform: Platforms): Promi

// Uses nested array with paths, cuz glob not save orders with using patterns for path.
// Also uses sort after glob for idempotent result.
const result = flatten(await Promise.all(paths.map((path) => glob.sync(path).sort())))
const resolvedPaths = await Promise.all(
paths.map((path) => glob.sync(normalizePaths(path)).sort()),
)
const result = flatten(resolvedPaths)
.filter((file) => {
const filePlatform = getPlatformFromFilePath(file)
return levels.includes(filePlatform)
Expand Down
9 changes: 4 additions & 5 deletions src/core/load-theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import glob from 'fast-glob'
import pkgDir from 'pkg-dir'

import { Platforms } from './platforms'
import { throwError } from './utils'
import { throwError, normalizePaths } from './utils'

type InputTheme = {
mappers: string[]
Expand All @@ -31,10 +31,9 @@ export async function loadTheme(
const theme: InputTheme = await readJSON(sources)

if (theme.extends !== undefined) {
const [extendsPath] = await glob([
resolve(cwd, 'node_modules', theme.extends),
resolve(cwd, theme.extends),
])
const [extendsPath] = await glob(
normalizePaths([resolve(cwd, 'node_modules', theme.extends), resolve(cwd, theme.extends)]),
)

if (extendsPath === undefined) {
throwError(`Cannot load theme: "${theme.extends}".`)
Expand Down
6 changes: 4 additions & 2 deletions src/core/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import glob from 'fast-glob'
import { readJSON, readFileSync } from 'fs-extra'
import YAML from 'yaml'

export async function loadMappers(path: string[]): Promise<any> {
import { normalizePaths } from './utils'

export async function loadMappers(paths: string[]): Promise<any> {
const result = {}
for (const file of await glob(path)) {
for (const file of await glob(normalizePaths(paths))) {
if (/\.ya?ml$/.test(file)) {
Object.assign(result, YAML.parse(await readFileSync(file, 'utf8')))
} else {
Expand Down
6 changes: 6 additions & 0 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import normalize from 'normalize-path'

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

export function throwError(messag: string): void {
Expand Down Expand Up @@ -38,3 +40,7 @@ type ArrayType<T> = T extends (infer U)[] ? U : never
export function flatten<T extends any[]>(arrays: T[]): ArrayType<T>[] {
return arrays.reduce<any[]>((acc, value) => acc.concat(value), [])
}

export function normalizePaths(paths: string[]): string[] {
return paths.map((path) => normalize(path))
}

0 comments on commit 5db41d4

Please sign in to comment.