Skip to content

Commit

Permalink
feat: add resolutions alias support (#354)
Browse files Browse the repository at this point in the history
* feat: add resolutions alias support

Co-authored-by: killa <killa123@126.com>
  • Loading branch information
gemwuu and killagu authored Sep 24, 2021
1 parent e8bd485 commit 11589da
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ both the same version: 1.1.2

support [selective version resolutions](https://yarnpkg.com/en/docs/selective-version-resolutions) like yarn. which lets you define custom package versions inside your dependencies through the resolutions field in your `package.json` file.

resolutions also supports [npm alias)(https://docs.npmjs.com/cli/v7/commands/npm-install). It's a workaround feature to fix some archived/inactive/ package by uploading your own bug-fixed version to npm registry.

see use case at [unittest package.json](./test/fixtures/resolutions-alias/package.json).

## Benchmarks

https://github.com/cnpm/npminstall-benchmark
Expand Down
2 changes: 1 addition & 1 deletion lib/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async function _install(parentDir, pkg, ancestors, options, context) {
pkg.version = '*';
}

pkg = options.resolution(pkg, ancestors);
pkg = options.resolution(pkg, ancestors, context.nested);

debug('[%s/%s] install %s@%s in %s',
options.progresses.finishedInstallTasks,
Expand Down
20 changes: 19 additions & 1 deletion lib/resolution.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const minimatch = require('minimatch');
const utils = require('./utils');
const chalk = require('chalk');
const { parsePackageName } = require('./alias');
const npa = require('./npa');

// https://github.com/yarnpkg/rfcs/blob/master/implemented/0000-selective-versions-resolutions.md#package-designation
// https://github.com/yarnpkg/yarn/blob/3119382885/src/util/parse-package-path.js#L10
Expand Down Expand Up @@ -55,7 +57,7 @@ module.exports = (pkg, options) => {
resolutionMap.get(endpoint).push([ packages.join('/'), version ]);
}

return (pkg, ancestors) => {
return (pkg, ancestors, nested) => {
// only work for nested dependencies
if (!ancestors.length) return pkg;
// check pkg.name first to reduce calculate
Expand All @@ -74,6 +76,22 @@ module.exports = (pkg, options) => {
chalk.gray(utils.getDisplayName(pkg, ancestors)),
chalk.magenta(`${path}/${pkg.name}@${version}`),
]);
// alias(npm:lodash@^1) support
const [ aliasPackageName, realPackageName ] = parsePackageName(`${pkg.name}@${version}`, nested);

if (aliasPackageName) {
const {
name,
fetchSpec,
} = npa(realPackageName, { nested });

return Object.assign({}, pkg, {
alias: aliasPackageName,
version: fetchSpec,
name,
});
}

return Object.assign({}, pkg, { version });
}
}
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/resolutions-alias/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"dependencies": {
"object-pipeline": "^1"
},
"resolutions": {
"lodash.has": "npm:lodash.get@4.4.2"
}
}
22 changes: 22 additions & 0 deletions test/resolutions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,26 @@ describe('test/resolutions.test.js', () => {
await checkPkg('@koa/cors/node_modules/vary', '1.0.0');
});
});

describe('resolutions alias', () => {
before(() => {
root = helper.fixtures('resolutions-alias');
cleanup();
});

afterEach(cleanup);

it('should work', async () => {
await coffee.fork(helper.npminstall, {
cwd: root,
})
.debug()
.expect('code', 0)
.end();

const pkgJSON = require(path.join(root, 'node_modules', 'object-pipeline/node_modules/lodash.has/package.json'));
assert.strictEqual(pkgJSON.name, 'lodash.get');
assert.strictEqual(pkgJSON.version, '4.4.2');
});
});
});

0 comments on commit 11589da

Please sign in to comment.