Skip to content

Commit 3e6e6a8

Browse files
authored
feat(travis-cli): add reusable travis command line interface
* related #99
1 parent ee6ec6e commit 3e6e6a8

File tree

22 files changed

+431
-50
lines changed

22 files changed

+431
-50
lines changed

.editorconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ insert_final_newline = true
44
trim_trailing_whitespace = true
55
indent_style = tab
66

7-
[{.*rc,*.yml,*.md,package.json,*.svg}]
7+
[{.*rc,*.yml,*.md,package.json,lerna.json,*.svg}]
88
indent_style = space
99

1010
[*.md]

.travis.yml

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ node_js:
66
install:
77
- npm install -g npx
88
- npx yarn install
9-
- npx lerna bootstrap --since $TRAVIS_BRANCH --include-filtered-dependencies --stream
9+
- npx lerna bootstrap
1010
- npm --version
1111
- yarn --version
1212
- npx --version
1313
script:
14-
- npx yarn build --since $TRAVIS_BRANCH --include-filtered-dependencies --stream
15-
- npx yarn lint --since $TRAVIS_BRANCH --include-filtered-dependencies --stream
16-
- npx yarn deps --since $TRAVIS_BRANCH --include-filtered-dependencies --stream
17-
- npx yarn test --since $TRAVIS_BRANCH --include-filtered-dependencies --stream
14+
- npx yarn build
15+
- node @commitlint/travis-cli/lib/cli.js
16+
- npx yarn lint
17+
- npx yarn deps
18+
- npx yarn test

@commitlint/cli/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const path = require('path');
2+
3+
module.exports = path.join(__dirname, 'lib/cli.js');

