Comparing version 0.1.3 to 0.2.0
@@ -1,1 +0,1 @@ | ||
module.exports = require('lib/index'); | ||
module.exports = require('./lib/index'); |
@@ -14,2 +14,6 @@ 'use strict'; | ||
var correctPath = process.platform === 'win32' ? require('./correctPath').correctPath : function (p) { | ||
return p; | ||
}; | ||
function stripBOM(content) { | ||
@@ -23,4 +27,22 @@ if (content.charCodeAt(0) === 0xFEFF) { | ||
function patchRequire(vol) { | ||
var Module = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : require('module'); | ||
var unixifyPaths = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; | ||
var Module = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : require('module'); | ||
if (process.platform === 'win32' && unixifyPaths) { | ||
var original = vol; | ||
vol = { | ||
readFileSync: function readFileSync(path, options) { | ||
return original.readFileSync(correctPath(path), options); | ||
}, | ||
realpathSync: function realpathSync(path) { | ||
return original.realpathSync(correctPath(path)); | ||
}, | ||
statSync: function statSync(path) { | ||
return original.statSync(correctPath(path)); | ||
} | ||
}; | ||
} | ||
function internalModuleReadFile(path) { | ||
@@ -143,3 +165,3 @@ try { | ||
if (curPath && stat(curPath) < 1) continue; | ||
var basePath = path.resolve(curPath, request); | ||
var basePath = correctPath(path.resolve(curPath, request)); | ||
var filename; | ||
@@ -146,0 +168,0 @@ |
{ | ||
"name": "fs-monkey", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"description": "Monkey patches for file system related things.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -60,3 +60,3 @@ # `fs-monkey` | ||
# `patchRequire(vol[, Module])` | ||
# `patchRequire(vol[, unixifyPaths[, Module]])` | ||
@@ -66,2 +66,3 @@ Patches Node's `module` module to use a given *fs-like* object `vol` for module loading. | ||
- `vol` - fs-like object | ||
- `unixifyPaths` *(optional)* - whether to convert Windows paths to unix style paths, defaults to `false`. | ||
- `Module` *(optional)* - a module to patch, defaults to `require('module')` | ||
@@ -68,0 +69,0 @@ |
import * as path from 'path'; | ||
const correctPath = process.platform === 'win32' ? require('./correctPath').correctPath : p => p; | ||
@@ -16,3 +17,2 @@ /** | ||
/** | ||
@@ -36,4 +36,22 @@ * Rewrites `modules.js`, which is the factory for the `require` function. | ||
*/ | ||
export default function patchRequire(vol, Module = require('module')) { | ||
export default function patchRequire(vol, unixifyPaths = true, Module = require('module')) { | ||
// ensure all paths are corrected before use. | ||
if(process.platform === 'win32' && unixifyPaths) { | ||
const original = vol; | ||
vol = { | ||
readFileSync: (path,options) => { | ||
return original.readFileSync(correctPath(path),options); | ||
}, | ||
realpathSync: (path) => { | ||
return original.realpathSync(correctPath(path)); | ||
}, | ||
statSync: (path) => { | ||
return original.statSync(correctPath(path)); | ||
} | ||
}; | ||
} | ||
// Used to speed up module loading. Returns the contents of the file as | ||
@@ -182,3 +200,3 @@ // a string or undefined when the file cannot be opened. The speedup | ||
if (curPath && stat(curPath) < 1) continue; | ||
var basePath = path.resolve(curPath, request); | ||
var basePath = correctPath( path.resolve(curPath, request) ); | ||
var filename; | ||
@@ -185,0 +203,0 @@ |
36477
28
833
146