@@ -22,6 +22,32 @@ cache:
22
22
- node_modules
23
23
before_install :
24
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
+ }
25
51
function v () {
26
52
tr '.' '\n' <<< "${1}" \
27
53
| awk '{ printf "%03d", $0 }' \
@@ -33,33 +59,19 @@ before_install:
33
59
npm config set shrinkwrap false
34
60
# Setup Node.js version-specific dependencies
35
61
- |
36
- # eslint for linting
37
- if [[ "$(v "$TRAVIS_NODE_VERSION")" -lt "$(v '6.0')" ]]; then
38
- # - remove on Node.js < 6
39
- node -pe 'Object.keys(require("./package").devDependencies).join("\n")' | \
40
- grep -E '^eslint(-|$)' | \
41
- xargs npm rm --save-dev
62
+ # Configure eslint for linting
63
+ if node_version_lt '6.0'; then npm_remove_module_re '^eslint(-|$)'
42
64
fi
43
65
- |
44
- # istanbul for coverage
45
- if [[ "$(v "$TRAVIS_NODE_VERSION")" -lt "$(v '0.10')" ]]; then
46
- # - remove on Node.js < 0.10
47
- npm rm --save-dev istanbul
66
+ # Configure istanbul for coverage
67
+ if node_version_lt '0.10'; then npm_remove_module_re '^istanbul$'
48
68
fi
49
69
- |
50
- # mocha for testing
51
- if [[ "$(v "$TRAVIS_NODE_VERSION")" -lt "$(v '0.8')" ]]; then
52
- # - use 1.x for Node.js < 0.8
53
- npm install --save-dev [email protected]
54
- elif [[ "$(v "$TRAVIS_NODE_VERSION")" -lt "$(v '0.10')" ]]; then
55
- # - use 2.x for Node.js < 0.10
56
- npm install --save-dev [email protected]
57
- elif [[ "$(v "$TRAVIS_NODE_VERSION")" -lt "$(v '4.0')" ]]; then
58
- # - use 3.x for Node.js < 4
59
- npm install --save-dev [email protected]
60
- elif [[ "$(v "$TRAVIS_NODE_VERSION")" -lt "$(v '6.0')" ]]; then
61
- # - use 5.x for Node.js < 6
62
- npm install --save-dev [email protected]
70
+ # Configure mocha for testing
71
+ if node_version_lt '0.8' ; then npm_use_module 'mocha' '1.21.5'
72
+ elif node_version_lt '0.10'; then npm_use_module 'mocha' '2.5.3'
73
+ elif node_version_lt '4.0' ; then npm_use_module 'mocha' '3.5.3'
74
+ elif node_version_lt '6.0' ; then npm_use_module 'mocha' '5.2.0'
63
75
fi
64
76
# Update Node.js modules
65
77
- |
@@ -68,19 +80,15 @@ before_install:
68
80
npm prune
69
81
npm rebuild
70
82
fi
71
-
72
83
script :
73
84
- |
74
85
# Run test script, depending on istanbul install
75
- if npm -ps ls istanbul | grep -q istanbul; then
76
- npm run-script test-travis
77
- else
78
- npm test
86
+ if npm_module_installed 'istanbul'; then npm run-script test-travis
87
+ else npm test
79
88
fi
80
89
- |
81
- # Run linting, depending on eslint install
82
- if npm -ps ls eslint | grep -q eslint; then
83
- npm run-script lint
90
+ # Run linting, if eslint exists
91
+ if npm_module_installed 'eslint'; then npm run-script lint
84
92
fi
85
93
after_script :
86
94
- |
0 commit comments