Skip to content

Commit

Permalink
feat(travis-cli): add reusable travis command line interface
Browse files Browse the repository at this point in the history
* related #99
  • Loading branch information
marionebl authored Nov 24, 2017
1 parent ee6ec6e commit 3e6e6a8
Show file tree
Hide file tree
Showing 22 changed files with 431 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ insert_final_newline = true
trim_trailing_whitespace = true
indent_style = tab

[{.*rc,*.yml,*.md,package.json,*.svg}]
[{.*rc,*.yml,*.md,package.json,lerna.json,*.svg}]
indent_style = space

[*.md]
Expand Down
11 changes: 6 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ node_js:
install:
- npm install -g npx
- npx yarn install
- npx lerna bootstrap --since $TRAVIS_BRANCH --include-filtered-dependencies --stream
- npx lerna bootstrap
- npm --version
- yarn --version
- npx --version
script:
- npx yarn build --since $TRAVIS_BRANCH --include-filtered-dependencies --stream
- npx yarn lint --since $TRAVIS_BRANCH --include-filtered-dependencies --stream
- npx yarn deps --since $TRAVIS_BRANCH --include-filtered-dependencies --stream
- npx yarn test --since $TRAVIS_BRANCH --include-filtered-dependencies --stream
- npx yarn build
- node @commitlint/travis-cli/lib/cli.js
- npx yarn lint
- npx yarn deps
- npx yarn test
3 changes: 3 additions & 0 deletions @commitlint/cli/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const path = require('path');

