@@ -19,70 +19,86 @@ cache:
19
19
directories :
20
20
- node_modules
21
21
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
+ }
22
54
# Configure npm
23
55
- |
24
56
# Skip updating shrinkwrap / lock
25
57
npm config set shrinkwrap false
26
58
# Setup Node.js version-specific dependencies
27
59
- |
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$'
32
62
fi
33
63
- |
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(-|$)'
40
66
fi
41
67
- |
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'
52
71
fi
53
72
- |
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'
61
76
fi
62
77
# Update Node.js modules
63
78
- |
64
- # Prune and rebuild node_modules
79
+ # Prune & rebuild node_modules
65
80
if [[ -d node_modules ]]; then
66
81
npm prune
67
82
npm rebuild
68
83
fi
84
+ before_scrpt :
85
+ - |
86
+ # Contents of node_modules
87
+ npm -s ls ||:
69
88
script :
70
89
- |
71
90
# 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
76
93
fi
77
94
- |
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
81
97
fi
82
98
after_script :
83
99
- |
84
100
# Upload coverage to coveralls if exists
85
- if [[ -f ./coverage/lcov.info ]]; then
101
+ if [[ -e ./coverage/lcov.info ]]; then
86
102
npm install --save-dev coveralls@2
87
103
coveralls < ./coverage/lcov.info
88
104
fi
0 commit comments