Skip to content

Commit ddf2b6e

Browse files
committed
build: simplify & speed up logic in Travis CI build steps
1 parent 61a438d commit ddf2b6e

File tree

1 file changed

+52
-36
lines changed

1 file changed

+52
-36
lines changed

.travis.yml

+52-36
Original file line numberDiff line numberDiff line change
@@ -19,70 +19,86 @@ cache:
1919
directories:
2020
- node_modules
2121
before_install:
22+
- |
23+
# Setup utility functions
24+
function node_version_lt () {
25+
[[ "$(v "$TRAVIS_NODE_VERSION")" -lt "$(v "${1}")" ]]
26+
}
27+
function npm_module_installed () {
28+
npm -lsp ls | grep -Fq "$(pwd)/node_modules/${1}:${1}@"
29+
}
30+
function npm_remove_module_re () {
31+
node -e '
32+
fs = require("fs");
33+
p = JSON.parse(fs.readFileSync("package.json", "utf8"));
34+
r = RegExp(process.argv[1]);
35+
for (k in p.devDependencies) {
36+
if (r.test(k)) delete p.devDependencies[k];
37+
}
38+
fs.writeFileSync("package.json", JSON.stringify(p, null, 2) + "\n");
39+
' "$@"
40+
}
41+
function npm_use_module () {
42+
node -e '
43+
fs = require("fs");
44+
p = JSON.parse(fs.readFileSync("package.json", "utf8"));
45+
p.devDependencies[process.argv[1]] = process.argv[2];
46+
fs.writeFileSync("package.json", JSON.stringify(p, null, 2) + "\n");
47+
' "$@"
48+
}
49+
function v () {
50+
tr '.' '\n' <<< "${1}" \
51+
| awk '{ printf "%03d", $0 }' \
52+
| sed 's/^0*//'
53+
}
2254
# Configure npm
2355
- |
2456
# Skip updating shrinkwrap / lock
2557
npm config set shrinkwrap false
2658
# Setup Node.js version-specific dependencies
2759
- |
28-
# istanbul for coverage
29-
# - remove on Node.js < 0.10
30-
if [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -eq 0 && "$(cut -d. -f2 <<< "$TRAVIS_NODE_VERSION")" -lt 10 ]]; then
31-
npm rm --silent --save-dev istanbul
60+
# Configure istanbul for coverage
61+
if node_version_lt '0.10'; then npm_remove_module_re '^istanbul$'
3262
fi
3363
- |
34-
# eslint for linting
35-
# - remove on Node.js < 6
36-
if [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -lt 6 ]]; then
37-
node -pe 'Object.keys(require("./package").devDependencies).join("\n")' | \
38-
grep -E '^eslint(-|$)' | \
39-
xargs npm rm --save-dev
64+
# Configure eslint for linting
65+
if node_version_lt '6.0'; then npm_remove_module_re '^eslint(-|$)'
4066
fi
4167
- |
42-
# mocha for testing
43-
# - use 1.x for Node.js < 0.8
44-
# - use 2.x for Node.js < 0.10
45-
# - use 3.x for Node.js < 4
46-
if [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -eq 0 && "$(cut -d. -f2 <<< "$TRAVIS_NODE_VERSION")" -lt 8 ]]; then
47-
npm install --save-dev [email protected]
48-
elif [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -eq 0 && "$(cut -d. -f2 <<< "$TRAVIS_NODE_VERSION")" -lt 10 ]]; then
49-
npm install --save-dev [email protected]
50-
elif [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -lt 4 ]]; then
51-
npm install --save-dev [email protected]
68+
# Configure mocha for testing
69+
if node_version_lt '0.10'; then npm_use_module 'mocha' '2.5.3'
70+
elif node_version_lt '4.0' ; then npm_use_module 'mocha' '3.5.3'
5271
fi
5372
- |
54-
# supertest for http calls
55-
# - use 1.1.0 for Node.js < 0.10
56-
# - use 2.0.0 for Node.js < 4
57-
if [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -eq 0 && "$(cut -d. -f2 <<< "$TRAVIS_NODE_VERSION")" -lt 10 ]]; then
58-
npm install --save-dev [email protected]
59-
elif [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -lt 4 ]]; then
60-
npm install --save-dev [email protected]
73+
# Configure supertest for http calls
74+
if node_version_lt '0.10'; then npm_use_module 'supertest' '1.1.0'
75+
elif node_version_lt '4.0' ; then npm_use_module 'supertest' '2.0.0'
6176
fi
6277
# Update Node.js modules
6378
- |
64-
# Prune and rebuild node_modules
79+
# Prune & rebuild node_modules
6580
if [[ -d node_modules ]]; then
6681
npm prune
6782
npm rebuild
6883
fi
84+
before_scrpt:
85+
- |
86+
# Contents of node_modules
87+
npm -s ls ||:
6988
script:
7089
- |
7190
# Run test script, depending on istanbul install
72-
if npm -ps ls istanbul | grep -q istanbul; then
73-
npm run-script test-travis
74-
else
75-
npm test
91+
if npm_module_installed 'istanbul'; then npm run-script test-travis
92+
else npm test
7693
fi
7794
- |
78-
# Run linting, depending on eslint install
79-
if npm -ps ls eslint | grep -q eslint; then
80-
npm run-script lint
95+
# Run linting, if eslint exists
96+
if npm_module_installed 'eslint'; then npm run-script lint
8197
fi
8298
after_script:
8399
- |
84100
# Upload coverage to coveralls if exists
85-
if [[ -f ./coverage/lcov.info ]]; then
101+
if [[ -e ./coverage/lcov.info ]]; then
86102
npm install --save-dev coveralls@2
87103
coveralls < ./coverage/lcov.info
88104
fi

0 commit comments

Comments
 (0)