-
Notifications
You must be signed in to change notification settings - Fork 30.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
672 additions
and
281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,64 @@ | ||
'use strict'; | ||
|
||
const { | ||
StringPrototypeEndsWith, | ||
} = primordials; | ||
const { URL, fileURLToPath } = require('internal/url'); | ||
const packageJsonReader = require('internal/modules/package_json_reader'); | ||
|
||
/** | ||
* @typedef {object} PackageConfig | ||
* @property {string} pjsonPath - The path to the package.json file. | ||
* @property {boolean} exists - Whether the package.json file exists. | ||
* @property {'none' | 'commonjs' | 'module'} type - The type of the package. | ||
* @property {string} [name] - The name of the package. | ||
* @property {string} [main] - The main entry point of the package. | ||
* @property {PackageTarget} [exports] - The exports configuration of the package. | ||
* @property {Record<string, string | Record<string, string>>} [imports] - The imports configuration of the package. | ||
*/ | ||
/** | ||
* @typedef {string | string[] | Record<string, string | Record<string, string>>} PackageTarget | ||
*/ | ||
const { ArrayIsArray } = primordials; | ||
const { toNamespacedPath } = require('path'); | ||
const modulesBinding = internalBinding('modules'); | ||
|
||
/** | ||
* Returns the package configuration for the given resolved URL. | ||
* @param {URL | string} resolved - The resolved URL. | ||
* @returns {PackageConfig} - The package configuration. | ||
* @returns {import('typings/internalBinding/modules').PackageConfig} - The package configuration. | ||
*/ | ||
function getPackageScopeConfig(resolved) { | ||
let packageJSONUrl = new URL('./package.json', resolved); | ||
while (true) { | ||
const packageJSONPath = packageJSONUrl.pathname; | ||
if (StringPrototypeEndsWith(packageJSONPath, 'node_modules/package.json')) { | ||
break; | ||
} | ||
const packageConfig = packageJsonReader.read(fileURLToPath(packageJSONUrl), { | ||
__proto__: null, | ||
specifier: resolved, | ||
isESM: true, | ||
}); | ||
if (packageConfig.exists) { | ||
return packageConfig; | ||
} | ||
|
||
const lastPackageJSONUrl = packageJSONUrl; | ||
packageJSONUrl = new URL('../package.json', packageJSONUrl); | ||
|
||
// Terminates at root where ../package.json equals ../../package.json | ||
// (can't just check "/package.json" for Windows support). | ||
if (packageJSONUrl.pathname === lastPackageJSONUrl.pathname) { | ||
break; | ||
} | ||
} | ||
const packageJSONPath = fileURLToPath(packageJSONUrl); | ||
return { | ||
const packageScopeConfig = modulesBinding.getPackageScopeConfig(toNamespacedPath(`${resolved}`)); | ||
const response = { | ||
__proto__: null, | ||
pjsonPath: packageJSONPath, | ||
pjsonPath: undefined, | ||
exists: false, | ||
main: undefined, | ||
name: undefined, | ||
type: 'none', | ||
exports: undefined, | ||
imports: undefined, | ||
}; | ||
if (ArrayIsArray(packageScopeConfig)) { | ||
const { | ||
0: name, | ||
1: main, | ||
2: type, | ||
3: imports, | ||
4: exports, | ||
5: _manifest, // eslint-disable-line no-unused-vars | ||
6: filePath, | ||
} = packageScopeConfig; | ||
response.name = name; | ||
response.main = main; | ||
response.type = type; | ||
response.imports = imports; | ||
response.exports = exports; | ||
response.exists = true; | ||
response.pjsonPath = filePath; | ||
} else { | ||
// This means that the response is a string | ||
// and it is the path to the package.json file | ||
response.pjsonPath = packageScopeConfig; | ||
} | ||
|
||
return response; | ||
} | ||
|
||
/** | ||
* Returns the package type for a given URL. | ||
* @param {URL} url - The URL to get the package type for. | ||
*/ | ||
function getPackageType(url) { | ||
// TODO(@anonrig): Write a C++ function that returns only "type". | ||
const packageConfig = getPackageScopeConfig(url); | ||
return packageConfig.type; | ||
} | ||
|
||
|
||
module.exports = { | ||
getPackageScopeConfig, | ||
getPackageType, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.