Skip to content

Commit 1073c65

Browse files
authored
Merge pull request #2 from fearthecowboy/master
fixes path issues on windows for non-unixified paths
2 parents afbfcc1 + 0e8c81b commit 1073c65

File tree

6 files changed

+156
-5
lines changed

6 files changed

+156
-5
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = require('lib/index');
1+
module.exports = require('./lib/index');

lib/correctPath.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.unixify = unixify;
7+
exports.correctPath = correctPath;
8+
var isWin = process.platform === 'win32';
9+
10+
function removeTrailingSeparator(str) {
11+
var i = str.length - 1;
12+
if (i < 2) {
13+
return str;
14+
}
15+
while (isSeparator(str, i)) {
16+
i--;
17+
}
18+
return str.substr(0, i + 1);
19+
};
20+
21+
function isSeparator(str, i) {
22+
var char = str[i];
23+
return i > 0 && (char === '/' || isWin && char === '\\');
24+
}
25+
26+
function normalizePath(str, stripTrailing) {
27+
if (typeof str !== 'string') {
28+
throw new TypeError('expected a string');
29+
}
30+
str = str.replace(/[\\\/]+/g, '/');
31+
if (stripTrailing !== false) {
32+
str = removeTrailingSeparator(str);
33+
}
34+
return str;
35+
};
36+
37+
function unixify(filepath) {
38+
var stripTrailing = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
39+
40+
if (isWin) {
41+
filepath = normalizePath(filepath, stripTrailing);
42+
return filepath.replace(/^([a-zA-Z]+:|\.\/)/, '');
43+
}
44+
return filepath;
45+
};
46+
47+
function correctPath(filepath) {
48+
return unixify(filepath.replace(/^\\\\\?\\.:\\/, '\\'));
49+
}

lib/patchRequire.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ var path = _interopRequireWildcard(_path);
1111

1212
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
1313

14+
var correctPath = process.platform === 'win32' ? require('./correctPath').correctPath : function (p) {
15+
return p;
16+
};
17+
1418
function stripBOM(content) {
1519
if (content.charCodeAt(0) === 0xFEFF) {
1620
content = content.slice(1);
@@ -21,6 +25,23 @@ function stripBOM(content) {
2125
function patchRequire(vol) {
2226
var Module = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : require('module');
2327

28+
if (process.platform === 'win32') {
29+
var original = vol;
30+
vol = {
31+
readFileSync: function readFileSync(path, options) {
32+
return original.readFileSync(correctPath(path), options);
33+
},
34+
35+
realpathSync: function realpathSync(path) {
36+
return original.realpathSync(correctPath(path));
37+
},
38+
39+
statSync: function statSync(path) {
40+
return original.statSync(correctPath(path));
41+
}
42+
};
43+
};
44+
2445
function internalModuleReadFile(path) {
2546
try {
2647
return vol.readFileSync(path, 'utf8');
@@ -138,7 +159,7 @@ function patchRequire(vol) {
138159
for (var i = 0; i < paths.length; i++) {
139160
var curPath = paths[i];
140161
if (curPath && stat(curPath) < 1) continue;
141-
var basePath = path.resolve(curPath, request);
162+
var basePath = correctPath(path.resolve(curPath, request));
142163
var filename;
143164

144165
var rc = stat(basePath);

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.3",
3+
"version": "0.1.5",
44
"description": "Monkey patches for file system related things.",
55
"main": "lib/index.js",
66
"keywords": [

src/correctPath.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const isWin = process.platform === 'win32';
2+
/*!
3+
* removeTrailingSeparator <https://github.com/darsain/remove-trailing-separator>
4+
*
5+
* Inlined from:
6+
* Copyright (c) darsain.
7+
* Released under the ISC License.
8+
*/
9+
function removeTrailingSeparator(str) {
10+
var i = str.length - 1;
11+
if (i < 2) {
12+
return str;
13+
}
14+
while (isSeparator(str, i)) {
15+
i--;
16+
}
17+
return str.substr(0, i + 1);
18+
};
19+
20+
function isSeparator(str, i) {
21+
var char = str[i];
22+
return i > 0 && (char === '/' || (isWin && char === '\\'));
23+
}
24+
25+
/*!
26+
* normalize-path <https://github.com/jonschlinkert/normalize-path>
27+
*
28+
* Inlined from:
29+
* Copyright (c) 2014-2017, Jon Schlinkert.
30+
* Released under the MIT License.
31+
*/
32+
function normalizePath(str, stripTrailing) {
33+
if (typeof str !== 'string') {
34+
throw new TypeError('expected a string');
35+
}
36+
str = str.replace(/[\\\/]+/g, '/');
37+
if (stripTrailing !== false) {
38+
str = removeTrailingSeparator(str);
39+
}
40+
return str;
41+
};
42+
43+
/*!
44+
* unixify <https://github.com/jonschlinkert/unixify>
45+
*
46+
* Inlined from:
47+
* Copyright (c) 2014, 2017, Jon Schlinkert.
48+
* Released under the MIT License.
49+
*/
50+
export function unixify(filepath, stripTrailing = true) {
51+
if(isWin) {
52+
filepath = normalizePath(filepath, stripTrailing);
53+
return filepath.replace(/^([a-zA-Z]+:|\.\/)/, '');
54+
}
55+
return filepath;
56+
};
57+
58+
/*
59+
* Corrects a windows path to unix format (including \\?\c:...)
60+
*/
61+
export function correctPath(filepath) {
62+
return unixify( filepath.replace(/^\\\\\?\\.:\\/,'\\'));
63+
}

src/patchRequire.js

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

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

45
/**
56
* Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
@@ -13,7 +14,6 @@ function stripBOM(content) {
1314
return content;
1415
}
1516

16-
1717
/**
1818
* Rewrites `modules.js`, which is the factory for the `require` function.
1919
* You give this function your custom file system object and this function
@@ -34,6 +34,24 @@ function stripBOM(content) {
3434
*/
3535
export default function patchRequire(vol, Module = require('module')) {
3636

37+
// ensure all paths are corrected before use.
38+
if( process.platform === 'win32') {
39+
const original = vol;
40+
vol = {
41+
readFileSync: (path,options) => {
42+
return original.readFileSync(correctPath( path ),options);
43+
},
44+
45+
realpathSync: (path) => {
46+
return original.realpathSync(correctPath( path ));
47+
},
48+
49+
statSync: (path) => {
50+
return original.statSync(correctPath( path ));
51+
}
52+
};
53+
};
54+
3755
// Used to speed up module loading. Returns the contents of the file as
3856
// a string or undefined when the file cannot be opened. The speedup
3957
// comes from not creating Error objects on failure.
@@ -177,7 +195,7 @@ export default function patchRequire(vol, Module = require('module')) {
177195
// Don't search further if path doesn't exist
178196
const curPath = paths[i];
179197
if (curPath && stat(curPath) < 1) continue;
180-
var basePath = path.resolve(curPath, request);
198+
var basePath = correctPath( path.resolve(curPath, request) );
181199
var filename;
182200

183201
var rc = stat(basePath);

0 commit comments

Comments
 (0)