Skip to content

Commit

Permalink
feat(type): add API method to tell if commit type is public or chore, c…
Browse files Browse the repository at this point in the history
…lose #12
  • Loading branch information
bahmutov committed Jul 6, 2017
1 parent bd689cb commit f0599a2
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 8 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ const result = simple.parse(message)
simple.validate(message, log)
// message wizard built on top of inquirer
simple.prompter(inquirer, cb)
// returns true if the commit message is public ("feat", etc)
simple.isPublic(
simple.parse("feat(foo): add foo feature").type
) // true
```

See [inquirer](https://www.npmjs.com/package/inquirer) for details.
Expand Down
38 changes: 38 additions & 0 deletions __snapshots__/is-public-spec.js.snap-shot
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
exports['determines if commit is public 1'] = {
"name": "isPublicCommitType",
"behavior": [
{
"given": "major",
"expect": true
},
{
"given": "break",
"expect": true
},
{
"given": "minor",
"expect": true
},
{
"given": "feat",
"expect": true
},
{
"given": "fix",
"expect": true
},
{
"given": "patch",
"expect": true
},
{
"given": "chore",
"expect": false
},
{
"given": "anything else",
"expect": false
}
]
}

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
"ramda": "0.23.0",
"rocha": "2.3.0",
"semantic-release": "6.0.3",
"snap-shot": "2.7.2",
"standard": "8.6.0"
"snap-shot": "2.17.0",
"standard": "10.0.2"
},
"config": {
"pre-git": {
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const analyze = eval( // eslint-disable-line no-eval
const props = {
parse: require('./valid-message').parse,
validate: require('./valid-message').validate,
prompter: require('./message-wizard').prompter
prompter: require('./message-wizard').prompter,
isPublic: require('./is-public')
}

const combined = Object.assign(analyze, props)
Expand Down
15 changes: 15 additions & 0 deletions src/is-public-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

const snapshot = require('snap-shot')

/* global describe, it */
describe('is-public', () => {
const isPublic = require('./is-public')

it('determines if commit is public', () => {
snapshot(isPublic,
'major', 'break', 'minor', 'feat', 'fix', 'patch', 'chore',
'anything else'
)
})
})
20 changes: 20 additions & 0 deletions src/is-public.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const validMessage = require('./valid-message')

// users only care about these commit types
const publicTypes = {
major: true,
break: true,
minor: true,
feat: true,
fix: true,
patch: true
}

function isPublicCommitType (s) {
if (!validMessage.isValidType(s)) {
return false
}
return Boolean(publicTypes[s])
}

module.exports = isPublicCommitType
4 changes: 2 additions & 2 deletions src/message-wizard.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const join = require('path').join
function fillQuestions (questions) {
// assumes commit from the package folder
const pkg = require(join(process.cwd(), 'package.json'))
const currentTag = pkg.publishConfig &&
pkg.publishConfig.tag || 'latest'
const configuredTag = pkg.publishConfig && pkg.publishConfig.tag
const currentTag = configuredTag || 'latest'

const filledQuestions = updateQuestion(questions, {
name: 'tag',
Expand Down
18 changes: 15 additions & 3 deletions src/valid-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ const TYPE_MAP = {
chore: 'chore'
}

// given "major" or "break" returns "major" for example
function normalizeCommitType (s) {
return TYPE_MAP[s] || s
}

function isValidType (s) {
const type = normalizeCommitType(s)
return TYPES.hasOwnProperty(type)
}

// removes all lines that start with '#'
function removeComments (str) {
return str.split('\n').filter(s => !/^\s*#/.test(s)).join('\n')
Expand All @@ -51,7 +61,7 @@ function parseMessage (str) {
return
}

const type = TYPE_MAP[match[2]] || match[2]
const type = normalizeCommitType(match[2])
return {
firstLine: match[1],
type: type,
Expand Down Expand Up @@ -101,7 +111,7 @@ function validateMessage (message, log) {
return false
}

if (!TYPES.hasOwnProperty(parsed.type)) {
if (!isValidType(parsed.type)) {
failedMessage('"%s" is not allowed type !', parsed.type)
return false
}
Expand All @@ -121,5 +131,7 @@ function validateMessage (message, log) {

module.exports = {
validate: validateMessage,
parse: parseMessage
parse: parseMessage,
normalize: normalizeCommitType,
isValidType: isValidType
}

0 comments on commit f0599a2

Please sign in to comment.