From 2c3007c61d4f09798103311f66ecf3dca0fb82b6 Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 16 Aug 2022 07:34:24 -0700 Subject: [PATCH 1/5] add initial test --- tests/tests-basic/tests-basic.test.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/tests-basic/tests-basic.test.js b/tests/tests-basic/tests-basic.test.js index c2d95de..25e855d 100644 --- a/tests/tests-basic/tests-basic.test.js +++ b/tests/tests-basic/tests-basic.test.js @@ -2,12 +2,29 @@ // Timestamp: 2017.04.23-23:31:33 (last modified) // Author(s): bumblehead -import url from 'url'; +import url, { fileURLToPath } from 'url'; +import os from 'os'; import path from 'path'; import test from 'node:test' import assert from 'node:assert/strict' import resolvewithplus from '../../resolvewithplus.js'; +test('should pass windows and posix system-specific module path', () => { + const modulePath = fileURLToPath( + new URL('../testfiles/testscript.js', import.meta.url)) + const calleePath = import.meta.url; + const returnPath = resolvewithplus(modulePath, calleePath) + + if (os.platform() === 'win32') { + assert.ok(returnPath.endsWith('/tests/testfiles/testscript.js')) + } else { + assert.ok(returnPath.endsWith('/tests/testfiles/testscript.js')) + } + + console.log({ modulePath, calleePath, returnPath }) + assert.ok(returnPath) +}); + test('should return a core module reference as require.resolve id', () => { assert.strictEqual(resolvewithplus('path'), 'path'); }); From b39e34c6ce5a6a7915ea2134d9ab9c08f4cc3ee1 Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 16 Aug 2022 07:43:41 -0700 Subject: [PATCH 2/5] check if returnpath stringy --- tests/tests-basic/tests-basic.test.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/tests-basic/tests-basic.test.js b/tests/tests-basic/tests-basic.test.js index 25e855d..94c56e3 100644 --- a/tests/tests-basic/tests-basic.test.js +++ b/tests/tests-basic/tests-basic.test.js @@ -14,15 +14,19 @@ test('should pass windows and posix system-specific module path', () => { new URL('../testfiles/testscript.js', import.meta.url)) const calleePath = import.meta.url; const returnPath = resolvewithplus(modulePath, calleePath) - + console.log({ modulePath, calleePath, returnPath }) + // posix modulePath + // /root/resolvewithplus/tests/testfiles/testscript.js + // posix calleePath + // file:///root/resolvewithplus/tests/tests-basic/tests-basic.test.js + // posix returnPath + // /root/resolvewithplus/tests/testfiles/testscript.js + assert.ok(typeof returnPath === 'string') if (os.platform() === 'win32') { assert.ok(returnPath.endsWith('/tests/testfiles/testscript.js')) } else { assert.ok(returnPath.endsWith('/tests/testfiles/testscript.js')) } - - console.log({ modulePath, calleePath, returnPath }) - assert.ok(returnPath) }); test('should return a core module reference as require.resolve id', () => { From 2ffeb7aefa164054ba76a303b556d6c625a48e38 Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 16 Aug 2022 08:08:19 -0700 Subject: [PATCH 3/5] convert win32 path to posix path, add test --- resolvewithplus.js | 11 ++++++++++- tests/tests-basic/tests-basic.test.js | 20 +++++++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/resolvewithplus.js b/resolvewithplus.js index 425ed7d..913dbaf 100644 --- a/resolvewithplus.js +++ b/resolvewithplus.js @@ -11,12 +11,15 @@ const isBuiltinRe = new RegExp( '^(?:node:)?('+module.builtinModules.join('|').replace('/', '\/')+')$'); const isDirPathRe = /^\.?\.?(\/|\\)/; const isRelPathRe = /^.\.?(?=\/|\\)/; +const isWin32PathRe = /\\/g; +const isWin32DriveRe = /^[a-zA-Z]:/; const isSupportedIndexRe = /index.[tj]sx?$/; const isResolveWithPathRe = /[\\/]resolvewithplus[\\/]/; const supportedExtensions = [ '.js', '.mjs', '.ts', '.tsx', '.json', '.node' ]; const node_modules = 'node_modules'; const packagejson = 'package.json'; + export default (o => { o = (requirepath, withpath, opts) => { let resolvedpath = o.cache[requirepath+withpath]; @@ -28,6 +31,12 @@ export default (o => { }; o.cache = {}; + + // ex, D:\\a\\resolvewithplus\\pathto\\testfiles\\testscript.js + // -> /a/resolvewithplus/pathto/testfiles/testscript.js + o.pathToPosix = pathany => isWin32PathRe.test(pathany) + ? pathany.replace(isWin32DriveRe, '').replace(isWin32PathRe, path.posix.sep) + : pathany // https://nodejs.org/api/modules.html#modules_module_require_id // @@ -51,7 +60,7 @@ export default (o => { fullpath = requirepath; } else { fullpath = isDirPathRe.test(requirepath) - ? o.getasfileordir(requirepath, withpath, opts) + ? o.getasfileordir(o.pathToPosix(requirepath), withpath, opts) : o.getasnode_module(requirepath, withpath); fullpath = fullpath && realpath(fullpath); diff --git a/tests/tests-basic/tests-basic.test.js b/tests/tests-basic/tests-basic.test.js index 94c56e3..8e295a2 100644 --- a/tests/tests-basic/tests-basic.test.js +++ b/tests/tests-basic/tests-basic.test.js @@ -9,6 +9,14 @@ import test from 'node:test' import assert from 'node:assert/strict' import resolvewithplus from '../../resolvewithplus.js'; +test('should convert win32 path to node-friendly posix path', () => { + const win32Path = 'D:\\a\\resolvewithplus\\pathto\\testfiles\\testscript.js'; + const posixPath = '/a/resolvewithplus/pathto/testfiles/testscript.js'; + const returnPath = resolvewithplus.pathToPosix(win32Path); + + assert.strictEqual(returnPath, posixPath); +}) + test('should pass windows and posix system-specific module path', () => { const modulePath = fileURLToPath( new URL('../testfiles/testscript.js', import.meta.url)) @@ -16,11 +24,17 @@ test('should pass windows and posix system-specific module path', () => { const returnPath = resolvewithplus(modulePath, calleePath) console.log({ modulePath, calleePath, returnPath }) // posix modulePath - // /root/resolvewithplus/tests/testfiles/testscript.js + // /root/pathto/testfiles/testscript.js // posix calleePath - // file:///root/resolvewithplus/tests/tests-basic/tests-basic.test.js + // file:///root/pathto/tests-basic/tests-basic.test.js // posix returnPath - // /root/resolvewithplus/tests/testfiles/testscript.js + // /root/pathto/testfiles/testscript.js + // + // win32 modulePath + // D:\\a\\resolvewithplus\\pathto\\testfiles\\testscript.js + // win32 calleePath eslint-disable-next-line max-len + // file:///D:/a/resolvewithplus/pathto/tests-basic/tests-basic.test.js + // returnPath: null assert.ok(typeof returnPath === 'string') if (os.platform() === 'win32') { assert.ok(returnPath.endsWith('/tests/testfiles/testscript.js')) From 34dfc6dc257f5cee6c41dcd1000f90daf43e2c52 Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 16 Aug 2022 23:06:41 -0700 Subject: [PATCH 4/5] update isDirPathRe --- resolvewithplus.js | 2 +- tests/tests-basic/tests-basic.test.js | 13 ++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/resolvewithplus.js b/resolvewithplus.js index 913dbaf..690a480 100644 --- a/resolvewithplus.js +++ b/resolvewithplus.js @@ -9,7 +9,7 @@ const require = module.createRequire(import.meta.url); const realpath = fs.realpathSync.native; const isBuiltinRe = new RegExp( '^(?:node:)?('+module.builtinModules.join('|').replace('/', '\/')+')$'); -const isDirPathRe = /^\.?\.?(\/|\\)/; +const isDirPathRe = /^\.?\.?([a-zA-Z]:)?(\/|\\)/; const isRelPathRe = /^.\.?(?=\/|\\)/; const isWin32PathRe = /\\/g; const isWin32DriveRe = /^[a-zA-Z]:/; diff --git a/tests/tests-basic/tests-basic.test.js b/tests/tests-basic/tests-basic.test.js index 8e295a2..8e97fbc 100644 --- a/tests/tests-basic/tests-basic.test.js +++ b/tests/tests-basic/tests-basic.test.js @@ -22,7 +22,6 @@ test('should pass windows and posix system-specific module path', () => { new URL('../testfiles/testscript.js', import.meta.url)) const calleePath = import.meta.url; const returnPath = resolvewithplus(modulePath, calleePath) - console.log({ modulePath, calleePath, returnPath }) // posix modulePath // /root/pathto/testfiles/testscript.js // posix calleePath @@ -34,13 +33,13 @@ test('should pass windows and posix system-specific module path', () => { // D:\\a\\resolvewithplus\\pathto\\testfiles\\testscript.js // win32 calleePath eslint-disable-next-line max-len // file:///D:/a/resolvewithplus/pathto/tests-basic/tests-basic.test.js - // returnPath: null + // returnPath + // D:\\a\\resolvewithplus\\pathto\\testfiles\\testscript.js assert.ok(typeof returnPath === 'string') - if (os.platform() === 'win32') { - assert.ok(returnPath.endsWith('/tests/testfiles/testscript.js')) - } else { - assert.ok(returnPath.endsWith('/tests/testfiles/testscript.js')) - } + assert.ok(returnPath.endsWith( + os.platform() === 'win32' + ? '\\tests\\testfiles\\testscript.js' + : '/tests/testfiles/testscript.js')) }); test('should return a core module reference as require.resolve id', () => { From cf2a8c2f9cf1469fb53ddda9517dd5f3b002f34e Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 16 Aug 2022 23:11:23 -0700 Subject: [PATCH 5/5] increment version number and changelog --- CHANGELOG.md | 2 ++ package.json | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 137a476..0e1e8f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # changelog + * 0.8.8 _Aug.16.2022_ + * support win32 [drive-style module-path](https://github.com/iambumblehead/resolvewithplus/pull/31) * 0.8.7 _Aug.15.2022_ * support core modules [w/ node: prefix](https://github.com/iambumblehead/resolvewithplus/pull/27), credit @gmahomarf * 0.8.6 _Aug.01.2022_ diff --git a/package.json b/package.json index b73bc1a..44d757b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "resolvewithplus", - "version": "0.8.7", + "version": "0.8.8", "engines" : { "node" : ">=12.16.0" },