@commitlint/cli/package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
"commitlint": "./lib/cli.js"
77
},
88
"scripts": {
9-
"build": "npx cross-env NODE_ENV=production npx -p babel-cli babel src --out-dir lib --source-maps",
9+
"build": "cross-env NODE_ENV=production babel src --out-dir lib --source-maps",
1010
"clean": "npx rimraf lib",
1111
"deps": "dep-check",
12-
"lint": "npx xo",
13-
"start": "npx concurrently \"npx ava -c 4 --verbose --watch\" \"npx yarn run watch\"",
14-
"test": "npx ava -c 4 --verbose",
15-
"watch": "npx -p babel-cli babel src --out-dir lib --watch --source-maps"
12+
"lint": "xo",
13+
"start": "concurrently \"ava -c 4 --verbose --watch\" \"yarn run watch\"",
14+
"test": "ava -c 4 --verbose",
15+
"watch": "babel src --out-dir lib --watch --source-maps"
1616
},
1717
"ava": {
1818
"files": [

@commitlint/config-angular-type-enum/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"scripts": {
66
"clean": "exit 0",
77
"deps": "dep-check",
8-
"lint": "npx xo",
8+
"lint": "xo",
99
"start": "exit 0",
1010
"test": "exit 0"
1111
},

@commitlint/config-angular/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"scripts": {
66
"clean": "exit 0",
77
"deps": "dep-check",
8-
"lint": "npx xo",
8+
"lint": "xo",
99
"start": "exit 0",
1010
"test": "exit 0"
1111
},

@commitlint/config-lerna-scopes/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
"description": "Shareable commitlint config enforcing lerna package names as scopes",
55
"scripts": {
66
"clean": "exit 0",
7-
"lint": "npx xo",
8-
"start": "npx ava --watch --verbose",
9-
"test": "npx ava --verbose"
7+
"lint": "xo",
8+
"start": "ava --watch --verbose",
9+
"test": "ava --verbose"
1010
},
1111
"xo": false,
1212
"ava": {

@commitlint/config-patternplate/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"scripts": {
66
"clean": "exit 0",
77
"deps": "dep-check",
8-
"lint": "npx xo",
8+
"lint": "xo",
99
"start": "exit 0",
1010
"test": "exit 0"
1111
},

@commitlint/core/package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
"description": "Lint your commit messages",
55
"main": "lib/index.js",
66
"scripts": {
7-
"build": "npx cross-env NODE_ENV=production npx -p babel-cli babel src --out-dir lib --source-maps",
7+
"build": "cross-env NODE_ENV=production babel src --out-dir lib --source-maps",
88
"clean": "npx rimraf lib",
99
"deps": "dep-check",
10-
"lint": "npx xo",
11-
"start": "npx concurrently \"npx ava -c 4 --verbose --watch\" \"npx yarn run watch\"",
12-
"test": "npx ava -c 4 --verbose && npx ava \"src/*.serial-test.js\" --verbose",
13-
"watch": "npx -p babel-cli babel src --out-dir lib --watch --source-maps"
10+
"lint": "xo",
11+
"start": "concurrently \"ava -c 4 --verbose --watch\" \"yarn run watch\"",
12+
"test": "ava -c 4 --verbose && ava \"src/*.serial-test.js\" --verbose",
13+
"watch": "babel src --out-dir lib --watch --source-maps"
1414
},
1515
"ava": {
1616
"files": [

@commitlint/prompt-cli/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"clean": "npx rimraf lib",
1010
"commit": "$npm_package_bin_commit",
1111
"deps": "dep-check",
12-
"lint": "npx xo"
12+
"lint": "xo"
1313
},
1414
"xo": false,
1515
"repository": {

@commitlint/prompt/package.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
"description": "commitizen prompt using commitlint.config.js",
55
"main": "./lib/index.js",
66
"scripts": {
7-
"build": "npx cross-env NODE_ENV=production npx -p babel-cli babel src --out-dir lib --source-maps",
7+
"build": "cross-env NODE_ENV=production babel src --out-dir lib --source-maps",
88
"clean": "npx rimraf lib",
9-
"commit": "npx git-cz",
9+
"commit": "git-cz",
1010
"deps": "dep-check",
11-
"lint": "npx xo",
12-
"start": "npx concurrently \"npx ava --watch --verbose\" \"npx yarn run watch\"",
13-
"test": "npx ava --verbose",
14-
"watch": "npx -p babel-cli babel src --out-dir lib --watch --source-maps"
11+
"lint": "xo",
12+
"start": "concurrently \"ava --watch --verbose\" \"yarn run watch\"",
13+
"test": "ava --verbose",
14+
"watch": "babel src --out-dir lib --watch --source-maps"
1515
},
1616
"ava": {
1717
"babel": "inherit",

@commitlint/travis-cli/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
> Lint all relevant commits for a change or PR on Travis CI
2+
3+
# @commitlint/travis-cli
4+
5+
This package is a convenience wrapper around `commitlint`,
6+
providing zero-configuration linting of all relevant commits
7+
for a given change/build combination.
8+
9+
## Getting started
10+
11+
```
12+
npm install --save-dev @commitlint/travis-cli
13+
```
14+
15+
```yml
16+
# .travis.yml
17+
script
18+
- commitlint-travis
19+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env node
2+
console.log(process.argv);
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env node
2+
console.log(process.argv);

@commitlint/travis-cli/package.json

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{
2+
"name": "@commitlint/travis-cli",
3+
"version": "5.0.1",
4+
"description": "Lint all relevant commits for a change or PR on Travis CI",
5+
"bin": {
6+
"commitlint-travis": "./lib/cli.js"
7+
},
8+
"scripts": {
9+
"build": "cross-env NODE_ENV=production babel src --out-dir lib --source-maps",
10+
"clean": "npx rimraf lib",
11+
"deps": "dep-check",
12+
"lint": "xo",
13+
"start": "ava -c 4 --verbose --watch",
14+
"test": "ava -c 4 --verbose",
15+
"watch": "babel src --out-dir lib --watch --source-maps"
16+
},
17+
"ava": {
18+
"files": [
19+
"src/**/*.test.js"
20+
],
21+
"source": [
22+
"lib/**/*.js"
23+
],
24+
"babel": "inherit",
25+
"require": [
26+
"babel-register"
27+
]
28+
},
29+
"babel": {
30+
"presets": [
31+
"babel-preset-commitlint"
32+
]
33+
},
34+
"xo": false,
35+
"engines": {
36+
"node": ">=4"
37+
},
38+
"repository": {
39+
"type": "git",
40+
"url": "https://github.com/marionebl/commitlint.git"
41+
},
42+
"bugs": {
43+
"url": "https://github.com/marionebl/commitlint/issues"
44+
},
45+
"homepage": "https://github.com/marionebl/commitlint#readme",
46+
"keywords": [
47+
"conventional-changelog",
48+
"commitlint",
49+
"cli"
50+
],
51+
"author": {
52+
"name": "Mario Nebl",
53+
"email": "[email protected]"
54+
},
55+
"license": "MIT",
56+
"devDependencies": {
57+
"@commitlint/utils": "^5.0.1",
58+
"@commitlint/test": "^5.0.1",
59+
"ava": "0.18.2",
60+
"babel-cli": "6.26.0",
61+
"babel-preset-commitlint": "^5.0.1",
62+
"babel-register": "6.26.0",
63+
"cross-env": "5.1.1",
64+
"which": "^1.3.0"
65+
},
66+
"dependencies": {
67+
"@commitlint/cli": "^5.0.1",
68+
"@marionebl/sander": "^0.6.1",
69+
"execa": "^0.8.0",
70+
"find-up": "^2.1.0"
71+
}
72+
}

@commitlint/travis-cli/src/cli.js

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env node
2+
const sander = require('@marionebl/sander');
3+
const execa = require('execa');
4+
const findUp = require('find-up');
5+
6+
// Allow to override used bins for testing purposes
7+
const GIT = process.env.TRAVIS_COMMITLINT_GIT_BIN || 'git';
8+
const COMMITLINT =
9+
process.env.TRAVIS_COMMITLINT_BIN || require('@commitlint/cli'); // eslint-disable-line import/newline-after-import
10+
const REQUIRED = ['TRAVIS_COMMIT', 'TRAVIS_BRANCH'];
11+
12+
const TRAVIS_BRANCH = process.env.TRAVIS_BRANCH;
13+
const TRAVIS_COMMIT = process.env.TRAVIS_COMMIT;
14+
15+
main().catch(err => {
16+
console.log(err);
17+
process.exit(1);
18+
});
19+
20+
async function main() {
21+
if (process.env.CI !== 'true' || process.env.TRAVIS !== 'true') {
22+
throw new Error(
23+
`@commitlint/travis-cli is inteded to be used on Travis CI`
24+
);
25+
}
26+
27+
const gitRoot = await findUp('.git');
28+
const missing = REQUIRED.filter(envVar => !(envVar in process.env));
29+
30+
if (missing.length > 0) {
31+
const stanza = missing.length > 1 ? 'they were not' : 'it was not';
32+
throw new Error(
33+
`Expected ${missing.join(', ')} to be defined globally, ${stanza}.`
34+
);
35+
}
36+
37+
const pop = await stash();
38+
39+
await git(['remote', 'set-branches', 'origin', TRAVIS_BRANCH]);
40+
41+
if (await sander.exists(gitRoot, 'shallow')) {
42+
await git(['fetch', '--unshallow', '--quiet']);
43+
}
44+
45+
await git(['checkout', TRAVIS_BRANCH, '--quiet']);
46+
await git(['checkout', '-', '--quiet']);
47+
48+
await pop();
49+
50+
await lint(['--from', TRAVIS_BRANCH, '--to', TRAVIS_COMMIT]);
51+
}
52+
53+
async function git(args, options) {
54+
return execa(GIT, args, Object.assign({}, {stdio: 'inherit'}, options));
55+
}
56+
57+
async function isClean() {
58+
const result = await git(['status', '--porcelain'], {
59+
stdio: ['pipe', 'pipe', 'pipe']
60+
});
61+
return !(result.stdout && result.stdout.trim());
62+
}
63+
64+
async function lint(args, options) {
65+
return execa(
66+
COMMITLINT,
67+
args,
68+
Object.assign({}, {stdio: 'inherit'}, options)
69+
);
70+
}
71+
72+
async function stash() {
73+
if (await isClean()) {
74+
return async () => {};
75+
}
76+
await git(['stash']);
77+
return () => git(['stash', 'pop']);
78+
}

0 commit comments

Comments
 (0)