-
Notifications
You must be signed in to change notification settings - Fork 18
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
8 changed files
with
90 additions
and
9 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 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,7 @@ | ||
// Util to normalize windows paths to posix | ||
export function normalizeWindowsPath (input: string = '') { | ||
if (!input || !input.includes('\\')) { | ||
return input | ||
} | ||
return input.replace(/\\/g, '/') | ||
} |
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 |
---|---|---|
@@ -1,7 +1,30 @@ | ||
// Util to normalize windows paths to posix | ||
export function normalizeWindowsPath (input: string = '') { | ||
if (!input || !input.includes('\\')) { | ||
return input | ||
const pathSeparators = ['/', '\\', undefined] | ||
|
||
export function normalizeAliases (_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]) => _compareAliases(a, b))) | ||
// Resolve alias values in relation to each other | ||
for (const key in aliases) { | ||
for (const alias in aliases) { | ||
// don't resolve a more specific alias with regard to a less specific one | ||
if (alias === key || key.startsWith(alias)) { continue } | ||
|
||
if (aliases[key].startsWith(alias) && pathSeparators.includes(aliases[key][alias.length])) { | ||
aliases[key] = aliases[alias] + aliases[key].slice(alias.length) | ||
} | ||
} | ||
} | ||
return input.replace(/\\/g, '/') | ||
return aliases | ||
} | ||
|
||
const FILENAME_RE = /(?<=^|[\\/])([^\\/]+?)(?=(\.[^.]+)?$)/ | ||
|
||
export function filename (path: string) { | ||
return path.match(FILENAME_RE)?.[0] | ||
} | ||
|
||
// --- internals --- | ||
|
||
function _compareAliases (a: string, b: string) { | ||
return b.split('/').length - a.split('/').length | ||
} |
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,43 @@ | ||
import { describe, expect, it } from 'vitest' | ||
|
||
import { normalizeAliases, filename } from '../src/utils' | ||
|
||
describe('normalizeAliases', () => { | ||
it('should work', () => { | ||
expect(normalizeAliases({ | ||
'@foo/bar': '@foo/bar/dist/index.mjs', | ||
'@foo/bar/utils': '@foo/bar/dist/utils.mjs', | ||
'@': '/root', | ||
bingpot: '@/bingpot/index.ts', | ||
unchanged: '@bingpot/index.ts' | ||
})).toMatchInlineSnapshot(` | ||
{ | ||
"@": "/root", | ||
"@foo/bar": "@foo/bar/dist/index.mjs", | ||
"@foo/bar/utils": "@foo/bar/dist/utils.mjs", | ||
"bingpot": "/root/bingpot/index.ts", | ||
"unchanged": "@bingpot/index.ts", | ||
} | ||
`) | ||
}) | ||
}) | ||
|
||
describe('filename', () => { | ||
const files = { | ||
// POSIX | ||
'test.html': 'test', | ||
'/temp/myfile.html': 'myfile', | ||
'./myfile.html': 'myfile', | ||
|
||
// Windows | ||
'C:\\temp\\': undefined, | ||
'C:\\temp\\myfile.html': 'myfile', | ||
'\\temp\\myfile.html': 'myfile', | ||
'.\\myfile.html': 'myfile' | ||
} | ||
for (const file in files) { | ||
it(file, () => { | ||
expect(filename(file)).toEqual(files[file]) | ||
}) | ||
} | ||
}) |
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