Skip to content

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

Merged
merged 28 commits into from
Nov 24, 2017
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
6973c6b
build: switch to independent mode
marionebl Nov 22, 2017
58fdd3e
ci: draft shareable travis script
marionebl Nov 22, 2017
a7ad565
ci: relink after building
marionebl Nov 22, 2017
ec62b22
feat(cli): expose cli path
marionebl Nov 23, 2017
0ea70f1
test(travis-cli): cover invocations
marionebl Nov 23, 2017
f7dbac4
style: ensure consistent json indent
marionebl Nov 23, 2017
6436911
fix(travis-cli): remove unneeded meow dep
marionebl Nov 23, 2017
661fa7d
test(travis-cli): skip invocation tests on win32
marionebl Nov 23, 2017
4fa8744
style: simplify catch branch
marionebl Nov 23, 2017
de96e8d
style: avoid Promise constructor
marionebl Nov 23, 2017
2561a31
refactor: use babel to use async/await
marionebl Nov 23, 2017
4d6fb5c
ci: build everything
marionebl Nov 23, 2017
5c15e9e
fix: use Object.assign instead of spread
marionebl Nov 23, 2017
3a8fc35
fix: integrate dogfed tools robustly
marionebl Nov 23, 2017
51c850b
test: add babel build to ava
marionebl Nov 23, 2017
291792e
build(travis-cli): ensure node <8 compat
marionebl Nov 23, 2017
08597d4
fix(travis-cli): be less noisy
marionebl Nov 24, 2017
b37ca41
build: remove npx for speed and simplicity
marionebl Nov 24, 2017
ac4ebc0
build: simplify root level scripts
marionebl Nov 24, 2017
333f287
fix(travis-cli): remove faulty flag
marionebl Nov 24, 2017
e1e1897
build: remove independent mode
marionebl Nov 24, 2017
09f63d7
fix(cli): resolve to artifacts
marionebl Nov 24, 2017
da852e9
fix(travis-cli): stash if git wd is dirty
marionebl Nov 24, 2017
1faa70a
test(travis-cli): increase git coverage
marionebl Nov 24, 2017
ae3b87a
test(travis-cli): explicitly disable Travis CI
marionebl Nov 24, 2017
f8fafa3
test(travis-cli): check if Travis CI on our own
marionebl Nov 24, 2017
930487c
fix(travis-cli): remove obsolete dep
marionebl Nov 24, 2017
9c0f3cd
fix(travis-cli): readd as depdev
marionebl Nov 24, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ install:
- npx --version
script:
- npx yarn build --since $TRAVIS_BRANCH --include-filtered-dependencies --stream
- npx lerna link
- commitlint-travis
- 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
1 change: 1 addition & 0 deletions @commitlint/cli/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require.resolve('./src/cli.js');
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
```
61 changes: 61 additions & 0 deletions @commitlint/travis-cli/cli.js
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});

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?

throw err;
}, 0);
});

function main() {
return new Promise((resolve, reject) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

execa returns a Promise so you shouldn't need to use the Promise constructor

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'}
)
);
});
}
119 changes: 119 additions & 0 deletions @commitlint/travis-cli/cli.test.js
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)
);
}
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);
54 changes: 54 additions & 0 deletions @commitlint/travis-cli/package.json
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",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why npx?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The over-simplified answer is: I introduced npx at one point to ensure the dev setup works regardless of the current state it is in. I'll keep them in travis-cli for now to be consistent, will remove them from all packages in one go in the near future.

"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"
}
}
3 changes: 2 additions & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"lerna": "2.5.1",
"npmClient": "yarn",
"useWorkspaces": true,
"version": "5.0.1"
"version": "5.0.1",
"mode": "independent"
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"@marionebl/sander": "0.6.1"
},
"devDependencies": {
"@commitlint/travis-cli": "file:./@commitlint/travis-cli",
"docsify-cli": "4.1.12",
"eslint": "4.11.0",
"eslint-config-prettier": "2.8.0",
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@
imurmurhash "^0.1.4"
slide "^1.1.5"

"@commitlint/travis-cli@file:./@commitlint/travis-cli":
version "5.0.1"
dependencies:
"@commitlint/cli" "^5.0.1"
execa "^0.8.0"
is-travis "^1.0.0"
meow "3.7.0"

"@concordance/react@^1.0.0":
version "1.0.0"
resolved "https://registry.npmjs.org/@concordance/react/-/react-1.0.0.tgz#fcf3cad020e5121bfd1c61d05bc3516aac25f734"
Expand Down Expand Up @@ -3955,6 +3963,10 @@ is-text-path@^1.0.0:
dependencies:
text-extensions "^1.0.0"

is-travis@^1.0.0:
version "1.0.0"
resolved "https://registry.npmjs.org/is-travis/-/is-travis-1.0.0.tgz#89d40ed56d9b8f8c36dfbe5811ba7e5e14944df9"

is-typedarray@~1.0.0:
version "1.0.0"
resolved "https://registry.npmjs.org/is-typedarray/-/is-typedarray-1.0.0.tgz#e479c80858df0c1b11ddda6940f96011fcda4a9a"
Expand Down