Skip to content

Commit

Permalink
build: install nyc on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Oct 28, 2018
1 parent 0420029 commit 37fbbdd
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 10 deletions.
10 changes: 4 additions & 6 deletions .travis.yml
Expand Up @@ -56,8 +56,7 @@ before_install:
- "npm config set shrinkwrap false"

# Setup Node.js version-specific dependencies
- "test $TRAVIS_NODE_VERSION != '0.6' || npm rm --save-dev nyc"
- "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev nyc"
- "node tool/install-nyc.js --nyc-optional"
- "test $(echo $TRAVIS_NODE_VERSION | cut -d. -f1) -ge 4 || npm rm --save-dev eslint"

# Update Node.js modules
Expand All @@ -78,10 +77,9 @@ before_script:
- "docker exec mysql mysql -e 'select version()'"

script:
# Run test script, depending on nyc install
- "test ! -z $(npm -ps ls nyc) || npm test"
- "test -z $(npm -ps ls nyc) || npm run-script test-ci"
- "test -z $(npm -ps ls eslint) || npm run-script lint"
# Run test script
- "npm run-script test-ci"
- "test -z $(npm -ps ls eslint) || npm run-script lint"

after_script:
- "test -d .nyc_output && npm install coveralls@2 && nyc report --reporter=text-lcov | coveralls"
2 changes: 1 addition & 1 deletion appveyor.yml
Expand Up @@ -32,7 +32,7 @@ install:
catch { Update-NodeJsInstallation (Get-NodeJsLatestBuild $env:nodejs_version) }
- npm config set shrinkwrap false
- if "%nodejs_version%" equ "0.8" npm config set strict-ssl false
- npm rm --silent --save-dev eslint nyc
- npm rm --silent --save-dev eslint
- if exist node_modules npm prune
- if exist node_modules npm rebuild
- npm install
Expand Down
5 changes: 2 additions & 3 deletions package.json
Expand Up @@ -21,7 +21,6 @@
"devDependencies": {
"after": "0.8.2",
"eslint": "4.19.1",
"nyc": "10.3.2",
"seedrandom": "2.4.4",
"timezone-mock": "0.0.7",
"urun": "0.0.8",
Expand All @@ -40,8 +39,8 @@
"scripts": {
"lint": "eslint .",
"test": "node test/run.js",
"test-ci": "nyc --reporter=text npm test",
"test-cov": "nyc --reporter=html --reporter=text npm test",
"test-ci": "node tool/install-nyc.js --nyc-optional --reporter=text -- npm test",
"test-cov": "node tool/install-nyc.js --reporter=html --reporter=text -- npm test",
"version": "node tool/version-changes.js && git add Changes.md"
}
}
96 changes: 96 additions & 0 deletions tool/install-nyc.js
@@ -0,0 +1,96 @@
var path = require('path');
var spawn = require('child_process').spawn;

process.nextTick(run);

function installNpmModule(name, version, callback) {
if (getPackageVersion(name) === version) {
callback();
return;
}

var spec = name + '@' + version;
var args = ['install', '--silent', '--no-save', spec];
var child = spawn('npm', args, {
cwd : path.join(__dirname, '..'),
shell : true
});

child.stderr.resume();
child.stdout.resume();

child.on('exit', function (code) {
var err = null;

if (code !== 0) {
err = new Error('npm install ' + spec + ' failed with exit code ' + code);
}

callback(err);
});
}

function getNycVersion() {
var nodeVersion = process.version.replace(/^v/, '').split('.')
.map(function (s) { return Number(s); });

if (nodeVersion[0] === 0 && nodeVersion[1] < 10) {
return undefined;
} else if (nodeVersion[0] < 4) {
return '10.3.2';
} else if (nodeVersion[0] < 6) {
return '11.9.0';
} else {
return '13.1.0';
}
}

function getPackageVersion(name) {
try {
return require(name + '/package').version;
} catch (e) {
return undefined;
}
}

function run() {
var args = process.argv.slice(2);
var cmd = null;
var divider = args.indexOf('--');
var version = getNycVersion();

if (divider !== -1) {
cmd = args.slice(divider + 1);
args = args.slice(0, divider);
}

if (!version && args.indexOf('--nyc-optional') === -1) {
console.error('nyc does not support current Node.js version');
process.exit(1);
} else if (version) {
installNpmModule('nyc', version, function (err) {
if (err) {
console.error(err.message);
process.exit(1);
} else if (cmd) {
runCmd('nyc', args.concat(cmd));
}
});
} else if (cmd) {
runCmd(cmd[0], cmd.slice(1));
}
}

function runCmd(cmd, args) {
var child = spawn(cmd, args, {
cwd : path.join(__dirname, '..'),
shell : true
});

child.stderr.pipe(process.stderr, {end: false});
child.stdout.pipe(process.stdout, {end: false});

child.on('exit', function (code) {
process.exit(code);
});
}

0 comments on commit 37fbbdd

Please sign in to comment.