Skip to content

Commit bcdf5c1

Browse files
committed
feat: initial version
0 parents  commit bcdf5c1

16 files changed

+25277
-0
lines changed

.eslintignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/.git
2+
/node_modules
3+
package.json
4+
package-lock.json

.eslintrc.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
env: {
3+
commonjs: true,
4+
es2021: true,
5+
node: true,
6+
jest: true
7+
},
8+
plugins: ['prettier'],
9+
extends: [
10+
'standard',
11+
// resolve conflicts between prettier and eslint
12+
'plugin:prettier/recommended'
13+
],
14+
parserOptions: {
15+
ecmaVersion: 12
16+
},
17+
rules: {}
18+
}

.gitignore

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Editor
2+
.vscode/
3+
4+
# Secure
5+
.coveralls.yml
6+
7+
# Logs
8+
logs
9+
*.log
10+
npm-debug.log*
11+
yarn-debug.log*
12+
yarn-error.log*
13+
lerna-debug.log*
14+
15+
# Diagnostic reports (https://nodejs.org/api/report.html)
16+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
17+
18+
# Runtime data
19+
pids
20+
*.pid
21+
*.seed
22+
*.pid.lock
23+
24+
# Directory for instrumented libs generated by jscoverage/JSCover
25+
lib-cov
26+
27+
# Coverage directory used by tools like istanbul
28+
coverage
29+
*.lcov
30+
31+
# nyc test coverage
32+
.nyc_output
33+
34+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
35+
.grunt
36+
37+
# Bower dependency directory (https://bower.io/)
38+
bower_components
39+
40+
# node-waf configuration
41+
.lock-wscript
42+
43+
# Compiled binary addons (https://nodejs.org/api/addons.html)
44+
build/Release
45+
46+
# Dependency directories
47+
node_modules/
48+
jspm_packages/
49+
50+
# Snowpack dependency directory (https://snowpack.dev/)
51+
web_modules/
52+
53+
# TypeScript cache
54+
*.tsbuildinfo
55+
56+
# Optional npm cache directory
57+
.npm
58+
59+
# Optional eslint cache
60+
.eslintcache
61+
62+
# Microbundle cache
63+
.rpt2_cache/
64+
.rts2_cache_cjs/
65+
.rts2_cache_es/
66+
.rts2_cache_umd/
67+
68+
# Optional REPL history
69+
.node_repl_history
70+
71+
# Output of 'npm pack'
72+
*.tgz
73+
74+
# Yarn Integrity file
75+
.yarn-integrity
76+
77+
# dotenv environment variables file
78+
.env
79+
.env.test
80+
81+
# parcel-bundler cache (https://parceljs.org/)
82+
.cache
83+
.parcel-cache
84+
85+
# Next.js build output
86+
.next
87+
out
88+
89+
# Nuxt.js build / generate output
90+
.nuxt
91+
dist
92+
93+
# Gatsby files
94+
.cache/
95+
# Comment in the public line in if your project uses Gatsby and not Next.js
96+
# https://nextjs.org/blog/next-9-1#public-directory-support
97+
# public
98+
99+
# vuepress build output
100+
.vuepress/dist
101+
102+
# Serverless directories
103+
.serverless/
104+
105+
# FuseBox cache
106+
.fusebox/
107+
108+
# DynamoDB Local files
109+
.dynamodb/
110+
111+
# TernJS port file
112+
.tern-port
113+
114+
# Stores VSCode versions used for testing VSCode extensions
115+
.vscode-test
116+
117+
# yarn v2
118+
.yarn/cache
119+
.yarn/unplugged
120+
.yarn/build-state.yml
121+
.yarn/install-state.gz
122+
.pnp.*

.husky/commit-msg

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
npx --no-install commitlint --edit "$1"

.husky/pre-commit

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
npm run pre-commit

.prettierrc.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
bracketSpacing: true,
3+
singleQuote: true,
4+
trailingComma: "none",
5+
arrowParens: "avoid",
6+
semi: false,
7+
};

