Skip to content

Commit

Permalink
fix: resolutions support scope packages (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored Jan 5, 2019
1 parent bf02725 commit 8f558a5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
27 changes: 21 additions & 6 deletions lib/resolution.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,39 @@ module.exports = (pkg, options) => {
// less: [
// "**", "^1"
// ],
// vary: [
// "@koa/cors", "1.0.0"
// ]
// }
for (const path in resolutions) {
const sections = path.split('/');
// debug => **/debug
if (sections.length === 1) sections.unshift('**');
// check
for (const section of sections) {
let scope = '';
const packages = [];
// 1. check package name
// 2. process package with scope like `@koa/cors`
for (let section of sections) {
if (section.startsWith('@') && !scope) {
scope = section;
continue;
}
if (scope) {
section = `${scope}/${section}`;
scope = '';
}
if (section !== '**' && !npa(section).name) {
throw new Error(`[resolutions] resolution package ${path} format error`);
}
packages.push(section);
}
const endpoint = sections.pop();
// debug => **/debug
if (packages.length === 1) packages.unshift('**');
const endpoint = packages.pop();
if (endpoint === '**') throw new Error(`[resolutions] resolution package ${path} format error`);

const version = resolutions[path];

if (!resolutionMap.has(endpoint)) resolutionMap.set(endpoint, []);
resolutionMap.get(endpoint).push([ sections.join('/'), version ]);
resolutionMap.get(endpoint).push([ packages.join('/'), version ]);
}

return (pkg, ancestors) => {
Expand Down
6 changes: 4 additions & 2 deletions test/fixtures/resolutions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
"dependencies": {
"koa": "2",
"debug": "2.0.0",
"cookies": "0.7.1"
"cookies": "0.7.1",
"@koa/cors": "2.2.3"
},
"resolutions": {
"debug": "1.0.0",
"**/http-errors": "1.0.0",
"koa/cookies": "0.7.0"
"koa/cookies": "0.7.0",
"**/@koa/cors/vary": "1.0.0"
}
}
3 changes: 2 additions & 1 deletion test/resolutions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@ describe('test/seperate-dependencies.test.js', () => {
yield coffee.fork(bin, {
cwd: root,
})
.debug()
// .debug()
.expect('code', 0)
.end();

yield checkPkg('debug', '2.0.0');
yield checkPkg('koa/node_modules/debug', '1.0.0');
yield checkPkg('koa/node_modules/cookies', '0.7.0');
yield checkPkg('cookies', '0.7.1');
yield checkPkg('@koa/cors/node_modules/vary', '1.0.0');

});
});
Expand Down

0 comments on commit 8f558a5

Please sign in to comment.