-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update to commitlint v11 (#62)
* feat(): update to commitlint v11 * test: split code and add tests * chore: lint and add test script
- Loading branch information
1 parent
69162c0
commit 30f8219
Showing
6 changed files
with
289 additions
and
280 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
name: "build" | ||
on: | ||
pull_request: | ||
push: | ||
|
||
jobs: | ||
build: # make sure build/ci work properly | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { | ||
LintOptions, | ||
ParserOptions, | ||
ParserPreset, | ||
QualifiedConfig | ||
} from '@commitlint/types' | ||
import load from '@commitlint/load' | ||
import lint from '@commitlint/lint' | ||
|
||
function selectParserOpts(parserPreset: ParserPreset) { | ||
if (typeof parserPreset !== 'object') { | ||
return undefined | ||
} | ||
|
||
if (typeof parserPreset.parserOpts !== 'object') { | ||
return undefined | ||
} | ||
|
||
return parserPreset.parserOpts | ||
} | ||
|
||
function getLintOptions(configuration: QualifiedConfig): LintOptions { | ||
const parserOpts = selectParserOpts(configuration.parserPreset) | ||
const opts: LintOptions & {parserOpts: ParserOptions} = { | ||
parserOpts: {}, | ||
plugins: {}, | ||
ignores: [], | ||
defaultIgnores: true | ||
} | ||
if (parserOpts) { | ||
opts.parserOpts = parserOpts | ||
} | ||
if (configuration.plugins) { | ||
opts.plugins = configuration.plugins | ||
} | ||
if (configuration.ignores) { | ||
opts.ignores = configuration.ignores | ||
} | ||
if (!configuration.defaultIgnores) { | ||
opts.defaultIgnores = false | ||
} | ||
return opts | ||
} | ||
|
||
export async function lintPullRequest(title: string, configPath: string) { | ||
const configuration = await load({}, {file: configPath, cwd: process.cwd()}) | ||
|
||
const options = getLintOptions(configuration) | ||
|
||
const result = await lint(title, configuration.rules, options) | ||
|
||
if (result.valid) return | ||
const errorMessage = result.errors | ||
.map(({message, name}: any) => `${name}:${message}`) | ||
.join('\n') | ||
throw new Error(errorMessage) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {lintPullRequest} from '../src/linter' | ||
|
||
test('should lint the title correctly', async () => { | ||
// Given | ||
const title = 'feat(valid): scope' | ||
|
||
// Expect | ||
await expect( | ||
lintPullRequest(title, './commitlint.config.js') | ||
).resolves.toBeUndefined() | ||
}) | ||
|
||
test('should raise errors', async () => { | ||
// Given | ||
const title = 'feat(INVALID): scope' | ||
|
||
// Expect | ||
await expect( | ||
lintPullRequest(title, './commitlint.config.js') | ||
).rejects.toThrow() | ||
}) |
Oops, something went wrong.