.travis.yml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
language: node_js
2+
node_js:
3+
- 'stable'
4+
before_script:
5+
- npm prune
6+
jobs:
7+
include:
8+
- stage: test
9+
script:
10+
- echo "Linting ..." && npm run lint
11+
- echo "Code Coverage and Test ..." && npm run test:coverage
12+
after-success:
13+
- echo "Semantic Release Deployment .."
14+
- npm run semantic-release
15+
branches:
16+
only:
17+
- master

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Ridvan Altun
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## Introduction
2+
3+
[![npm version](https://img.shields.io/npm/v/commitlint-plugin-selective-scope.svg)](https://npmjs.com/package/commitlint-plugin-selective-scope)
4+
[![npm downloads](https://img.shields.io/npm/dt/commitlint-plugin-selective-scope.svg)](https://npmjs.com/package/commitlint-plugin-selective-scope)
5+
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
6+
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
7+
[![Build Status](https://travis-ci.com/ridvanaltun/commitlint-plugin-selective-scope.svg?branch=master)](https://travis-ci.com/ridvanaltun/commitlint-plugin-selective-scope)
8+
[![Coverage Status](https://coveralls.io/repos/github/ridvanaltun/commitlint-plugin-selective-scope/badge.svg?branch=master)](https://coveralls.io/github/ridvanaltun/commitlint-plugin-selective-scope?branch=master)
9+
[![license](https://img.shields.io/npm/l/commitlint-plugin-selective-scope.svg)](https://github.com/ridvanaltun/commitlint-plugin-selective-scope/blob/master/LICENSE)
10+
11+
> Limit scopes per type with regexp and plain text.
12+
13+
## Installation
14+
15+
```bash
16+
npm i commitlint-plugin-selective-scope --save-dev
17+
```
18+
19+
## Usage
20+
21+
- If a type does not appear in the rule, do not enforce scope
22+
- If a type appears in the rule with an empty array, do not allow scope
23+
- If a type appears in the rule with an non-empty array, only allow the values in the array for scope.
24+
- If the array includes null, the scope is optional.
25+
26+
## Example
27+
28+
```javascript
29+
module.exports = {
30+
plugins: ['selective-scope'],
31+
rules: {
32+
'selective-scope': [
33+
2,
34+
'always',
35+
{
36+
feat: [/^frontend\/[^\/]+$/, 'backend'],
37+
perf: [],
38+
ci: [null, 'codebuild', 'jenkins']
39+
}
40+
]
41+
}
42+
}
43+
```
44+
45+
**In the above rules configuration,**
46+
47+
- if the type is `feat`, the scope must be either match the regex `/frontend\/[^\/]+/` or be `backend`
48+
- if the type if `chore`, the scope is optional and can be anything
49+
- if the type is `perf`, a scope is not allowed
50+
- if the type is `ci`, the scope must be `codebuild` or `jenkins` if present, but is not required

__tests__/plugin.test.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const commitlintPluginSelectiveScope = require('../index')
2+
3+
describe('commitlintPluginSelectiveScope', () => {
4+
it('should return a valid config', () => {
5+
expect(commitlintPluginSelectiveScope).toHaveProperty('rules')
6+
expect(
7+
Object.keys(commitlintPluginSelectiveScope.rules).length
8+
).toBeGreaterThan(0)
9+
})
10+
})

commitlint.config.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = {
2+
extends: ['@commitlint/config-conventional'],
3+
rules: {
4+
'body-max-line-length': [1, 'always', 100]
5+
},
6+
helpUrl: `
7+
Commit messages must follow conventional commit format:
8+
https://www.conventionalcommits.org/en/v1.0.0/#summary
9+
type(optional-scope): subject
10+
11+
[optional body]
12+
* To bypass pre-commit hooks run 'git commit --no-verify'
13+
>>> Use "npm run commit" for interactive prompt. <<<
14+
`
15+
}

index.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
module.exports = {
2+
rules: {
3+
'selective-scope': (ctx, applicable, rule) => {
4+
if (applicable === 'never') {
5+
return [false, 'the "allowed-scopes" rule does not support "never"']
6+
}
7+
8+
const allowedScopes = rule[ctx.type]
9+
10+
// If the type does not appear in the rule config, allow any scope or no scope
11+
if (allowedScopes === undefined) {
12+
return [true]
13+
}
14+
15+
if (Array.isArray(allowedScopes)) {
16+
// If the type maps to an empty array in the rule config, scope it not allowed
17+
if (allowedScopes.length === 0) {
18+
if (ctx.scope != null) {
19+
return [
20+
false,
21+
`commit messages with type "${ctx.type}" must not specify a scope`
22+
]
23+
}
24+
25+
return [true]
26+
}
27+
28+
// Otherwise attempt to match against either null, a string literal, or a RegExp
29+
if (
30+
allowedScopes.findIndex(s => {
31+
if (
32+
typeof ctx.scope === 'string' &&
33+
Object.prototype.toString.call() === '[object RegExp]'
34+
) {
35+
return ctx.scope.match(s)
36+
}
37+
38+
// Equality comparison works for both strings and null
39+
return s === ctx.scope
40+
}) !== -1
41+
) {
42+
return [true]
43+
} else if (allowedScopes.includes(null)) {
44+
return [
45+
false,
46+
`commit message with type "${
47+
ctx.type
48+
}" may specify a scope, but if specified, it must be one of the following: ${allowedScopes
49+
.filter(s => s !== null)
50+
.map(s => `"${s}"`)
51+
.join(', ')}`
52+
]
53+
} else {
54+
return [
55+
false,
56+
`commit message with type "${
57+
ctx.type
58+
}" must specify one of the following scopes: ${allowedScopes
59+
.map(s => `"${s}"`)
60+
.join(', ')}`
61+
]
62+
}
63+
}
64+
65+
return [
66+
false,
67+
`invalid rule entry: { ${ctx.type}: ${JSON.stringify(allowedScopes)} }`
68+
]
69+
}
70+
}
71+
}

jest.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
verbose: true,
3+
roots: ['__tests__/']
4+
}

0 commit comments

Comments
 (0)