From b2e000a68e89e77975e34b9a61b37373fedf3e0d Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Mon, 21 Jan 2019 22:50:32 -0500 Subject: [PATCH] build: speed up logic in Travis CI build steps --- .travis.yml | 72 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 61 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9af6991..3942415 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,18 +18,68 @@ cache: directories: - node_modules before_install: - # Skip updating shrinkwrap / lock - - "npm config set shrinkwrap false" + - | + # 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 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 - - "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev istanbul" - - "test $(echo $TRAVIS_NODE_VERSION | cut -d. -f1) -ge 6 || npm rm --save-dev $(grep -E '\"eslint\\S*\"' package.json | cut -d'\"' -f2)" + - | + # Configure eslint for linting + if node_version_lt '6.0'; then npm_remove_module_re '^eslint(-|$)' + fi + - | + # Configure istanbul for coverage + if node_version_lt '0.10'; then npm_remove_module_re '^istanbul$' + fi # Update Node.js modules - - "test ! -d node_modules || npm prune" - - "test ! -d node_modules || npm rebuild" + - | + # 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 - - "test ! -z $(npm -ps ls istanbul) || npm test" - - "test -z $(npm -ps ls istanbul) || npm run-script test-travis" - - "test -z $(npm -ps ls eslint ) || npm run-script lint" + - | + # Run test script, depending on istanbul install + if npm_module_installed 'istanbul'; then npm run-script test-travis + else npm test + fi + - | + # Run linting, if eslint exists + if npm_module_installed 'eslint'; then npm run-script lint + fi after_script: - - "test -e ./coverage/lcov.info && npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" + - | + # Upload coverage to coveralls if exists + if [[ -e ./coverage/lcov.info ]]; then + npm install --save-dev coveralls@2 + coveralls < ./coverage/lcov.info + fi