forked from barsh/true-case-path
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add optional basePath argument for selective case-correction
Closes upstream barsh#2, re gatsbyjs/gatsby#15876
- Loading branch information
Showing
7 changed files
with
135 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"arrowParens": "always", | ||
"semi": false, | ||
"singleQuote": true | ||
} |
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,14 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"name": "Test", | ||
"program": "${workspaceFolder}/test/index.js" | ||
} | ||
] | ||
} |
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,3 +1,6 @@ | ||
export function trueCasePath(filePath: string): Promise<string> | ||
export function trueCasePath( | ||
filePath: string, | ||
basePath?: string | ||
): Promise<string> | ||
|
||
export function trueCasePathSync(filePath: string): string | ||
export function trueCasePathSync(filePath: string, basePath?: string): string |
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,61 +1,84 @@ | ||
'use strict' | ||
|
||
const fs = require('fs') | ||
const os = require('os') | ||
const path = require('path') | ||
const { readdir: _readdir, readdirSync } = require('fs') | ||
const { platform } = require('os') | ||
const { isAbsolute } = require('path') | ||
const { promisify: pify } = require('util') | ||
|
||
const readdir = pify(fs.readdir) | ||
const isWindows = os.platform() === 'win32' | ||
const readdir = pify(_readdir) | ||
const isWindows = platform() === 'win32' | ||
const delimiter = isWindows ? '\\' : '/' | ||
|
||
module.exports = { | ||
trueCasePath, | ||
trueCasePathSync | ||
trueCasePath: _trueCasePath({ sync: false }), | ||
trueCasePathSync: _trueCasePath({ sync: true }) | ||
} | ||
|
||
function getFilePathSegments(filePath) { | ||
return path.resolve(process.cwd(), filePath).split(delimiter).filter((s) => s !== '') | ||
function getRelevantFilePathSegments(filePath) { | ||
return filePath.split(delimiter).filter((s) => s !== '') | ||
} | ||
|
||
function escapeString(str) { | ||
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') | ||
} | ||
|
||
function isDriveLetter(str) { | ||
return /[a-zA-Z]:/.test(str) | ||
} | ||
|
||
function matchCaseInsensitive(fileOrDirectory, directoryContents, filePath) { | ||
const caseInsensitiveRegex = new RegExp(`^${escapeString(fileOrDirectory)}$`, 'i') | ||
const caseInsensitiveRegex = new RegExp( | ||
`^${escapeString(fileOrDirectory)}$`, | ||
'i' | ||
) | ||
for (const file of directoryContents) { | ||
if (caseInsensitiveRegex.test(file)) { | ||
return file | ||
} | ||
if (caseInsensitiveRegex.test(file)) return file | ||
} | ||
throw new Error(`[true-case-path]: Called with ${filePath}, but no matching file exists`) | ||
throw new Error( | ||
`[true-case-path]: Called with ${filePath}, but no matching file exists` | ||
) | ||
} | ||
|
||
async function trueCasePath(filePath) { | ||
const segments = getFilePathSegments(filePath) | ||
let realPath = '' | ||
if (isWindows) { | ||
realPath += segments.shift().toUpperCase() // drive letter | ||
} | ||
for (const fileOrDirectory of segments) { | ||
const contents = await readdir(realPath + delimiter) | ||
const realCaseFileOrDirectory = matchCaseInsensitive(fileOrDirectory, contents, filePath) | ||
realPath += delimiter + realCaseFileOrDirectory | ||
function _trueCasePath({ sync }) { | ||
return (filePath, basePath) => { | ||
if (basePath && !isAbsolute(basePath)) { | ||
throw new Error( | ||
`[true-case-path]: basePath argument must be absolute. Received "${basePath}"` | ||
) | ||
} | ||
const segments = getRelevantFilePathSegments(filePath) | ||
if (!basePath) basePath = isAbsolute(filePath) ? '' : process.cwd() | ||
if (isDriveLetter(segments[0])) segments[0] = segments[0].toUpperCase() | ||
return sync | ||
? iterateSync(basePath, filePath, segments) | ||
: iterateAsync(basePath, filePath, segments) | ||
} | ||
return realPath | ||
} | ||
|
||
function trueCasePathSync(filePath) { | ||
const segments = getFilePathSegments(filePath) | ||
let realPath = '' | ||
if (isWindows) { | ||
realPath += segments.shift().toUpperCase() // drive letter | ||
} | ||
for (const fileOrDirectory of segments) { | ||
const contents = fs.readdirSync(realPath + delimiter) | ||
const realCaseFileOrDirectory = matchCaseInsensitive(fileOrDirectory, contents, filePath) | ||
realPath += delimiter + realCaseFileOrDirectory | ||
} | ||
return realPath | ||
} | ||
function iterateSync(basePath, filePath, segments) { | ||
return segments.reduce( | ||
(realPath, fileOrDirectory) => | ||
realPath + | ||
delimiter + | ||
matchCaseInsensitive( | ||
fileOrDirectory, | ||
readdirSync(realPath + delimiter), | ||
filePath | ||
), | ||
basePath | ||
) | ||
} | ||
|
||
async function iterateAsync(basePath, filePath, segments) { | ||
return await segments.reduce( | ||
async (realPathPromise, fileOrDirectory) => | ||
(await realPathPromise) + | ||
delimiter + | ||
matchCaseInsensitive( | ||
fileOrDirectory, | ||
await readdir((await realPathPromise) + delimiter), | ||
filePath | ||
), | ||
basePath | ||
) | ||
} |
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 @@ | ||
home |
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