Skip to content

Commit

Permalink
Merge pull request #32 from iambumblehead/resolve-pg-pattern
Browse files Browse the repository at this point in the history
resolve pg main: ./lib definition
  • Loading branch information
iambumblehead authored Aug 19, 2022
2 parents 7624f9e + ab99e2d commit 758f433
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# changelog

* 0.8.9 _Aug.19.2022_
* resolve pg packages cjs ["main": "./lib"](https://github.com/iambumblehead/resolvewithplus/pull/32)
* 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_
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "resolvewithplus",
"version": "0.8.8",
"version": "0.8.9",
"engines" : {
"node" : ">=12.16.0"
},
Expand Down
13 changes: 6 additions & 7 deletions resolvewithplus.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,6 @@ export default (o => {
return indexval;
};

o.getpackagepath = (jsonfile, opts) => (
o.isfilesync(jsonfile) && (jsonfile = require(jsonfile)) &&
(o.gettargetindex(jsonfile, opts) || jsonfile.main));

// https://nodejs.org/api/modules.html#modules_module_require_id
//
// LOAD_AS_FILE(X)
Expand Down Expand Up @@ -181,9 +177,12 @@ export default (o => {
let filepath = null;
let relpath;
let json = path.join(d, packagejson);

if ((relpath = o.getpackagepath(json, opts))) {
filepath = o.getasfilesync(path.join(d, relpath));
let jsonobj = o.isfilesync(json) && require(json);
if ((relpath = o.gettargetindex(jsonobj, opts))) {
filepath = o.getasfilesync(path.join(d, relpath))
} else if ((relpath = jsonobj.main)) {
filepath = o.getasfilesync(path.join(d, relpath))
|| o.getasfilesync(path.join(d, path.join(relpath, 'index')));
} else {
supportedExtensions.some(f => (
(f = path.join(d, `index${f}`)) && o.isfilesync(f) && (filepath = f)));
Expand Down
1 change: 1 addition & 0 deletions tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"main": "package.json.resolvewithplus.export.js",
"dependencies": {
"npm-run-all": "^4.1.5",
"pg": "8.7.3",
"got": "12.0.1",
"eslint": "^8.0.1",
"koa": "^2.13.4",
Expand Down
12 changes: 10 additions & 2 deletions tests/tests-basic/tests-basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Timestamp: 2017.04.23-23:31:33 (last modified)
// Author(s): bumblehead <[email protected]>

import url, { fileURLToPath } from 'url';
import url from 'url';
import os from 'os';
import path from 'path';
import test from 'node:test'
Expand All @@ -18,7 +18,7 @@ test('should convert win32 path to node-friendly posix path', () => {
})

test('should pass windows and posix system-specific module path', () => {
const modulePath = fileURLToPath(
const modulePath = url.fileURLToPath(
new URL('../testfiles/testscript.js', import.meta.url))
const calleePath = import.meta.url;
const returnPath = resolvewithplus(modulePath, calleePath)
Expand Down Expand Up @@ -150,6 +150,14 @@ test('should handle package.json stringy "exports" field (got)', () => {
path.resolve('../node_modules/got/dist/source/index.js'));
});

test('should handle package.json "main": "./lib" field (pg)', () => {
const fullpath = path.resolve('../testfiles/');

assert.strictEqual(
resolvewithplus('pg', fullpath),
path.resolve('../node_modules/pg/lib/index.js'));
});

test('should return values from cache', () => {
resolvewithplus.cache['filepathkey'] = 'filepathvalue';

Expand Down

0 comments on commit 758f433

Please sign in to comment.