@@ -21,54 +21,63 @@ cache:
21
21
directories :
22
22
- node_modules
23
23
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
+ }
24
56
# Configure npm
25
57
- |
26
58
# Skip updating shrinkwrap / lock
27
59
npm config set shrinkwrap false
28
60
# Setup Node.js version-specific dependencies
29
61
- |
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(-|$)'
36
64
fi
37
65
- |
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$'
42
68
fi
43
69
- |
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'
60
75
fi
61
76
- |
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'
72
81
fi
73
82
# Update Node.js modules
74
83
- |
@@ -81,15 +90,12 @@ before_install:
81
90
script :
82
91
- |
83
92
# 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
88
95
fi
89
96
- |
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
93
99
fi
94
100
after_script :
95
101
- |
0 commit comments