Skip to content

Commit

Permalink
Merge pull request #60 from yarastqt/yarastqt.win-normalize
Browse files Browse the repository at this point in the history
Add paths normalize for win platform
  • Loading branch information
yarastqt authored Jul 28, 2020
2 parents 32de726 + 5db41d4 commit b820364
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"deepmerge": "4.2.2",
"fast-glob": "3.2.2",
"fs-extra": "8.1.0",
"normalize-path": "3.0.0",
"pkg-dir": "4.2.0",
"style-dictionary": "2.10.0",
"yaml": "1.10.0"
Expand Down
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))
}
5 changes: 5 additions & 0 deletions src/modules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,8 @@ declare module 'style-dictionary' {
}
export default StyleDictionaryApi
}

declare module 'normalize-path' {
function normalize(path: string, trailingSlashes?: boolean): string
export default normalize
}

0 comments on commit b820364

Please sign in to comment.