-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add npm package-lock.json support (#462)
Co-authored-by: tianding.wk <tianding.wk@alipay.com>
- Loading branch information
1 parent
b55568e
commit 2c24360
Showing
7 changed files
with
285 additions
and
4 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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
'use strict'; | ||
|
||
const path = require('node:path'); | ||
const assert = require('node:assert'); | ||
const dependencies = require('./dependencies'); | ||
|
||
const NODE_MODULES_DIR = 'node_modules/'; | ||
|
||
/** | ||
* The lockfileConverter converts a npm package-lockfile.json to npminstall .dependencies-tree.json. | ||
* Only lockfileVersion >= 2 is supported. | ||
* The `.dependencies-tree.json` does not recognize a npm-workspaces package, so we don't need to handle it neither for now. | ||
* @param {Object} lockfile package-lock.json data | ||
* @param {Object} options installation options | ||
* @param {Nested} nested Nested | ||
*/ | ||
exports.lockfileConverter = function lockfileConverter(lockfile, options, nested) { | ||
assert(lockfile.lockfileVersion >= 2, 'Only lockfileVersion >=2 is supported.'); | ||
|
||
const tree = {}; | ||
|
||
const packages = lockfile.packages; | ||
for (const pkgPath in packages) { | ||
/** | ||
* the lockfile contains all the deps so there's no need to be deps type sensitive. | ||
*/ | ||
const deps = dependencies(packages[pkgPath], options, nested); | ||
const allMap = deps.allMap; | ||
for (const key in allMap) { | ||
const mani = exports.nodeModulesPath(pkgPath, key, packages); | ||
const dist = { | ||
checkSSRI: true, | ||
integrity: mani.integrity, | ||
tarball: mani.resolved, | ||
}; | ||
// we need to remove the integrity and resolved field from the mani | ||
// but the mani should be left intact | ||
const maniClone = Object.assign({}, mani); | ||
delete maniClone.integrity; | ||
delete maniClone.resolved; | ||
tree[`${key}@${allMap[key]}`] = { | ||
name: key, | ||
...maniClone, | ||
dist, | ||
_id: `${key}@${mani.version}`, | ||
}; | ||
} | ||
} | ||
|
||
return tree; | ||
}; | ||
|
||
/** | ||
* find the matched version of current semver based on nodejs modules resolution algorithm | ||
* we need check the current directory and ancestors directories to find the matched version of lodash.has | ||
* e.g. | ||
* "": { | ||
* "dependencies": { | ||
* "lodash.has": "4", | ||
* "a": "latest" | ||
* }, | ||
* 'node_modules/lodash.has': { | ||
* "version": '4.4.0' | ||
* }, | ||
* 'node_modules/a': { | ||
* 'dependencies': { | ||
* "lodash.has": "3" | ||
* } | ||
* }, | ||
* 'node_modules/a/node_modules/lodash.has': { | ||
* "version": "3.0.0" | ||
* }, | ||
* } | ||
* | ||
* the root lodash.has@4 should matches node_modules/lodash.has | ||
* | ||
* @param {string} currentPath the current pakcage.json path | ||
* @param {string} name the dependency name of current package.json | ||
* @param {Object} packages the lockfile packages | ||
*/ | ||
exports.nodeModulesPath = function nodeModulesPath(currentPath, name, packages) { | ||
const dirs = currentPath.split(NODE_MODULES_DIR); | ||
dirs.push(name); | ||
|
||
do { | ||
const dir = path.normalize('.' + dirs.join('/' + NODE_MODULES_DIR)); | ||
const pkg = packages[dir]; | ||
if (pkg) { | ||
return pkg; | ||
} | ||
|
||
dirs.splice(dirs.length - 2, 1); | ||
} while (dirs.length > 1); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,9 @@ | ||
{ | ||
"dependencies": { | ||
"lodash.has": "4.0.0", | ||
"lodash.get": "3", | ||
"lodash._baseget": "3.7.1", | ||
"lodash._baseslice": "^3.0.1", | ||
"lodash.has3": "npm:lodash.has@3" | ||
} | ||
} |
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,48 @@ | ||
const coffee = require('coffee'); | ||
const path = require('node:path'); | ||
const assert = require('node:assert'); | ||
const fs = require('node:fs/promises'); | ||
const { lockfileConverter } = require('../lib/lockfile_resolver'); | ||
const Nested = require('../lib/nested'); | ||
const helper = require('./helper'); | ||
|
||
describe('test/install-with-lockfile.test.js', () => { | ||
const cwd = helper.fixtures('lockfile'); | ||
const lockfile = require(path.join(cwd, 'package-lock.json')); | ||
const nested = new Nested([]); | ||
const cleanup = helper.cleanup(cwd); | ||
|
||
beforeEach(cleanup); | ||
afterEach(cleanup); | ||
|
||
// the Windows path sucks, shamefully skip these tests | ||
if (process.platform !== 'win32') { | ||
it('should install successfully', async () => { | ||
await coffee.fork( | ||
helper.npminstall, | ||
[ | ||
'--lockfile-path', | ||
path.join(cwd, 'package-lock.json'), | ||
], { cwd }) | ||
.debug() | ||
.expect('code', 0) | ||
.end(); | ||
assert.strictEqual( | ||
await fs.readlink(path.join(cwd, 'node_modules', 'lodash.has3'), 'utf8'), | ||
'.store/lodash.has@3.2.1/node_modules/lodash.has' | ||
); | ||
assert.strictEqual( | ||
await fs.readlink(path.join(cwd, 'node_modules', 'lodash.has'), 'utf8'), | ||
'.store/lodash.has@4.0.0/node_modules/lodash.has' | ||
); | ||
}); | ||
|
||
it('should convert package-lock.json to .dependencies-tree.json successfully', () => { | ||
const dependenciesTree = lockfileConverter(lockfile, { | ||
ignoreOptionalDependencies: true, | ||
}, nested); | ||
|
||
assert.strictEqual(Object.keys(dependenciesTree).length, 12); | ||
}); | ||
} | ||
}); |