Skip to content

Commit 2806208

Browse files
committed
chore: optionaly unixify windows paths
1 parent 1073c65 commit 2806208

File tree

6 files changed

+24
-21
lines changed

6 files changed

+24
-21
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@ console.log(require('fs').readdirSync('/')); // [ 'dir' ]
5757
```
5858

5959

60-
# `patchRequire(vol[, Module])`
60+
# `patchRequire(vol[, unixifyPaths[, Module]])`
6161

6262
Patches Node's `module` module to use a given *fs-like* object `vol` for module loading.
6363

6464
- `vol` - fs-like object
65+
- `unixifyPaths` *(optional)* - whether to convert Windows paths to unix style paths, defaults to `false`.
6566
- `Module` *(optional)* - a module to patch, defaults to `require('module')`
6667

6768
Monkey-patches the `require` function in Node, this way you can make

lib/correctPath.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function removeTrailingSeparator(str) {
1616
i--;
1717
}
1818
return str.substr(0, i + 1);
19-
};
19+
}
2020

2121
function isSeparator(str, i) {
2222
var char = str[i];
@@ -32,7 +32,7 @@ function normalizePath(str, stripTrailing) {
3232
str = removeTrailingSeparator(str);
3333
}
3434
return str;
35-
};
35+
}
3636

3737
function unixify(filepath) {
3838
var stripTrailing = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
@@ -42,7 +42,7 @@ function unixify(filepath) {
4242
return filepath.replace(/^([a-zA-Z]+:|\.\/)/, '');
4343
}
4444
return filepath;
45-
};
45+
}
4646

4747
function correctPath(filepath) {
4848
return unixify(filepath.replace(/^\\\\\?\\.:\\/, '\\'));

lib/patchRequire.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ function stripBOM(content) {
2323
}
2424

2525
function patchRequire(vol) {
26-
var Module = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : require('module');
26+
var unixifyPaths = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
27+
var Module = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : require('module');
2728

28-
if (process.platform === 'win32') {
29+
if (process.platform === 'win32' && unixifyPaths) {
2930
var original = vol;
3031
vol = {
3132
readFileSync: function readFileSync(path, options) {
@@ -40,7 +41,7 @@ function patchRequire(vol) {
4041
return original.statSync(correctPath(path));
4142
}
4243
};
43-
};
44+
}
4445

4546
function internalModuleReadFile(path) {
4647
try {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fs-monkey",
3-
"version": "0.1.5",
3+
"version": "0.2.1",
44
"description": "Monkey patches for file system related things.",
55
"main": "lib/index.js",
66
"keywords": [

src/correctPath.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const isWin = process.platform === 'win32';
2+
23
/*!
34
* removeTrailingSeparator <https://github.com/darsain/remove-trailing-separator>
45
*
@@ -7,18 +8,18 @@ const isWin = process.platform === 'win32';
78
* Released under the ISC License.
89
*/
910
function removeTrailingSeparator(str) {
10-
var i = str.length - 1;
11+
let i = str.length - 1;
1112
if (i < 2) {
1213
return str;
1314
}
1415
while (isSeparator(str, i)) {
1516
i--;
1617
}
1718
return str.substr(0, i + 1);
18-
};
19+
}
1920

2021
function isSeparator(str, i) {
21-
var char = str[i];
22+
let char = str[i];
2223
return i > 0 && (char === '/' || (isWin && char === '\\'));
2324
}
2425

@@ -38,7 +39,7 @@ function normalizePath(str, stripTrailing) {
3839
str = removeTrailingSeparator(str);
3940
}
4041
return str;
41-
};
42+
}
4243

4344
/*!
4445
* unixify <https://github.com/jonschlinkert/unixify>
@@ -53,11 +54,11 @@ export function unixify(filepath, stripTrailing = true) {
5354
return filepath.replace(/^([a-zA-Z]+:|\.\/)/, '');
5455
}
5556
return filepath;
56-
};
57+
}
5758

5859
/*
5960
* Corrects a windows path to unix format (including \\?\c:...)
6061
*/
6162
export function correctPath(filepath) {
62-
return unixify( filepath.replace(/^\\\\\?\\.:\\/,'\\'));
63+
return unixify(filepath.replace(/^\\\\\?\\.:\\/,'\\'));
6364
}

src/patchRequire.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as path from 'path';
22

3-
const correctPath = process.platform === 'win32' ? require('./correctPath').correctPath : (p)=>p;
3+
const correctPath = process.platform === 'win32' ? require('./correctPath').correctPath : p => p;
44

55
/**
66
* Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
@@ -32,25 +32,25 @@ function stripBOM(content) {
3232
* @param {Object} vol
3333
* @param {Object} Module Module loader to patch.
3434
*/
35-
export default function patchRequire(vol, Module = require('module')) {
35+
export default function patchRequire(vol, unixifyPaths = false, Module = require('module')) {
3636

3737
// ensure all paths are corrected before use.
38-
if( process.platform === 'win32') {
38+
if(process.platform === 'win32' && unixifyPaths) {
3939
const original = vol;
4040
vol = {
4141
readFileSync: (path,options) => {
42-
return original.readFileSync(correctPath( path ),options);
42+
return original.readFileSync(correctPath(path),options);
4343
},
4444

4545
realpathSync: (path) => {
46-
return original.realpathSync(correctPath( path ));
46+
return original.realpathSync(correctPath(path));
4747
},
4848

4949
statSync: (path) => {
50-
return original.statSync(correctPath( path ));
50+
return original.statSync(correctPath(path));
5151
}
5252
};
53-
};
53+
}
5454

5555
// Used to speed up module loading. Returns the contents of the file as
5656
// a string or undefined when the file cannot be opened. The speedup

0 commit comments

Comments
 (0)