Skip to content

Commit

Permalink
fix: fix module-root.js package.json in subpath
Browse files Browse the repository at this point in the history
  • Loading branch information
dword-design committed Jan 9, 2024
1 parent a464654 commit cdbf4d8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/utils/module-root.js
Expand Up @@ -7,14 +7,16 @@ export default (...args) => {
const name = args.find((arg) => typeof arg === 'string');
const options = args.find((arg) => typeof arg === 'object') || {};
options.cwd = options.cwd || process.cwd();
let pkg;
try {
const fullpath = name
? resolveFrom(options.cwd, name)
: callsite()[1].getFileName();
pkg = findup('package.json', { cwd: path.dirname(fullpath) });
if (name) {
const fullpath = resolveFrom(options.cwd, name);
const index = fullpath.indexOf(name.replace(/\//g, path.sep))

Check failure on line 13 in src/utils/module-root.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 16.x)

Insert `;`

Check failure on line 13 in src/utils/module-root.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

Insert `;`

Check failure on line 13 in src/utils/module-root.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20.x)

Insert `;`
return fullpath.substring(0, index + name.length)

Check failure on line 14 in src/utils/module-root.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 16.x)

Insert `;`

Check failure on line 14 in src/utils/module-root.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

Insert `;`

Check failure on line 14 in src/utils/module-root.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20.x)

Insert `;`
} else {

Check failure on line 15 in src/utils/module-root.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 16.x)

Unnecessary 'else' after 'return'

Check failure on line 15 in src/utils/module-root.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

Unnecessary 'else' after 'return'

Check failure on line 15 in src/utils/module-root.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20.x)

Unnecessary 'else' after 'return'
return path.dirname(findup('package.json', { cwd: path.dirname(callsite()[1].getFileName()) }))

Check failure on line 16 in src/utils/module-root.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 16.x)

Replace `findup('package.json',·{·cwd:·path.dirname(callsite()[1].getFileName())·}))` with `⏎········findup('package.json',·{⏎··········cwd:·path.dirname(callsite()[1].getFileName()),⏎········}),⏎······);`

Check failure on line 16 in src/utils/module-root.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

Replace `findup('package.json',·{·cwd:·path.dirname(callsite()[1].getFileName())·}))` with `⏎········findup('package.json',·{⏎··········cwd:·path.dirname(callsite()[1].getFileName()),⏎········}),⏎······);`

Check failure on line 16 in src/utils/module-root.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20.x)

Replace `findup('package.json',·{·cwd:·path.dirname(callsite()[1].getFileName())·}))` with `⏎········findup('package.json',·{⏎··········cwd:·path.dirname(callsite()[1].getFileName()),⏎········}),⏎······);`
}
} catch {
pkg = resolveFrom(options.cwd, `${args[0]}/package.json`);
const pkg = resolveFrom(options.cwd, `${name}/package.json`);
return path.resolve(path.dirname(pkg));
}
return path.resolve(path.dirname(pkg));
};
7 changes: 7 additions & 0 deletions test/node_modules/esm-packagejson-subpath/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions test/special/bin.js
Expand Up @@ -125,6 +125,12 @@ const testCases = [
dependencies: ['@scoped/scoped-binary-package'],
expected: ['@scoped/scoped-binary-package'],
},
{
name: 'esm package.json in subpath',
script: 'esm-packagejson-subpath',
dependencies: ['esm-packagejson-subpath'],
expected: ['esm-packagejson-subpath'],
},
{
name: 'esm',
script: 'esm',
Expand Down

0 comments on commit cdbf4d8

Please sign in to comment.