resolve-pkglock 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -17943,63 +17943,71 @@ async function init(workspaceRoot) {
17943
17943
  addToRegistry(packageRegistry, pkgInfo);
17944
17944
  }
17945
17945
  }
17946
+ function getParent(parentURL) {
17947
+ if (parentURL == null) {
17948
+ throw new Error('No parentURL!');
17949
+ }
17950
+ let pkg;
17951
+ // bottom-up first, because there are packages that introduce multiple package.json in their hierarchy
17952
+ if (parentURL.startsWith(virtualStoreDirUrlPath)) {
17953
+ const subpath = parentURL.substring(0, parentURL.indexOf('/node_modules/', virtualStoreDirUrlPath.length + 1) + 14), // '/node_modules/'.length = 14
17954
+ match = parentURL.substring(subpath.length).match(PACKAGE_REGEX);
17955
+ if (match == null) {
17956
+ throw new Error('can\'t parse calling module\'s path');
17957
+ }
17958
+ const [, name] = match;
17959
+ pkg = dirToPackage[join(fileURLToPath(subpath), name)];
17960
+ }
17961
+ else {
17962
+ pkg = dirToPackage[dirname(findPackageJSON(parentURL))];
17963
+ }
17964
+ if (pkg == null || pkg.name == null || pkg.version == null) {
17965
+ throw new Error('unknown package');
17966
+ }
17967
+ const { name: parentName, version: parentVersion } = pkg, parent = packageRegistry.get(parentName ?? null)?.get(parentVersion ?? null);
17968
+ if (parent == null) {
17969
+ throw new Error('can\'t identify parent');
17970
+ }
17971
+ return parent;
17972
+ }
17946
17973
  registerHooks({
17947
17974
  resolve(specifier, context, next) {
17948
- let parent, packageLocation;
17949
17975
  if (isBuiltin(specifier) || isAbsolute(specifier)) {
17950
17976
  return next(specifier, context);
17951
17977
  }
17952
17978
  else if (specifier.startsWith('.')) {
17953
17979
  return next(join(dirname(context.parentURL ? fileURLToPath(context.parentURL) : '.'), specifier), context);
17954
17980
  }
17955
- else {
17956
- if (context.parentURL == null) {
17957
- throw new Error('No parentURL!');
17958
- }
17959
- let pkg;
17960
- // bottom-up first, because there are packages that introduce multiple package.json in their hierarchy
17961
- if (context.parentURL.startsWith(virtualStoreDirUrlPath)) {
17962
- const subpath = context.parentURL.substring(0, context.parentURL.indexOf('/node_modules/', virtualStoreDirUrlPath.length + 1) + 14), // '/node_modules/'.length = 14
17963
- match = context.parentURL.substring(subpath.length).match(PACKAGE_REGEX);
17964
- if (match == null) {
17965
- throw new Error('can\'t parse path');
17966
- }
17967
- const [, name] = match;
17968
- pkg = dirToPackage[join(fileURLToPath(subpath), name)];
17969
- }
17970
- else {
17971
- pkg = dirToPackage[dirname(findPackageJSON(context.parentURL))];
17972
- }
17973
- if (pkg == null || pkg.name == null || pkg.version == null) {
17974
- throw new Error('unknown package');
17975
- }
17976
- const { name: parentName, version: parentVersion } = pkg;
17977
- parent = packageRegistry.get(parentName ?? null)?.get(parentVersion ?? null);
17978
- }
17979
17981
  const match = specifier.match(PACKAGE_REGEX);
17980
17982
  if (match == null) {
17981
- throw new Error('can\'t read specifier');
17983
+ throw new Error('can\'t read package specifier');
17982
17984
  }
17983
- const [, name, appendix] = match, version = parent?.dependencies?.[name] || parent?.devDependencies?.[name] || parent?.optionalDependencies?.[name];
17985
+ const [, name, appendix] = match, // strip appendix as in somepackage/appendix or @some/package/appendix
17986
+ parent = getParent(context.parentURL), version = parent.dependencies?.[name] || parent.devDependencies?.[name] || parent.optionalDependencies?.[name];
17984
17987
  if (version == null) {
17985
- throw new Error('can\'t find matching version');
17988
+ // some modules test for optional modules and require standard error code
17989
+ const err = new Error(`Cannot find module '${specifier}'.`);
17990
+ err.code = 'MODULE_NOT_FOUND';
17991
+ throw err;
17986
17992
  }
17987
- if (version?.startsWith('link:')) {
17988
- if (parent == null) {
17989
- throw new Error('link with no parent');
17990
- }
17993
+ let packageLocation;
17994
+ if (version.startsWith('link:')) {
17991
17995
  packageLocation = join(parent.packageLocation, version.substring(5));
17992
17996
  }
17997
+ else if (version.startsWith(`${name}@file://`)) { // plugin-commands-deploy/src/createDeployFiles.ts:createFileUrlDepPath()
17998
+ packageLocation = join(virtualStoreDir, libExports$2.depPathToFilename(version, virtualStoreDirMaxLength), 'node_modules', name);
17999
+ }
17993
18000
  else {
17994
18001
  packageLocation = join(virtualStoreDir, libExports$2.depPathToFilename(`${name}@${version}`, virtualStoreDirMaxLength), 'node_modules', name);
17995
18002
  }
17996
18003
  if (packageLocation[packageLocation.length - 1] === '/') {
17997
18004
  packageLocation = packageLocation.substring(0, packageLocation.length - 1);
17998
18005
  }
17999
- if (packageLocation.endsWith(`/node_modules/${name}`)) { // required, if there's appendixes
18006
+ if (packageLocation.endsWith(`/node_modules/${name}`)) {
18000
18007
  const dedicatedRequire = createRequire(pathToFileURL(packageLocation.substring(0, packageLocation.length - 13 - name.length)));
18001
18008
  return next(dedicatedRequire.resolve(specifier), context);
18002
18009
  }
18010
+ // fallback
18003
18011
  return next(defaultResolve(`${packageLocation}${appendix}`), context);
18004
18012
  }
18005
18013
  });