Skip to content

Commit ddfa758

Browse files
committed
build: simplify & speed up logic in Travis CI build steps
1 parent edc9458 commit ddfa758

File tree

1 file changed

+49
-26
lines changed

1 file changed

+49
-26
lines changed

.travis.yml

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,59 @@ 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
# Remove all non-test dependencies
2759
- |
2860
# Remove benchmark dependencies
29-
npm rm --save-dev beautify-benchmark benchmark
61+
npm_remove_module_re '(^|-)benchmark$'
3062
# Setup Node.js version-specific dependencies
3163
- |
32-
# mocha for testing
33-
# - use 2.x for Node.js < 0.10
34-
# - use 3.x for Node.js < 6
35-
if [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -eq 0 && "$(cut -d. -f2 <<< "$TRAVIS_NODE_VERSION")" -lt 10 ]]; then
36-
npm install --save-dev [email protected]
37-
elif [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -lt 6 ]]; then
38-
npm install --save-dev [email protected]
64+
# Configure istanbul for coverage
65+
if node_version_lt '0.10'; then npm_remove_module_re '^istanbul$'
3966
fi
4067
- |
41-
# istanbul for coverage
42-
# - remove for Node.js < 0.10
43-
if [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -eq 0 && "$(cut -d. -f2 <<< "$TRAVIS_NODE_VERSION")" -lt 10 ]]; then
44-
npm rm --silent --save-dev istanbul
68+
# Configure eslint for linting
69+
if node_version_lt '6.0'; then npm_remove_module_re '^eslint(-|$)'
4570
fi
4671
- |
47-
# eslint for linting
48-
# - remove on Node.js < 6
49-
if [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -lt 6 ]]; then
50-
node -pe 'Object.keys(require("./package").devDependencies).join("\n")' | \
51-
grep -E '^eslint(-|$)' | \
52-
xargs npm rm --save-dev
72+
# Configure mocha for testing
73+
if node_version_lt '0.10'; then npm_use_module 'mocha' '2.5.3'
74+
elif node_version_lt '4.0' ; then npm_use_module 'mocha' '3.5.3'
5375
fi
5476
# Update Node.js modules
5577
- |
@@ -58,18 +80,19 @@ before_install:
5880
npm prune
5981
npm rebuild
6082
fi
83+
before_scrpt:
84+
- |
85+
# Contents of node_modules
86+
npm -s ls ||:
6187
script:
62-
# Run test script
6388
- |
64-
if npm -ps ls istanbul | grep -q istanbul; then
65-
npm run test-travis
66-
else
67-
npm test
89+
# Run test script, depending on istanbul install
90+
if npm_module_installed 'istanbul'; then npm run-script test-travis
91+
else npm test
6892
fi
69-
# Run linting
7093
- |
71-
if npm -ps ls eslint | grep -q eslint; then
72-
npm run lint
94+
# Run linting, if eslint exists
95+
if npm_module_installed 'eslint'; then npm run-script lint
7396
fi
7497
after_script:
7598
- |

0 commit comments

Comments
 (0)