module.exports = path.join(__dirname, 'lib/cli.js');
10 changes: 5 additions & 5 deletions @commitlint/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
"commitlint": "./lib/cli.js"
},
"scripts": {
"build": "npx cross-env NODE_ENV=production npx -p babel-cli babel src --out-dir lib --source-maps",
"build": "cross-env NODE_ENV=production babel src --out-dir lib --source-maps",
"clean": "npx rimraf lib",
"deps": "dep-check",
"lint": "npx xo",
"start": "npx concurrently \"npx ava -c 4 --verbose --watch\" \"npx yarn run watch\"",
"test": "npx ava -c 4 --verbose",
"watch": "npx -p babel-cli babel src --out-dir lib --watch --source-maps"
"lint": "xo",
"start": "concurrently \"ava -c 4 --verbose --watch\" \"yarn run watch\"",
"test": "ava -c 4 --verbose",
"watch": "babel src --out-dir lib --watch --source-maps"
},
"ava": {
"files": [
Expand Down
2 changes: 1 addition & 1 deletion @commitlint/config-angular-type-enum/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"clean": "exit 0",
"deps": "dep-check",
"lint": "npx xo",
"lint": "xo",
"start": "exit 0",
"test": "exit 0"
},
Expand Down
2 changes: 1 addition & 1 deletion @commitlint/config-angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"clean": "exit 0",
"deps": "dep-check",
"lint": "npx xo",
"lint": "xo",
"start": "exit 0",
"test": "exit 0"
},
Expand Down
6 changes: 3 additions & 3 deletions @commitlint/config-lerna-scopes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"description": "Shareable commitlint config enforcing lerna package names as scopes",
"scripts": {
"clean": "exit 0",
"lint": "npx xo",
"start": "npx ava --watch --verbose",
"test": "npx ava --verbose"
"lint": "xo",
"start": "ava --watch --verbose",
"test": "ava --verbose"
},
"xo": false,
"ava": {
Expand Down
2 changes: 1 addition & 1 deletion @commitlint/config-patternplate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"clean": "exit 0",
"deps": "dep-check",
"lint": "npx xo",
"lint": "xo",
"start": "exit 0",
"test": "exit 0"
},
Expand Down
10 changes: 5 additions & 5 deletions @commitlint/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"description": "Lint your commit messages",
"main": "lib/index.js",
"scripts": {
"build": "npx cross-env NODE_ENV=production npx -p babel-cli babel src --out-dir lib --source-maps",
"build": "cross-env NODE_ENV=production babel src --out-dir lib --source-maps",
"clean": "npx rimraf lib",
"deps": "dep-check",
"lint": "npx xo",
"start": "npx concurrently \"npx ava -c 4 --verbose --watch\" \"npx yarn run watch\"",
"test": "npx ava -c 4 --verbose && npx ava \"src/*.serial-test.js\" --verbose",
"watch": "npx -p babel-cli babel src --out-dir lib --watch --source-maps"
"lint": "xo",
"start": "concurrently \"ava -c 4 --verbose --watch\" \"yarn run watch\"",
"test": "ava -c 4 --verbose && ava \"src/*.serial-test.js\" --verbose",
"watch": "babel src --out-dir lib --watch --source-maps"
},
"ava": {
"files": [
Expand Down
2 changes: 1 addition & 1 deletion @commitlint/prompt-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"clean": "npx rimraf lib",
"commit": "$npm_package_bin_commit",
"deps": "dep-check",
"lint": "npx xo"
"lint": "xo"
},
"xo": false,
"repository": {
Expand Down
12 changes: 6 additions & 6 deletions @commitlint/prompt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
"description": "commitizen prompt using commitlint.config.js",
"main": "./lib/index.js",
"scripts": {
"build": "npx cross-env NODE_ENV=production npx -p babel-cli babel src --out-dir lib --source-maps",
"build": "cross-env NODE_ENV=production babel src --out-dir lib --source-maps",
"clean": "npx rimraf lib",
"commit": "npx git-cz",
"commit": "git-cz",
"deps": "dep-check",
"lint": "npx xo",
"start": "npx concurrently \"npx ava --watch --verbose\" \"npx yarn run watch\"",
"test": "npx ava --verbose",
"watch": "npx -p babel-cli babel src --out-dir lib --watch --source-maps"
"lint": "xo",
"start": "concurrently \"ava --watch --verbose\" \"yarn run watch\"",
"test": "ava --verbose",
"watch": "babel src --out-dir lib --watch --source-maps"
},
"ava": {
"babel": "inherit",
Expand Down
19 changes: 19 additions & 0 deletions @commitlint/travis-cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
> Lint all relevant commits for a change or PR on Travis CI
# @commitlint/travis-cli

This package is a convenience wrapper around `commitlint`,
providing zero-configuration linting of all relevant commits
for a given change/build combination.

## Getting started

```
npm install --save-dev @commitlint/travis-cli
```

```yml
# .travis.yml
script
- commitlint-travis
```
2 changes: 2 additions & 0 deletions @commitlint/travis-cli/fixtures/commitlint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
console.log(process.argv);
2 changes: 2 additions & 0 deletions @commitlint/travis-cli/fixtures/git.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
console.log(process.argv);
72 changes: 72 additions & 0 deletions @commitlint/travis-cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"name": "@commitlint/travis-cli",
"version": "5.0.1",
"description": "Lint all relevant commits for a change or PR on Travis CI",
"bin": {
"commitlint-travis": "./lib/cli.js"
},
"scripts": {
"build": "cross-env NODE_ENV=production babel src --out-dir lib --source-maps",
"clean": "npx rimraf lib",
"deps": "dep-check",
"lint": "xo",
"start": "ava -c 4 --verbose --watch",
"test": "ava -c 4 --verbose",
"watch": "babel src --out-dir lib --watch --source-maps"
},
"ava": {
"files": [
"src/**/*.test.js"
],
"source": [
"lib/**/*.js"
],
"babel": "inherit",
"require": [
"babel-register"
]
},
"babel": {
"presets": [
"babel-preset-commitlint"
]
},
"xo": false,
"engines": {
"node": ">=4"
},
"repository": {
"type": "git",
"url": "https://github.com/marionebl/commitlint.git"
},
"bugs": {
"url": "https://github.com/marionebl/commitlint/issues"
},
"homepage": "https://github.com/marionebl/commitlint#readme",
"keywords": [
"conventional-changelog",
"commitlint",
"cli"
],
"author": {
"name": "Mario Nebl",
"email": "[email protected]"
},
"license": "MIT",
"devDependencies": {
"@commitlint/utils": "^5.0.1",
"@commitlint/test": "^5.0.1",
"ava": "0.18.2",
"babel-cli": "6.26.0",
"babel-preset-commitlint": "^5.0.1",
"babel-register": "6.26.0",
"cross-env": "5.1.1",
"which": "^1.3.0"
},
"dependencies": {
"@commitlint/cli": "^5.0.1",
"@marionebl/sander": "^0.6.1",
"execa": "^0.8.0",
"find-up": "^2.1.0"
}
}
78 changes: 78 additions & 0 deletions @commitlint/travis-cli/src/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env node
const sander = require('@marionebl/sander');
const execa = require('execa');
const findUp = require('find-up');

// Allow to override used bins for testing purposes
const GIT = process.env.TRAVIS_COMMITLINT_GIT_BIN || 'git';
const COMMITLINT =
process.env.TRAVIS_COMMITLINT_BIN || require('@commitlint/cli'); // eslint-disable-line import/newline-after-import
const REQUIRED = ['TRAVIS_COMMIT', 'TRAVIS_BRANCH'];

const TRAVIS_BRANCH = process.env.TRAVIS_BRANCH;
const TRAVIS_COMMIT = process.env.TRAVIS_COMMIT;

main().catch(err => {
console.log(err);
process.exit(1);
});

async function main() {
if (process.env.CI !== 'true' || process.env.TRAVIS !== 'true') {
throw new Error(
`@commitlint/travis-cli is inteded to be used on Travis CI`
);
}

const gitRoot = await findUp('.git');
const missing = REQUIRED.filter(envVar => !(envVar in process.env));

if (missing.length > 0) {
const stanza = missing.length > 1 ? 'they were not' : 'it was not';
throw new Error(
`Expected ${missing.join(', ')} to be defined globally, ${stanza}.`
);
}

const pop = await stash();

await git(['remote', 'set-branches', 'origin', TRAVIS_BRANCH]);

if (await sander.exists(gitRoot, 'shallow')) {
await git(['fetch', '--unshallow', '--quiet']);
}

await git(['checkout', TRAVIS_BRANCH, '--quiet']);
await git(['checkout', '-', '--quiet']);

await pop();

await lint(['--from', TRAVIS_BRANCH, '--to', TRAVIS_COMMIT]);
}

async function git(args, options) {
return execa(GIT, args, Object.assign({}, {stdio: 'inherit'}, options));
}

async function isClean() {
const result = await git(['status', '--porcelain'], {
stdio: ['pipe', 'pipe', 'pipe']
});
return !(result.stdout && result.stdout.trim());
}

async function lint(args, options) {
return execa(
COMMITLINT,
args,
Object.assign({}, {stdio: 'inherit'}, options)
);
}

async function stash() {
if (await isClean()) {
return async () => {};
}
await git(['stash']);
return () => git(['stash', 'pop']);
}
Loading

0 comments on commit 3e6e6a8

Please sign in to comment.