@everymundo/cleanrequire
Advanced tools
Comparing version 1.1.1 to 1.2.0
17
index.js
@@ -7,2 +7,10 @@ 'use strict'; | ||
const os = require('os'); | ||
const prependSlash = (str) => { | ||
if (os.platform() === 'win32') return str; | ||
return `/${str}`; | ||
}; | ||
const cleanrequire = (libpath) => { | ||
@@ -16,7 +24,8 @@ if (!/^\./.test(libpath)) return removeCacheAndLoad(libpath); | ||
secndI = (stack + '\n').indexOf('\n', firstI + 1), | ||
firstMatch = stack.substring(firstI, secndI).match(/\s+\(?(\/([^:]+):\d+)/), | ||
line = stack.substring(firstI, secndI), | ||
firstMatch = line.match(/\s+\(?((?:\/|\w:)([^:]+):\d+)/), | ||
// baseDir = firstMatch ? path.dirname(firstMatch[2]) : process.cwd(); | ||
baseDir = firstMatchOrCWD(firstMatch); | ||
const libFile = '/' + path.join(baseDir, libpath).replace(/^\//, ''); | ||
const libFile = prependSlash(path.join(baseDir, libpath).replace(/^\//, '')); | ||
@@ -34,3 +43,5 @@ return removeCacheAndLoad(libFile); | ||
cleanrequire.firstMatchOrCWD = firstMatchOrCWD; | ||
cleanrequire.prependSlash = prependSlash; | ||
module.exports = cleanrequire; | ||
module.exports = cleanrequire; |
{ | ||
"name": "@everymundo/cleanrequire", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Cleans nodejs require.cache for a given module and requires it again. Very useful for testing purposes.", | ||
@@ -27,4 +27,5 @@ "main": "index.js", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^5.0.1" | ||
"mocha": "^5.2.0", | ||
"sinon": "^6.2.0" | ||
} | ||
} |
@@ -8,2 +8,3 @@ 'use strict'; | ||
{ expect } = require('chai'), | ||
sinon = require('sinon'), | ||
cleanrequire = require('../'); | ||
@@ -22,2 +23,36 @@ | ||
describe('prependSlash', () => { | ||
context('NOT running on windows', () => { | ||
it('shoud return a string prepended by a slash', () => { | ||
const { prependSlash } = cleanrequire; | ||
const input = 'C:\\path\\another-path'; | ||
const expected = `/${input}`; | ||
const res = prependSlash(input); | ||
expect(res).to.equal(expected); | ||
}); | ||
}); | ||
context('running on windows', () => { | ||
const os = require('os'); | ||
beforeEach(() => { | ||
sinon.stub(os, 'platform').callsFake(() => 'win32'); | ||
}); | ||
afterEach(() => { | ||
os.platform.restore(); | ||
}); | ||
it('shoud return the exact same string without prepending the slash', () => { | ||
const { prependSlash } = cleanrequire; | ||
const input = 'C:\\path\\another-path'; | ||
const res = prependSlash(input); | ||
expect(res).to.equal(input); | ||
}); | ||
}); | ||
}); | ||
describe('require', () => { | ||
@@ -24,0 +59,0 @@ it('require should cache and return cached version on the second call', (done) => { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7247
104
4