Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add normalizeAliases and filename in /utils subpath #34

Merged
merged 15 commits into from Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Expand Up @@ -21,5 +21,6 @@ jobs:
- run: pnpm install
- run: pnpm lint
- run: pnpm build
- run: pnpm test:types
- run: pnpm vitest --coverage
- uses: codecov/codecov-action@v3
4 changes: 4 additions & 0 deletions build.config.ts
@@ -0,0 +1,4 @@
import { defineBuildConfig } from 'unbuild'
export default defineBuildConfig({
externals: ['pathe']
})
7 changes: 6 additions & 1 deletion package.json
Expand Up @@ -9,6 +9,10 @@
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
},
"./utils": {
"import": "./dist/utils.mjs",
"require": "./dist/utils.cjs"
}
},
"main": "./dist/index.cjs",
Expand All @@ -22,7 +26,8 @@
"lint": "eslint --ext .ts .",
"prepublishOnly": "pnpm build",
"release": "pnpm vitest run && standard-version && git push --follow-tags && pnpm publish",
"test": "pnpm lint && vitest run --coverage"
"test": "pnpm lint && vitest run --coverage",
"test:types": "tsc --noEmit"
},
"devDependencies": {
"@nuxtjs/eslint-config-typescript": "latest",
Expand Down
7 changes: 7 additions & 0 deletions src/_internal.ts
@@ -0,0 +1,7 @@
// Util to normalize windows paths to posix
export function normalizeWindowsPath (input: string = '') {
if (!input.includes('\\')) {
return input
}
return input.replace(/\\/g, '/')
}
2 changes: 1 addition & 1 deletion src/path.ts
Expand Up @@ -8,7 +8,7 @@ Check LICENSE file

import type path from 'path'

import { normalizeWindowsPath } from './utils'
import { normalizeWindowsPath } from './_internal'

const _UNC_REGEX = /^[/][/]/
const _UNC_DRIVE_REGEX = /^[/][/]([.]{1,2}[/])?([a-zA-Z]):[/]/
Expand Down
33 changes: 28 additions & 5 deletions src/utils.ts
@@ -1,7 +1,30 @@
// Util to normalize windows paths to posix
export function normalizeWindowsPath (input: string = '') {
if (!input.includes('\\')) {
return input
import { basename } from 'pathe'
pi0 marked this conversation as resolved.
Show resolved Hide resolved

export function resolveAliases (_aliases: Record<string, string>) {
// Sort aliases from specific to general (ie. fs/promises before fs)
const aliases = Object.fromEntries(Object.entries(_aliases).sort(([a], [b]) =>
(b.split('/').length - a.split('/').length) || (b.length - a.length)
danielroe marked this conversation as resolved.
Show resolved Hide resolved
))
// Resolve alias values in relation to each other
for (const key in aliases) {
for (const alias in aliases) {
if (!['~', '@', '#'].includes(alias[0])) { continue }
danielroe marked this conversation as resolved.
Show resolved Hide resolved
if (alias === '@' && !aliases[key].startsWith('@/')) { continue } // Don't resolve @foo/bar

if (aliases[key].startsWith(alias)) {
aliases[key] = aliases[alias] + aliases[key].slice(alias.length)
}
}
}
return input.replace(/\\/g, '/')
return aliases
}

export function sortPaths (paths: string[]) {
return paths.sort((a, b) =>
(b.split('/').length - a.split('/').length) || (b.length - a.length)
)
}

export function filename (path: string) {
return basename(path).replace(/\.[^.]+$/, '')
danielroe marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 1 addition & 1 deletion test/index.spec.ts
Expand Up @@ -2,7 +2,7 @@ import { describe, expect, it, vi } from 'vitest'

import { basename, dirname, extname, format, parse, relative, delimiter, isAbsolute, join, normalize, resolve, sep, toNamespacedPath } from '../src'

import { normalizeWindowsPath } from '../src/utils'
import { normalizeWindowsPath } from '../src/_internal'

runTest('normalizeWindowsPath', normalizeWindowsPath, {
// POSIX
Expand Down
14 changes: 12 additions & 2 deletions tsconfig.json
Expand Up @@ -3,9 +3,19 @@
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Node",
"esModuleInterop": true
"esModuleInterop": true,
"skipLibCheck": true,
"paths": {
"pathe/utils": [
"./src/utils"
],
"pathe": [
"./src/index"
]
},
},
"include": [
"src"
"src",
"test"
]
}