Skip to content

Commit 51f99b9

Browse files
committed
build: speed up logic in Travis CI build steps
1 parent 43dde8f commit 51f99b9

File tree

1 file changed

+49
-43
lines changed

1 file changed

+49
-43
lines changed

Diff for: .travis.yml

+49-43
Original file line numberDiff line numberDiff line change
@@ -21,54 +21,63 @@ cache:
2121
directories:
2222
- node_modules
2323
before_install:
24+
- |
25+
# Setup utility functions
26+
function node_version_lt () {
27+
[[ "$(v "$TRAVIS_NODE_VERSION")" -lt "$(v "${1}")" ]]
28+
}
29+
function npm_module_installed () {
30+
npm -lsp ls | grep -Fq "$(pwd)/node_modules/${1}:${1}@"
31+
}
32+
function npm_remove_module_re () {
33+
node -e '
34+
fs = require("fs");
35+
p = JSON.parse(fs.readFileSync("package.json", "utf8"));
36+
r = RegExp(process.argv[1]);
37+
for (k in p.devDependencies) {
38+
if (r.test(k)) delete p.devDependencies[k];
39+
}
40+
fs.writeFileSync("package.json", JSON.stringify(p, null, 2) + "\n");
41+
' "$@"
42+
}
43+
function npm_use_module () {
44+
node -e '
45+
fs = require("fs");
46+
p = JSON.parse(fs.readFileSync("package.json", "utf8"));
47+
p.devDependencies[process.argv[1]] = process.argv[2];
48+
fs.writeFileSync("package.json", JSON.stringify(p, null, 2) + "\n");
49+
' "$@"
50+
}
51+
function v () {
52+
tr '.' '\n' <<< "${1}" \
53+
| awk '{ printf "%03d", $0 }' \
54+
| sed 's/^0*//'
55+
}
2456
# Configure npm
2557
- |
2658
# Skip updating shrinkwrap / lock
2759
npm config set shrinkwrap false
2860
# Setup Node.js version-specific dependencies
2961
- |
30-
# eslint for linting
31-
# - remove on Node.js < 8
32-
if [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -lt 8 ]]; then
33-
node -pe 'Object.keys(require("./package").devDependencies).join("\n")' | \
34-
grep -E '^eslint(-|$)' | \
35-
xargs npm rm --save-dev
62+
# Configure eslint for linting
63+
if node_version_lt '8.0'; then npm_remove_module_re '^eslint(-|$)'
3664
fi
3765
- |
38-
# istanbul for coverage
39-
# - remove on Node.js < 0.10
40-
if [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -eq 0 && "$(cut -d. -f2 <<< "$TRAVIS_NODE_VERSION")" -lt 10 ]]; then
41-
npm rm --save-dev istanbul
66+
# Configure istanbul for coverage
67+
if node_version_lt '0.10'; then npm_remove_module_re '^istanbul$'
4268
fi
4369
- |
44-
# mocha for testing
45-
# - use 1.x for Node.js < 0.8
46-
# - use 2.x for Node.js < 0.10
47-
# - use 3.x for Node.js < 4
48-
# - use 5.x for Node.js < 6
49-
# - use 6.x for Node.js < 8
50-
if [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -eq 0 && "$(cut -d. -f2 <<< "$TRAVIS_NODE_VERSION")" -lt 8 ]]; then
51-
npm install --save-dev [email protected]
52-
elif [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -eq 0 && "$(cut -d. -f2 <<< "$TRAVIS_NODE_VERSION")" -lt 10 ]]; then
53-
npm install --save-dev [email protected]
54-
elif [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -lt 4 ]]; then
55-
npm install --save-dev [email protected]
56-
elif [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -lt 6 ]]; then
57-
npm install --save-dev [email protected]
58-
elif [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -lt 8 ]]; then
59-
npm install --save-dev [email protected]
70+
# Configure mocha for testing
71+
if node_version_lt '0.10'; then npm_use_module 'mocha' '2.5.3'
72+
elif node_version_lt '4.0' ; then npm_use_module 'mocha' '3.5.3'
73+
elif node_version_lt '6.0' ; then npm_use_module 'mocha' '5.2.0'
74+
elif node_version_lt '8.0' ; then npm_use_module 'mocha' '6.2.2'
6075
fi
6176
- |
62-
# supertest for http calls
63-
# - use 1.1.0 for Node.js < 0.10
64-
# - use 2.0.0 for Node.js < 4
65-
# - use 3.4.2 for Node.js < 6
66-
if [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -eq 0 && "$(cut -d. -f2 <<< "$TRAVIS_NODE_VERSION")" -lt 10 ]]; then
67-
npm install --silent --save-dev [email protected]
68-
elif [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -lt 4 ]]; then
69-
npm install --silent --save-dev [email protected]
70-
elif [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -lt 6 ]]; then
71-
npm install --save-dev [email protected]
77+
# Configure supertest for http calls
78+
if node_version_lt '0.10'; then npm_use_module 'supertest' '1.1.0'
79+
elif node_version_lt '4.0' ; then npm_use_module 'supertest' '2.0.0'
80+
elif node_version_lt '6.0' ; then npm_use_module 'supertest' '3.4.2'
7281
fi
7382
# Update Node.js modules
7483
- |
@@ -81,15 +90,12 @@ before_install:
8190
script:
8291
- |
8392
# Run test script, depending on istanbul install
84-
if npm -ps ls istanbul | grep -q istanbul; then
85-
npm run-script test-travis
86-
else
87-
npm test
93+
if npm_module_installed 'istanbul'; then npm run-script test-travis
94+
else npm test
8895
fi
8996
- |
90-
# Run linting, depending on eslint install
91-
if npm -ps ls eslint | grep -q eslint; then
92-
npm run-script lint
97+
# Run linting, if eslint exists
98+
if npm_module_installed 'eslint'; then npm run-script lint
9399
fi
94100
after_script:
95101
- |

0 commit comments

Comments
 (0)