diff --git a/lib/path.js b/lib/path.js new file mode 100644 index 00000000..ffe453d9 --- /dev/null +++ b/lib/path.js @@ -0,0 +1,4 @@ +const isWindows = typeof process === 'object' && + process && + process.platform === 'win32' +module.exports = isWindows ? { sep: '\\' } : { sep: '/' } diff --git a/minimatch.js b/minimatch.js index 0ced35ef..f3b491dd 100644 --- a/minimatch.js +++ b/minimatch.js @@ -11,9 +11,7 @@ const minimatch = module.exports = (p, pattern, options = {}) => { module.exports = minimatch -const path = (() => { try { return require('path') } catch (e) {}})() || { - sep: '/' -} +const path = require('./lib/path.js') minimatch.sep = path.sep const GLOBSTAR = Symbol('globstar **') diff --git a/package.json b/package.json index 66e6f51d..4a4b571e 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ }, "license": "ISC", "files": [ - "minimatch.js" + "minimatch.js", + "lib" ] } diff --git a/test/no-path-module.js b/test/no-path-module.js index c681ea96..653c56e8 100644 --- a/test/no-path-module.js +++ b/test/no-path-module.js @@ -1,3 +1,6 @@ const t = require('tap') -const mm = t.mock('../', { path: null }) +const proc = process +global.process = null +const mm = t.mock('../minimatch.js') +global.process = proc t.equal(mm.sep, '/') diff --git a/test/win-path-sep.js b/test/win-path-sep.js index 95679d3f..4b48d226 100644 --- a/test/win-path-sep.js +++ b/test/win-path-sep.js @@ -1,6 +1,6 @@ const t = require('tap') t.test('path separator /', t => { - const mm = t.mock('../', { path: { sep: '/' }}) + const mm = t.mock('../', { '../lib/path.js': { sep: '/' }}) t.equal(mm('x/y/z', 'x/y/*/z'), false) t.equal(mm('x/y/w/z', 'x/y/*/z'), true) @@ -8,7 +8,7 @@ t.test('path separator /', t => { }) t.test('path separator \\', t => { - const mm = t.mock('../', { path: { sep: '\\' }}) + const mm = t.mock('../', { '../lib/path.js': { sep: '\\' }}) t.equal(mm('x\\y\\z', 'x/y/*/z'), false) t.equal(mm('x\\y\\w\\z', 'x/y/*/z'), true) @@ -17,9 +17,25 @@ t.test('path separator \\', t => { // just in case Node every adds Mac OS 9 support 😅 t.test('path separator :', t => { - const mm = t.mock('../', { path: { sep: ':' }}) + const mm = t.mock('../', { '../lib/path.js': { sep: ':' }}) t.equal(mm('x:y:z', 'x/y/*/z'), false) t.equal(mm('x:y:w:z', 'x/y/*/z'), true) t.end() }) + +t.test('windows default', t => { + const proc = global.process + global.process = { platform: 'win32' } + t.equal(t.mock('../lib/path.js').sep, '\\') + global.process = proc + t.end() +}) + +t.test('posix default', t => { + const proc = global.process + global.process = { platform: 'posix' } + t.equal(t.mock('../lib/path.js').sep, '/') + global.process = proc + t.end() +})