-
Notifications
You must be signed in to change notification settings - Fork 931
ci: simplified commitlint script #151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
6973c6b
58fdd3e
a7ad565
ec62b22
0ea70f1
f7dbac4
6436911
661fa7d
4fa8744
de96e8d
2561a31
4d6fb5c
5c15e9e
3a8fc35
51c850b
291792e
08597d4
b37ca41
ac4ebc0
333f287
e1e1897
09f63d7
da852e9
1faa70a
ae3b87a
f8fafa3
930487c
9c0f3cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require.resolve('./src/cli.js'); |
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 | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#!/usr/bin/env node | ||
const execa = require('execa'); | ||
const isTravis = require('is-travis'); | ||
|
||
// 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']; | ||
|
||
main().catch(err => { | ||
console.log({err}); | ||
setTimeout(() => { | ||
console.log({err}); | ||
throw err; | ||
}, 0); | ||
}); | ||
|
||
function main() { | ||
return new Promise((resolve, reject) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if (!isTravis) { | ||
return reject( | ||
new Error(`@commitlint/travis-cli is inteded of usage on Travis CI`) | ||
); | ||
} | ||
|
||
const missing = REQUIRED.filter(envVar => !(envVar in process.env)); | ||
|
||
if (missing.length > 0) { | ||
const stanza = missing.length > 1 ? 'they were not' : 'it was not'; | ||
return reject( | ||
new Error( | ||
`Expected ${missing.join(', ')} to be defined globally, ${stanza}.` | ||
) | ||
); | ||
} | ||
|
||
return execa( | ||
GIT, | ||
['remote', 'set-branches', 'origin', process.env.TRAVIS_BRANCH], | ||
{stdio: 'inherit'} | ||
) | ||
.then(() => execa(GIT, ['fetch', '--unshallow'], {stdio: 'inherit'})) | ||
.then(() => | ||
execa(GIT, ['checkout', process.env.TRAVIS_BRANCH], {stdio: 'inherit'}) | ||
) | ||
.then(() => execa(GIT, ['checkout', '-'], {stdio: 'inherit'})) | ||
.then(() => | ||
execa( | ||
COMMITLINT, | ||
[ | ||
'--from', | ||
process.env.TRAVIS_BRANCH, | ||
'--to', | ||
process.env.TRAVIS_COMMIT | ||
], | ||
{stdio: 'inherit'} | ||
) | ||
); | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
const os = require('os'); | ||
const test = require('ava'); | ||
const execa = require('execa'); | ||
const which = require('which'); | ||
|
||
const NODE_BIN = which.sync('node'); | ||
const BIN = require.resolve('./cli.js'); | ||
|
||
const TRAVIS_COMMITLINT_BIN = require.resolve('./fixtures/commitlint'); | ||
const TRAVIS_COMMITLINT_GIT_BIN = require.resolve('./fixtures/git'); | ||
const TRAVIS_BRANCH = 'TRAVIS_BRANCH'; | ||
const TRAVIS_COMMIT = 'TRAVIS_COMMIT'; | ||
|
||
const bin = async (env = {}) => { | ||
try { | ||
return await execa(BIN, {env, extendEnv: false}); | ||
} catch (err) { | ||
throw new Error([err.stdout, err.stderr].join('\n')); | ||
} | ||
}; | ||
|
||
test('should throw when not on travis ci', async t => { | ||
await t.throws( | ||
bin(), | ||
/@commitlint\/travis-cli is inteded of usage on Travis CI/ | ||
); | ||
}); | ||
|
||
test('should throw when on travis ci, but env vars are missing', async t => { | ||
const env = { | ||
TRAVIS: true, | ||
CI: true | ||
}; | ||
|
||
await t.throws(bin(env), /TRAVIS_COMMIT, TRAVIS_BRANCH/); | ||
}); | ||
|
||
test('should throw when on travis ci, but TRAVIS_COMMIT is missing', async t => { | ||
const env = { | ||
TRAVIS: true, | ||
CI: true | ||
}; | ||
|
||
await t.throws(bin(env), /TRAVIS_COMMIT/); | ||
}); | ||
|
||
test('should throw when on travis ci, but TRAVIS_BRANCH is missing', async t => { | ||
const env = { | ||
TRAVIS: true, | ||
CI: true | ||
}; | ||
|
||
await t.throws(bin(env), /TRAVIS_BRANCH/); | ||
}); | ||
|
||
test('should call git with expected args if requirements are fulfilled', async t => { | ||
if (os.platform() === 'win32') { | ||
t.pass(); | ||
return; | ||
} | ||
|
||
const env = { | ||
TRAVIS: true, | ||
CI: true, | ||
TRAVIS_BRANCH, | ||
TRAVIS_COMMIT, | ||
TRAVIS_COMMITLINT_BIN, | ||
TRAVIS_COMMITLINT_GIT_BIN | ||
}; | ||
|
||
const result = await bin(env); | ||
const invocations = await getInvocations(result.stdout); | ||
t.is(invocations.length, 5); | ||
|
||
const [branches, unshallow, checkout, back, commilint] = invocations; | ||
|
||
t.deepEqual(branches, [ | ||
NODE_BIN, | ||
TRAVIS_COMMITLINT_GIT_BIN, | ||
'remote', | ||
'set-branches', | ||
'origin', | ||
TRAVIS_BRANCH | ||
]); | ||
t.deepEqual(unshallow, [ | ||
NODE_BIN, | ||
TRAVIS_COMMITLINT_GIT_BIN, | ||
'fetch', | ||
'--unshallow' | ||
]); | ||
t.deepEqual(checkout, [ | ||
NODE_BIN, | ||
TRAVIS_COMMITLINT_GIT_BIN, | ||
'checkout', | ||
TRAVIS_BRANCH | ||
]); | ||
t.deepEqual(back, [NODE_BIN, TRAVIS_COMMITLINT_GIT_BIN, 'checkout', '-']); | ||
t.deepEqual(commilint, [ | ||
NODE_BIN, | ||
TRAVIS_COMMITLINT_BIN, | ||
'--from', | ||
TRAVIS_BRANCH, | ||
'--to', | ||
TRAVIS_COMMIT | ||
]); | ||
}); | ||
|
||
function getInvocations(stdout) { | ||
const matches = stdout.match(/[^[\]]+/g); | ||
const raw = Array.isArray(matches) ? matches : []; | ||
|
||
return raw.filter(invocation => invocation !== '\n').map(invocation => | ||
invocation | ||
.split(',') | ||
.map(fragment => fragment.trim()) | ||
.map(fragment => fragment.substring(1, fragment.length - 1)) | ||
.filter(Boolean) | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/usr/bin/env node | ||
console.log(process.argv); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/usr/bin/env node | ||
console.log(process.argv); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
{ | ||
"name": "@commitlint/travis-cli", | ||
"version": "5.0.1", | ||
"description": "Lint all relevant commits for a change or PR on Travis CI", | ||
"bin": { | ||
"commitlint-travis": "./cli.js" | ||
}, | ||
"scripts": { | ||
"deps": "dep-check", | ||
"lint": "npx xo", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The over-simplified answer is: I introduced |
||
"start": "npx ava -c 4 --verbose --watch", | ||
"test": "npx ava -c 4 --verbose" | ||
}, | ||
"ava": { | ||
"files": [ | ||
"*.test.js" | ||
], | ||
"source": [ | ||
"*.js" | ||
] | ||
}, | ||
"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", | ||
"ava": "0.18.2", | ||
"which": "^1.3.0" | ||
}, | ||
"dependencies": { | ||
"@commitlint/cli": "^5.0.1", | ||
"execa": "^0.8.0", | ||
"is-travis": "^1.0.0" | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand this - why log the error twice? why not use
console.error(err); process.exit(1);
instead?