Skip to content

Commit

Permalink
build: simplify & speed up logic in Travis CI build steps
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Mar 13, 2019
1 parent 61a438d commit ddf2b6e
Showing 1 changed file with 52 additions and 36 deletions.
88 changes: 52 additions & 36 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,70 +19,86 @@ cache:
directories:
- node_modules
before_install:
- |
# Setup utility functions
function node_version_lt () {
[[ "$(v "$TRAVIS_NODE_VERSION")" -lt "$(v "${1}")" ]]
}
function npm_module_installed () {
npm -lsp ls | grep -Fq "$(pwd)/node_modules/${1}:${1}@"
}
function npm_remove_module_re () {
node -e '
fs = require("fs");
p = JSON.parse(fs.readFileSync("package.json", "utf8"));
r = RegExp(process.argv[1]);
for (k in p.devDependencies) {
if (r.test(k)) delete p.devDependencies[k];
}
fs.writeFileSync("package.json", JSON.stringify(p, null, 2) + "\n");
' "$@"
}
function npm_use_module () {
node -e '
fs = require("fs");
p = JSON.parse(fs.readFileSync("package.json", "utf8"));
p.devDependencies[process.argv[1]] = process.argv[2];
fs.writeFileSync("package.json", JSON.stringify(p, null, 2) + "\n");
' "$@"
}
function v () {
tr '.' '\n' <<< "${1}" \
| awk '{ printf "%03d", $0 }' \
| sed 's/^0*//'
}
# Configure npm
- |
# Skip updating shrinkwrap / lock
npm config set shrinkwrap false
# Setup Node.js version-specific dependencies
- |
# istanbul for coverage
# - remove on Node.js < 0.10
if [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -eq 0 && "$(cut -d. -f2 <<< "$TRAVIS_NODE_VERSION")" -lt 10 ]]; then
npm rm --silent --save-dev istanbul
# Configure istanbul for coverage
if node_version_lt '0.10'; then npm_remove_module_re '^istanbul$'
fi
- |
# eslint for linting
# - remove on Node.js < 6
if [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -lt 6 ]]; then
node -pe 'Object.keys(require("./package").devDependencies).join("\n")' | \
grep -E '^eslint(-|$)' | \
xargs npm rm --save-dev
# Configure eslint for linting
if node_version_lt '6.0'; then npm_remove_module_re '^eslint(-|$)'
fi
- |
# mocha for testing
# - use 1.x for Node.js < 0.8
# - use 2.x for Node.js < 0.10
# - use 3.x for Node.js < 4
if [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -eq 0 && "$(cut -d. -f2 <<< "$TRAVIS_NODE_VERSION")" -lt 8 ]]; then
npm install --save-dev mocha@1.21.5
elif [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -eq 0 && "$(cut -d. -f2 <<< "$TRAVIS_NODE_VERSION")" -lt 10 ]]; then
npm install --save-dev mocha@2.5.3
elif [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -lt 4 ]]; then
npm install --save-dev mocha@3.5.3
# Configure mocha for testing
if node_version_lt '0.10'; then npm_use_module 'mocha' '2.5.3'
elif node_version_lt '4.0' ; then npm_use_module 'mocha' '3.5.3'
fi
- |
# supertest for http calls
# - use 1.1.0 for Node.js < 0.10
# - use 2.0.0 for Node.js < 4
if [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -eq 0 && "$(cut -d. -f2 <<< "$TRAVIS_NODE_VERSION")" -lt 10 ]]; then
npm install --save-dev supertest@1.1.0
elif [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -lt 4 ]]; then
npm install --save-dev supertest@2.0.0
# Configure supertest for http calls
if node_version_lt '0.10'; then npm_use_module 'supertest' '1.1.0'
elif node_version_lt '4.0' ; then npm_use_module 'supertest' '2.0.0'
fi
# Update Node.js modules
- |
# Prune and rebuild node_modules
# Prune & rebuild node_modules
if [[ -d node_modules ]]; then
npm prune
npm rebuild
fi
before_scrpt:
- |
# Contents of node_modules
npm -s ls ||:
script:
- |
# Run test script, depending on istanbul install
if npm -ps ls istanbul | grep -q istanbul; then
npm run-script test-travis
else
npm test
if npm_module_installed 'istanbul'; then npm run-script test-travis
else npm test
fi
- |
# Run linting, depending on eslint install
if npm -ps ls eslint | grep -q eslint; then
npm run-script lint
# Run linting, if eslint exists
if npm_module_installed 'eslint'; then npm run-script lint
fi
after_script:
- |
# Upload coverage to coveralls if exists
if [[ -f ./coverage/lcov.info ]]; then
if [[ -e ./coverage/lcov.info ]]; then
npm install --save-dev coveralls@2
coveralls < ./coverage/lcov.info
fi

0 comments on commit ddf2b6e

Please sign in to comment.