-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This brings in parity with `read-package-json-fast`, which is a normalization method intended for parsing package.json files inside of an existing node_modules. Tests were copied straight from that package to ensure functionality was compatible. The only tests that weren't copied were the ones testing down into the bin normalization. That is delegated to a subdependency so these tests only ensure that *some* normalization is happening. Eventually as we consolidate our package.json reading libs, bin normalization can live in this package and be tested here. Finally, the errors that this package was throwing now include the metadata from the original errors (such as code) instead of making new errors with no context.
- Loading branch information
Showing
5 changed files
with
436 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
const normalizePackageBin = require('npm-normalize-package-bin') | ||
|
||
const normalize = async (pkg) => { | ||
const data = pkg.content | ||
|
||
// remove _attributes | ||
for (const key in data) { | ||
if (key.startsWith('_')) { | ||
delete pkg.content[key] | ||
} | ||
} | ||
|
||
// _id | ||
if (data.name && data.version) { | ||
data._id = `${data.name}@${data.version}` | ||
} | ||
|
||
// bundleDependencies | ||
if (data.bundleDependencies === undefined && data.bundledDependencies !== undefined) { | ||
data.bundleDependencies = data.bundledDependencies | ||
} | ||
delete data.bundledDependencies | ||
const bd = data.bundleDependencies | ||
if (bd === true) { | ||
data.bundleDependencies = Object.keys(data.dependencies || {}) | ||
} else if (bd && typeof bd === 'object') { | ||
if (!Array.isArray(bd)) { | ||
data.bundleDependencies = Object.keys(bd) | ||
} | ||
} else { | ||
data.bundleDependencies = [] | ||
} | ||
|
||
// it was once common practice to list deps both in optionalDependencies and | ||
// in dependencies, to support npm versions that did not know about | ||
// optionalDependencies. This is no longer a relevant need, so duplicating | ||
// the deps in two places is unnecessary and excessive. | ||
if (data.dependencies && | ||
data.optionalDependencies && typeof data.optionalDependencies === 'object') { | ||
for (const name in data.optionalDependencies) { | ||
delete data.dependencies[name] | ||
} | ||
if (!Object.keys(data.dependencies).length) { | ||
delete data.dependencies | ||
} | ||
} | ||
|
||
// scripts | ||
if (typeof data.scripts === 'object') { | ||
for (const name in data.scripts) { | ||
if (typeof data.scripts[name] !== 'string') { | ||
delete data.scripts[name] | ||
} | ||
} | ||
} else { | ||
delete data.scripts | ||
} | ||
|
||
// funding | ||
if (data.funding && typeof data.funding === 'string') { | ||
data.funding = { url: data.funding } | ||
} | ||
|
||
// bin | ||
normalizePackageBin(data) | ||
} | ||
|
||
module.exports = normalize |
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.