-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: stdin urls on the command line (#6345)
- Loading branch information
Showing
6 changed files
with
86 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export const UTF8 = 'utf8' as const; | ||
export const STDIN = 'stdin' as const; | ||
export const STDINProtocol = 'stdin://' as const; | ||
export const FileProtocol = 'file://' as const; | ||
export const STDINProtocol = 'stdin:' as const; | ||
export const STDINUrlPrefix = 'stdin://' as const; | ||
export const FileUrlPrefix = 'file://' as const; |
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,31 @@ | ||
import Path from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
|
||
import { describe, expect, test } from 'vitest'; | ||
|
||
import { isStdinUrl, resolveStdinUrl } from './stdinUrl.js'; | ||
|
||
const filenameURL = new URL(import.meta.url); | ||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = Path.dirname(__filename); | ||
const dirUrl = new URL('./', import.meta.url); | ||
const stdinUrl = dirUrl.href.replace(/^file:/, 'stdin:'); | ||
|
||
describe('stdinUrl', () => { | ||
test('isStdinUrl', () => { | ||
expect(isStdinUrl('stdin://')).toBe(true); | ||
expect(isStdinUrl('stdin:')).toBe(true); | ||
expect(isStdinUrl('stdin://foo')).toBe(true); | ||
}); | ||
|
||
test.each` | ||
url | expected | ||
${'stdin://'} | ${stdinUrl} | ||
${'stdin:package.json'} | ${new URL('package.json', stdinUrl).href} | ||
${'stdin:./package.json'} | ${new URL('package.json', stdinUrl).href} | ||
${'stdin:' + __filename} | ${new URL(filenameURL.pathname, stdinUrl).href} | ||
${'stdin://' + __filename} | ${new URL(filenameURL.pathname, stdinUrl).href} | ||
`('resolveStdinUrl', ({ url, expected }) => { | ||
expect(resolveStdinUrl(url, __dirname)).toBe(expected); | ||
}); | ||
}); |
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,28 @@ | ||
import assert from 'node:assert'; | ||
import Path from 'node:path'; | ||
import { pathToFileURL } from 'node:url'; | ||
|
||
import { STDINProtocol } from './constants.js'; | ||
|
||
export function isStdinUrl(url: string | URL): boolean { | ||
if (url instanceof URL) { | ||
return url.protocol === STDINProtocol; | ||
} | ||
return url.startsWith(STDINProtocol); | ||
} | ||
|
||
/** | ||
* Normalize and resolve a stdin url. | ||
* @param url - stdin url to resolve. | ||
* @param cwd - file path to resolve relative paths against. | ||
* @returns | ||
*/ | ||
export function resolveStdinUrl(url: string, cwd: string): string { | ||
assert(url.startsWith(STDINProtocol), `Expected url to start with ${STDINProtocol}`); | ||
const path = url | ||
.slice(STDINProtocol.length) | ||
.replace(/^\/\//, '') | ||
.replace(/^\/([a-z]:)/i, '$1'); | ||
const fileUrl = pathToFileURL(Path.resolve(cwd, path)); | ||
return fileUrl.toString().replace(/^file:/, STDINProtocol) + (path ? '' : '/'); | ||
} |