-
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: support selective version resolutions (#291)
- Loading branch information
1 parent
d4937f0
commit 9aa0a65
Showing
10 changed files
with
195 additions
and
8 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,66 @@ | ||
'use strict'; | ||
|
||
const minimatch = require('minimatch'); | ||
const npa = require('npm-package-arg'); | ||
const utils = require('./utils'); | ||
const chalk = require('chalk'); | ||
|
||
module.exports = (pkg, options) => { | ||
const resolutions = pkg && pkg.resolutions || {}; | ||
const resolutionMap = new Map(); | ||
|
||
// parse resolutions, generate resolutionMap: | ||
// { | ||
// debug: [ | ||
// "koa/accept", "1.0.0", | ||
// "send": "2.0.0" | ||
// ], | ||
// less: [ | ||
// "**", "^1" | ||
// ], | ||
// } | ||
for (const path in resolutions) { | ||
const sections = path.split('/'); | ||
// debug => **/debug | ||
if (sections.length === 1) sections.unshift('**'); | ||
// check | ||
for (const section of sections) { | ||
if (section !== '**' && !npa(section).name) { | ||
throw new Error(`[resolutions] resolution package ${path} format error`); | ||
} | ||
} | ||
const endpoint = sections.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 ]); | ||
} | ||
|
||
return (pkg, ancestors) => { | ||
// only work for nested dependencies | ||
if (!ancestors.length) return pkg; | ||
// check pkg.name first to reduce calculate | ||
const resolutions = resolutionMap.get(pkg.name); | ||
if (!resolutions) return pkg; | ||
|
||
const ancestorPath = ancestors.map(ancestor => ancestor.name).join('/'); | ||
for (const resolution of resolutions) { | ||
const path = resolution[0]; | ||
const version = resolution[1]; | ||
if (minimatch(ancestorPath, path)) { | ||
options.pendingMessages.push([ | ||
'warn', | ||
'%s %s override by %s', | ||
chalk.yellow('resolutions'), | ||
chalk.gray(utils.getDisplayName(pkg, ancestors)), | ||
chalk.magenta(`${path}/${pkg.name}@${version}`), | ||
]); | ||
return Object.assign({}, pkg, { version }); | ||
} | ||
} | ||
|
||
return pkg; | ||
}; | ||
}; |
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,9 @@ | ||
{ | ||
"name": "resolution-error-endpoint", | ||
"dependencies": { | ||
"koa": "^1" | ||
}, | ||
"resolutions": { | ||
"foo/bar-*": "1.0.0" | ||
} | ||
} |
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 @@ | ||
{ | ||
"name": "resolution-error-endpoint", | ||
"dependencies": { | ||
"koa": "^1" | ||
}, | ||
"resolutions": { | ||
"foo/**": "1.0.0" | ||
} | ||
} |
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,13 @@ | ||
{ | ||
"name": "resolutions", | ||
"dependencies": { | ||
"koa": "2", | ||
"debug": "2.0.0", | ||
"cookies": "0.7.1" | ||
}, | ||
"resolutions": { | ||
"debug": "1.0.0", | ||
"**/http-errors": "1.0.0", | ||
"koa/cookies": "0.7.0" | ||
} | ||
} |
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,80 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const rimraf = require('rimraf'); | ||
const path = require('path'); | ||
const readJSON = require('../lib/utils').readJSON; | ||
const coffee = require('coffee'); | ||
|
||
const bin = path.join(__dirname, '../bin/install.js'); | ||
|
||
let root; | ||
function cleanup() { | ||
rimraf.sync(path.join(root, 'node_modules')); | ||
} | ||
|
||
function* checkPkg(name, version) { | ||
const pkg = yield readJSON(path.join(root, 'node_modules', name, 'package.json')); | ||
assert.equal(pkg.version, version); | ||
} | ||
|
||
describe('test/seperate-dependencies.test.js', () => { | ||
describe('resolutions-error-format', () => { | ||
before(() => { | ||
root = path.join(__dirname, 'fixtures', 'resolutions-error-format'); | ||
cleanup(); | ||
}); | ||
afterEach(cleanup); | ||
|
||
it('should install error', () => { | ||
return coffee.fork(bin, { | ||
cwd: root, | ||
}) | ||
// .debug() | ||
.expect('code', 1) | ||
.expect('stderr', /resolution package foo\/\*\* format error/) | ||
.end(); | ||
}); | ||
}); | ||
|
||
describe('resolutions-error-endpoint', () => { | ||
before(() => { | ||
root = path.join(__dirname, 'fixtures', 'resolutions-error-endpoint'); | ||
cleanup(); | ||
}); | ||
afterEach(cleanup); | ||
|
||
it('should install error', () => { | ||
return coffee.fork(bin, { | ||
cwd: root, | ||
}) | ||
// .debug() | ||
.expect('code', 1) | ||
.expect('stderr', /resolution package foo\/bar-\* format error/) | ||
.end(); | ||
}); | ||
}); | ||
|
||
describe('resolutions', () => { | ||
before(() => { | ||
root = path.join(__dirname, 'fixtures', 'resolutions'); | ||
cleanup(); | ||
}); | ||
afterEach(cleanup); | ||
|
||
it('should work', function* () { | ||
yield coffee.fork(bin, { | ||
cwd: root, | ||
}) | ||
.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'); | ||
|
||
}); | ||
}); | ||
}); |