Skip to content

Commit 2dfa8b0

Browse files
committed
ci(travis): add custom npm install script/lock updater
1 parent 52d8ef6 commit 2dfa8b0

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

.travis.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ node_js:
55
- '8'
66
- '10'
77

8-
before_install: if [[ `npm -v` != 6* ]]; then npm install -g npm@6; fi
8+
before_install:
9+
- if [[ `npm -v` != 6* ]]; then npm install -g npm@6; fi
10+
- npm install -g greenkeeper-lockfile
911

1012
install:
11-
- npm ci
13+
- node travis-install.js
14+
15+
before_script: greenkeeper-lockfile-update
16+
after_script: greenkeeper-lockfile-upload
1217

1318
cache:
1419
directories:

travis-install.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* eslint-disable max-len */
2+
'use strict';
3+
4+
const exec = require('child_process').execSync;
5+
6+
const env = process.env;
7+
8+
const info = {
9+
// The GitHub repo slug
10+
repoSlug: env.TRAVIS_REPO_SLUG,
11+
// The name of the current branch
12+
branchName: env.TRAVIS_BRANCH,
13+
// Is this the first push on this branch
14+
// i.e. the Greenkeeper commit
15+
firstPush: !env.TRAVIS_COMMIT_RANGE,
16+
// Is this a regular build
17+
correctBuild: env.TRAVIS_PULL_REQUEST === 'false',
18+
// Should the lockfile be uploaded from this build
19+
uploadBuild:
20+
env.TRAVIS_JOB_NUMBER &&
21+
env.TRAVIS_JOB_NUMBER.endsWith(`.${env.BUILD_LEADER_ID || 1}`),
22+
};
23+
24+
function isGreenkeeperBranch() {
25+
if (!info.branchName) {
26+
console.error(
27+
'No branch details set, so assuming not a Greenkeeper branch',
28+
);
29+
return false;
30+
}
31+
32+
if (!info.branchName.startsWith('greenkeeper')) {
33+
console.log(`${info.branchName} is not a greenkeeper branch`);
34+
return false;
35+
}
36+
37+
return true;
38+
}
39+
40+
function execCmd(cmd) {
41+
return exec(cmd, { stdio: [
42+
process.stdin,
43+
process.stdout,
44+
process.stderr,
45+
] });
46+
}
47+
module.exports = function() {
48+
if (isGreenkeeperBranch()) {
49+
console.log('found greenkeeper branch build, use npm install');
50+
return execCmd('npm install');
51+
}
52+
console.log('running npm ci');
53+
return execCmd('npm ci');
54+
};
55+
56+
if (require.main === module) {
57+
module.exports();
58+
}

0 commit comments

Comments
 (0)