Skip to content

Commit

Permalink
feat: support mapping package version to other
Browse files Browse the repository at this point in the history
--package-version-mapping-file={mapping.json}

format:

```json
{
  "pedding": {
    "1.0.0": { "version": "1.1.0", "reason": "Security problem on 1.0.0" }
  }
}
```
  • Loading branch information
fengmk2 committed Dec 18, 2017
1 parent c497252 commit 09a59f6
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
15 changes: 15 additions & 0 deletions bin/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ const argv = parseArgs(orignalArgv, {
// {"http://a.com":"http://b.com"}
'tarball-url-mapping',
'proxy',
// --package-version-mapping-file=PWD/node_modules/.package-version-mapping.json
// {
// "pedding": {
// "1.0.0": { "version": "1.1.0", "reason": "Security problem on 1.0.0" }
// }
// }
'package-version-mapping-file',
],
boolean: [
'version',
Expand Down Expand Up @@ -234,6 +241,14 @@ co(function* () {
};
}

if (argv['package-version-mapping-file']) {
const packageVersionMapping = yield utils.readJSON(argv['package-version-mapping-file']);
config.autoFixVersion = function autoFixVersion(name, version) {
const fixVersions = packageVersionMapping[name];
return fixVersions && fixVersions[version] || null;
};
}

// -g install to npm's global prefix
if (argv.global) {
// support custom prefix for global install
Expand Down
8 changes: 8 additions & 0 deletions lib/download/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ function* resolve(pkg, options) {
throw new Error(`[${pkg.displayName}] Can\'t find package ${pkg.name}@${pkg.rawSpec}`);
}

if (options.autoFixVersion) {
const fixVersion = options.autoFixVersion(pkg.name, realPkgVersion);
if (fixVersion) {
options.console.warn(`[${pkg.name}@${realPkgVersion}] use ${pkg.name}@${chalk.green(fixVersion.version)} instead, reason: ${chalk.yellow(fixVersion.reason)}`);
realPkgVersion = fixVersion.version;
}
}

const realPkg = packageMeta.versions[realPkgVersion];
if (!realPkg) {
throw new Error(`[${pkg.displayName}] Can\'t find package ${pkg.name}\'s version: ${realPkgVersion}`);
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/package-version-mapping-file-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "package-version-mapping-file",
"version": "1.0.0",
"dependencies": {
"pedding": "1.0.0"
}
}
5 changes: 5 additions & 0 deletions test/fixtures/package-version-mapping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"pedding": {
"1.0.0": { "version": "1.1.0", "reason": "Security problem on 1.0.0" }
}
}
86 changes: 86 additions & 0 deletions test/package-version-mapping-file.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
'use strict';

const coffee = require('coffee');
const rimraf = require('rimraf');
const mkdirp = require('mkdirp');
const path = require('path');

describe('package-version-mapping-file.test.js', () => {
const tmp = path.join(__dirname, 'fixtures', 'tmp');
const demo = path.join(__dirname, 'fixtures', 'package-version-mapping-file-app');
const mappingFile = path.join(__dirname, 'fixtures', 'package-version-mapping.json');
const bin = path.join(__dirname, '../bin/install.js');
const update = path.join(__dirname, '../bin/update.js');

function cleanup() {
rimraf.sync(tmp);
rimraf.sync(path.join(demo, 'node_modules'));
}

beforeEach(() => {
cleanup();
mkdirp.sync(tmp);
});
afterEach(cleanup);

it('should use 1.1.0 instead of 1.0.0', done => {
coffee.fork(bin, [
'pedding@1.0.0',
'--package-version-mapping-file=' + mappingFile,
'-d',
'--no-cache',
], { cwd: tmp })
.debug()
.expect('code', 0)
.expect('stdout', /pedding@1\.1\.0@pedding/)
.end(done);
});

it('should support on install and update', function* () {
yield coffee.fork(bin, [
'--package-version-mapping-file=' + mappingFile,
'-d',
'--no-cache',
], { cwd: demo })
.debug()
.expect('code', 0)
.expect('stdout', /pedding@1\.1\.0@pedding/)
.end();

yield coffee.fork(update, [
'--package-version-mapping-file=' + mappingFile,
'-d',
'--no-cache',
], { cwd: demo })
.debug()
.expect('code', 0)
.expect('stdout', /pedding@1\.1\.0@pedding/)
.end();
});

it('should not match version', done => {
coffee.fork(bin, [
'pedding@0.0.1',
'--package-version-mapping-file=' + mappingFile,
'-d',
'--no-cache',
], { cwd: tmp })
.debug()
.expect('code', 0)
.expect('stdout', /pedding@0\.0\.1@pedding/)
.end(done);
});

it('should not match name', done => {
coffee.fork(bin, [
'mm',
'--package-version-mapping-file=' + mappingFile,
'-d',
'--no-cache',
], { cwd: tmp })
.debug()
.expect('code', 0)
.expect('stdout', /mm@\* installed/)
.end(done);
});
});

0 comments on commit 09a59f6

Please sign in to comment.