Skip to content

Commit

Permalink
feat: support auto fix bug dependencies (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored May 17, 2018
1 parent 147c925 commit b140b50
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ env:
- CC=clang CXX=clang++ npm_config_clang=1 npm_registry=https://registry.npmjs.org
language: node_js
node_js:
- '9'
- '10'
- '8'
- '6'
- '4'
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
This software is licensed under the MIT License.

Copyright (c) 2016 - 2017 cnpm and other contributors
Copyright (c) 2016 - present cnpm and other contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ environment:
- nodejs_version: "4"
- nodejs_version: "6"
- nodejs_version: "8"
- nodejs_version: "9"
- nodejs_version: "10"

# Install scripts. (runs after repo cloning)
install:
Expand Down
18 changes: 16 additions & 2 deletions lib/download/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ function* resolve(pkg, options) {

const distTags = packageMeta['dist-tags'];
let realPkgVersion = utils.findMaxSatisfyingVersion(spec, distTags, packageMeta.allVersions);
let fixDependencies;

if (!realPkgVersion) {
throw new Error(`[${pkg.displayName}] Can\'t find package ${pkg.name}@${pkg.rawSpec}`);
Expand All @@ -81,8 +82,14 @@ function* resolve(pkg, options) {
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;
if (fixVersion.version && fixVersion.version !== realPkgVersion) {
options.console.warn(`[${pkg.name}@${realPkgVersion}] use ${pkg.name}@${chalk.green(fixVersion.version)} instead, reason: ${chalk.yellow(fixVersion.reason)}`);
realPkgVersion = fixVersion.version;
}
if (fixVersion.dependencies) {
options.console.warn(`[${pkg.name}@${realPkgVersion}] use dependencies: ${chalk.green(JSON.stringify(fixVersion.dependencies))} instead, reason: ${chalk.yellow(fixVersion.reason)}`);
fixDependencies = fixVersion.dependencies;
}
}
}

Expand All @@ -91,6 +98,10 @@ function* resolve(pkg, options) {
throw new Error(`[${pkg.displayName}] Can\'t find package ${pkg.name}\'s version: ${realPkgVersion}`);
}

if (fixDependencies) {
realPkg.__fixDependencies = fixDependencies;
}

debug('[%s@%s] spec: %s, real version: %s, dist-tags: %j',
pkg.name, pkg.rawSpec, pkg.spec, realPkg.version, distTags);

Expand Down Expand Up @@ -191,6 +202,9 @@ function* download(pkg, options) {
// read package.json to merge into realPkg
const fullMeta = yield utils.readPackageJSON(ungzipDir);
Object.assign(pkg, fullMeta);
if (pkg.__fixDependencies) {
pkg.dependencies = Object.assign({}, pkg.dependencies, pkg.__fixDependencies);
}

yield fs.writeFile(donefile, Date());

Expand Down
23 changes: 23 additions & 0 deletions test/fix-bug-versions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ const coffee = require('coffee');
const rimraf = require('rimraf');
const mkdirp = require('mkdirp');
const path = require('path');
const assert = require('assert');
const fs = require('fs');

describe('fix-bug-versions.test.js', () => {
const tmp = path.join(__dirname, 'fixtures', 'tmp');
const demo = path.join(__dirname, 'fixtures', 'fix-bug-versions-app');
const bin = path.join(__dirname, '../bin/install.js');
const update = path.join(__dirname, '../bin/update.js');

function getPkg(subPath) {
return JSON.parse(fs.readFileSync(path.join(tmp, subPath)));
}

function cleanup() {
rimraf.sync(tmp);
rimraf.sync(path.join(demo, 'node_modules'));
Expand All @@ -35,6 +41,23 @@ describe('fix-bug-versions.test.js', () => {
.end(done);
});

it('should use fix dependencies instead', function* () {
yield coffee.fork(bin, [
'accord@0.28.0',
'-d',
'--fix-bug-versions',
'--no-cache',
], { cwd: tmp })
.debug()
.expect('code', 0)
.expect('stderr', /\[accord@0\.28\.0\] use dependencies: {\"less\":\"\^2.7.0\"} instead, reason:/)
.end();

assert(getPkg('node_modules/accord/package.json').version === '0.28.0');
assert(getPkg('node_modules/accord/node_modules/less/package.json'));
assert(getPkg('node_modules/accord/node_modules/less/package.json').version.split('.')[0] === '2');
});

it('should support on install and update', function* () {
yield coffee.fork(bin, [
'-d',
Expand Down
2 changes: 1 addition & 1 deletion test/fmode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ if (process.platform !== 'win32') {

beforeEach(() => {
cleanup();
fs.mkdir(homedir);
fs.mkdirSync(homedir);
});
afterEach(cleanup);

Expand Down
2 changes: 1 addition & 1 deletion test/install-cache-strict.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('test/install-cache-strict.test.js', () => {

beforeEach(() => {
cleanup();
fs.mkdir(homedir);
fs.mkdirSync(homedir);
});
afterEach(cleanup);

Expand Down
2 changes: 1 addition & 1 deletion test/install-save-folder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('test/install-save-folder.test.js', () => {

beforeEach(() => {
cleanup();
fs.mkdir(root);
fs.mkdirSync(root);
});
afterEach(cleanup);

Expand Down
2 changes: 1 addition & 1 deletion test/next.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const npminstall = require('./npminstall');
const exec = require('mz/child_process').exec;

// make sure https://github.com/cnpm/cnpm/issues/194 work!
describe('test/next.test.js', () => {
describe.skip('test/next.test.js', () => {
const tmp = path.join(__dirname, '.tmp', 'next');

function cleanup() {
Expand Down
2 changes: 1 addition & 1 deletion test/node-pre-gyp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('test/node-pre-gyp.test.js', () => {
yield npminstall({
root: tmp,
pkgs: [
{ name: 'sqlite3', version: '3' },
{ name: 'sqlite3', version: '4' },
],
production: true,
cacheDir: '',
Expand Down

0 comments on commit b140b50

Please sign in to comment.