Skip to content

Commit

Permalink
feat: parse cc spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Qu4k committed Aug 27, 2020
0 parents commit 92e337f
Show file tree
Hide file tree
Showing 11 changed files with 1,712 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
35 changes: 35 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: check

on: [push, pull_request]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2

- name: Setup latest deno version
uses: denolib/setup-deno@v2
with:
deno-version: v1.x

- name: Run deno fmt
run: deno fmt --check

- name: Run deno lint
run: deno lint --unstable

test:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2

- name: Setup latest deno version
uses: denolib/setup-deno@v2
with:
deno-version: v1.x

- name: Run deno test
run: deno test
19 changes: 19 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: release

on:
push:
tags:
- "*.*.*"
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Release
uses: softprops/action-gh-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
draft: true
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# OS files
.DS_Store
.cache

# IDE
.vscode
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020-present the denosaurs team

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# commit

[![Tags](https://img.shields.io/github/release/denosaurs/commit)](https://github.com/denosaurs/commit/releases)
[![CI Status](https://img.shields.io/github/workflow/status/denosaurs/commit/check)](https://github.com/denosaurs/commit/actions)
[![License](https://img.shields.io/github/license/denosaurs/commit)](https://github.com/denosaurs/commit/blob/master/LICENSE)

```typescript
import { parse } from "https://deno.land/x/commit/mod.ts";

const commit = parse("fix(std/io): utf-8 encoding");
console.log(commit);
/* {
type: "fix",
scope: "std/io",
subject: "utf-8 encoding",
merge: null,
header: "fix(std/io): utf-8 encoding",
body: null,
footer: null,
notes: [],
references: [],
mentions: [],
revert: null
} */
```

## other

### contribution

Pull request, issues and feedback are very welcome. Code style is formatted with deno fmt and commit messages are done following Conventional Commits spec.

### licence

Copyright 2020-present, the denosaurs team. All rights reserved. MIT license.
220 changes: 220 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
import { parser } from "./parser.ts";
import { regex } from "./regex.ts";

export type Actions = string[] | null;
export type Correspondence = string[] | null;
export type Keywords = string[] | null;
export type Pattern = RegExp | null;
export type Prefixes = string[] | null;

export interface Options {
/**
* Pattern to match merge headers. EG: branch merge, GitHub or GitLab like pull
* requests headers. When a merge header is parsed, the next line is used for
* conventional header parsing.
*
* For example, if we have a commit
*
* ```text
* Merge pull request #1 from user/feature/feature-name
*
* feat(scope): broadcast $destroy event on scope destruction
* ```
*
* We can parse it with these options and the default headerPattern:
*
* ```javascript
* {
* mergePattern: /^Merge pull request #(\d+) from (.*)$/,
* mergeCorrespondence: ['id', 'source']
* }
* ```
*
* @default
* null
*/
mergePattern?: Pattern;

/**
* Used to define what capturing group of `mergePattern`.
*
* If it's a `string` it will be converted to an `array` separated by a comma.
*
* @default
* null
*/
mergeCorrespondence?: Correspondence;

/**
* Used to match header pattern.
*
* @default
* /^(\w*)(?:\(([\w\$\.\-\* ]*)\))?\: (.*)$/
*/
headerPattern?: Pattern;

/**
* Used to define what capturing group of `headerPattern` captures what header
* part. The order of the array should correspond to the order of
* `headerPattern`'s capturing group. If the part is not captured it is `null`.
* If it's a `string` it will be converted to an `array` separated by a comma.
*
* @default
* ['type', 'scope', 'subject']
*/
headerCorrespondence?: Correspondence;

/**
* Keywords to reference an issue. This value is case __insensitive__. If it's a
* `string` it will be converted to an `array` separated by a comma.
*
* Set it to `null` to reference an issue without any action.
*
* @default
* ['close', 'closes', 'closed', 'fix', 'fixes', 'fixed', 'resolve', 'resolves', 'resolved']
*/
referenceActions?: Actions;

/**
* The prefixes of an issue. EG: In `gh-123` `gh-` is the prefix.
*
* @default
* ['#']
*/
issuePrefixes?: Prefixes;

/**
* Used to define if `issuePrefixes` should be considered case sensitive.
*
* @default
* false
*/
issuePrefixesCaseSensitive?: boolean;

/**
* Keywords for important notes. This value is case __insensitive__. If it's a
* `string` it will be converted to an `array` separated by a comma.
*
* @default
* ['BREAKING CHANGE']
*/
noteKeywords?: Keywords;

/**
* Pattern to match other fields.
*
* @default
* /^-(.*?)-$/
*/
fieldPattern?: Pattern;

/**
* Pattern to match what this commit reverts.
*
* @default
* /^Revert\s"([\s\S]*)"\s*This reverts commit (\w*)\./
*/
revertPattern?: Pattern;

/**
* Used to define what capturing group of `revertPattern` captures what reverted
* commit fields. The order of the array should correspond to the order of
* `revertPattern`'s capturing group.
*
* For example, if we had commit
*
* ```
* Revert "throw an error if a callback is passed"
*
* This reverts commit 9bb4d6c.
* ```
*
* If configured correctly, the parsed result would be
*
* ```
* {
* revert: {
* header: 'throw an error if a callback is passed',
* hash: '9bb4d6c'
* }
* }
* ```
*
* It implies that this commit reverts a commit with header `'throw an error if
* a callback is passed'` and hash `'9bb4d6c'`.
*
* If it's a `string` it will be converted to an `array` separated by a comma.
*
* @default
* ['header', 'hash']
*/
revertCorrespondence?: Correspondence;

/**
* What commentChar to use. By default it is `null`, so no comments are stripped.
* Set to `#` if you pass the contents of `.git/COMMIT_EDITMSG` directly.
*
* If you have configured the git commentchar via git config `core.commentchar`
* you'll want to pass what you have set there.
*
* @default
* null
*/
commentChar?: string | null;

/**
* What warn function to use. For example, `console.warn.bind(console)` or
* `grunt.log.writeln`. By default, it's a noop. If it is `true`, it will error
* if commit cannot be parsed (strict).
*
* @default
* function () {}
*/
warn?: (message?: string) => void | boolean;
}

function assignOpts(options?: Options): Required<Options> {
options = Object.assign(
{
headerPattern: /^(\w*)(?:\(([\w$.\-*/ ]*)\))?: (.*)$/,
headerCorrespondence: ["type", "scope", "subject"],
referenceActions: [
"close",
"closes",
"closed",
"fix",
"fixes",
"fixed",
"resolve",
"resolves",
"resolved",
],
issuePrefixes: ["#"],
noteKeywords: ["BREAKING CHANGE"],
fieldPattern: /^-(.*?)-$/,
revertPattern: /^Revert\s"([\s\S]*)"\s*This reverts commit (\w*)\./,
revertCorrespondence: ["header", "hash"],
warn: function () {},
mergePattern: null,
mergeCorrespondence: null,
},
options,
);

return options as Required<Options>;
}

/**
* The sync version. Useful when parsing a single commit. Returns the result.
*
* @param commit A single commit to be parsed.
* @param options Same as the `options` of `conventionalCommitsParser`.
*/
export function parse(commit?: string, options?: Options) {
options = assignOpts(options);
let reg = regex(options);

return parser(commit, options as Required<Options>, reg);
}

console.log(parse("fix(std/io): utf-8 encoding"));
Loading

0 comments on commit 92e337f

Please sign in to comment.