Skip to content
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

chore: use standard #84

Merged
merged 11 commits into from
Jul 27, 2020
101 changes: 49 additions & 52 deletions bin/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const exec = require('child_process').exec
const fs = require('fs')
const http = require('http')
const https = require('https')
const url = require('url')
const nopt = require('nopt')
const path = require('path')
const pretty = require('../lib/format-pretty')
Expand All @@ -16,42 +15,42 @@ const Tap = require('../lib/tap')
const utils = require('../lib/utils')
const subsystem = require('../lib/rules/subsystem')
const knownOpts = {
help: Boolean
, version: Boolean
, 'validate-metadata': Boolean
, tap: Boolean
, out: path
, list: Boolean
, 'list-subsystems': Boolean
help: Boolean,
version: Boolean,
'validate-metadata': Boolean,
tap: Boolean,
out: path,
list: Boolean,
'list-subsystems': Boolean
}
const shortHand = {
h: ['--help']
, v: ['--version']
, V: ['--validate-metadata']
, t: ['--tap']
, o: ['--out']
, l: ['--list']
, ls: ['--list-subsystems']
h: ['--help'],
v: ['--version'],
V: ['--validate-metadata'],
t: ['--tap'],
o: ['--out'],
l: ['--list'],
ls: ['--list-subsystems']
}

const parsed = nopt(knownOpts, shortHand)
const usage = require('help')()

if (parsed.help) {
return usage()
usage()
process.exit(0)
}

if (parsed.version) {
console.log('core-validate-commit', 'v' + require('../package').version)
return
process.exit(0)
}

const args = parsed.argv.remain
if (!args.length)
args.push('HEAD')
if (!args.length) { args.push('HEAD') }

function load(sha, cb) {
const parsed = url.parse(sha)
function load (sha, cb) {
const parsed = new URL(sha)
if (parsed.protocol) {
return loadPatch(parsed, cb)
}
Expand All @@ -62,7 +61,7 @@ function load(sha, cb) {
})
}

function loadPatch(uri, cb) {
function loadPatch (uri, cb) {
let h = http
if (~uri.protocol.indexOf('https')) {
h = https
Expand Down Expand Up @@ -91,7 +90,7 @@ const v = new Validator(parsed)

if (parsed['list-subsystems']) {
utils.describeSubsystem(subsystem.defaults.subsystems.sort())
return
process.exit(0)
}

if (parsed.list) {
Expand All @@ -104,15 +103,15 @@ if (parsed.list) {
for (const rule of v.rules.values()) {
utils.describeRule(rule, max)
}
return
process.exit(0)
}

if (parsed.tap) {
const tap = new Tap()
tap.pipe(process.stdout)
if (parsed.out) tap.pipe(fs.createWriteStream(parsed.out))
let count = 0
let total = args.length
const total = args.length

v.on('commit', (c) => {
count++
Expand All @@ -121,41 +120,39 @@ if (parsed.tap) {
if (count === total) {
setImmediate(() => {
tap.end()
if (tap.status === 'fail')
process.exitCode = 1
if (tap.status === 'fail') { process.exitCode = 1 }
})
}
})

function run() {
if (!args.length) return
const sha = args.shift()
load(sha, (err, data) => {
if (err) throw err
v.lint(data)
run()
})
}

run()

tapRun()
} else {
v.on('commit', (c) => {
pretty(c.commit, c.messages, v)
run()
commitRun()
})

function run() {
if (!args.length) {
process.exitCode = v.errors
return
}
const sha = args.shift()
load(sha, (err, data) => {
if (err) throw err
v.lint(data)
})
}
commitRun()
}

function tapRun () {
if (!args.length) return
const sha = args.shift()
load(sha, (err, data) => {
if (err) throw err
v.lint(data)
tapRun()
})
}

run()
function commitRun () {
if (!args.length) {
process.exitCode = v.errors
return
}
const sha = args.shift()
load(sha, (err, data) => {
if (err) throw err
v.lint(data)
})
}
8 changes: 4 additions & 4 deletions lib/format-pretty.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const utils = require('./utils')

const MAX_LINE_COL_LEN = 6

module.exports = function formatPretty(context, msgs, validator, opts) {
module.exports = function formatPretty (context, msgs, validator, opts) {
opts = Object.assign({
detailed: false
}, opts)
Expand Down Expand Up @@ -57,7 +57,7 @@ module.exports = function formatPretty(context, msgs, validator, opts) {
}
}

function formatLength(msg, opts) {
function formatLength (msg, opts) {
const out = formatMessage(msg)
const str = msg.string
const l = str.length
Expand All @@ -68,7 +68,7 @@ function formatLength(msg, opts) {
${diff}`
}

function formatMessage(msg) {
function formatMessage (msg) {
const l = msg.line || 0
const col = msg.column || 0
const pad = utils.rightPad(`${l}:${col}`, MAX_LINE_COL_LEN)
Expand All @@ -83,6 +83,6 @@ function formatMessage(msg) {
return ` ${icon} ${line} ${utils.rightPad(m, 40)} ${id}`
}

function formatId(id) {
function formatId (id) {
return chalk.red(id)
}
59 changes: 29 additions & 30 deletions lib/format-tap.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
'use strict'

module.exports = function formatTap(t, context, msgs, validator) {
module.exports = function formatTap (t, context, msgs, validator) {
for (const m of msgs) {
switch (m.level) {
case 'pass':
const a = m.string
? ` [${m.string}]`
: ''
case 'pass': {
const a = m.string ? ` [${m.string}]` : ''
t.pass(`${m.id}: ${m.message}${a}`)
break
}
case 'skip':
t.skip(`${m.id}: ${m.message}`)
break
Expand All @@ -19,7 +18,7 @@ module.exports = function formatTap(t, context, msgs, validator) {
}
}

function onFail(context, m, validator, t) {
function onFail (context, m, validator, t) {
switch (m.id) {
case 'line-length':
case 'title-length':
Expand All @@ -34,44 +33,44 @@ function onFail(context, m, validator, t) {
}
}

function lengthFail(context, m, validator, t) {
function lengthFail (context, m, validator, t) {
const body = m.id === 'title-length'
? context.title
: context.body
t.fail(`${m.id}: ${m.message}`, {
found: m.string.length
, compare: '<='
, wanted: m.maxLength
, at: {
line: m.line || 0
, column: m.column || 0
, body: body
found: m.string.length,
compare: '<=',
wanted: m.maxLength,
at: {
line: m.line || 0,
column: m.column || 0,
body: body
}
})
}

function subsystemFail(context, m, validator, t) {
function subsystemFail (context, m, validator, t) {
t.fail(`${m.id}: ${m.message} (${m.string})`, {
found: m.string
, compare: 'indexOf() !== -1'
, wanted: m.wanted || ''
, at: {
line: m.line || 0
, column: m.column || 0
, body: m.title
found: m.string,
compare: 'indexOf() !== -1',
wanted: m.wanted || '',
at: {
line: m.line || 0,
column: m.column || 0,
body: m.title
}
})
}

function defaultFail(context, m, validator, t) {
function defaultFail (context, m, validator, t) {
t.fail(`${m.id}: ${m.message} (${m.string})`, {
found: m.string
, compare: Array.isArray(m.wanted) ? 'indexOf() !== -1' : '==='
, wanted: m.wanted || ''
, at: {
line: m.line || 0
, column: m.column || 0
, body: context.body
found: m.string,
compare: Array.isArray(m.wanted) ? 'indexOf() !== -1' : '===',
wanted: m.wanted || '',
at: {
line: m.line || 0,
column: m.column || 0,
body: context.body
}
})
}
17 changes: 8 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const BaseRule = require('./rule')
const RULES = require('./rules')

module.exports = class ValidateCommit extends EE {
constructor(options) {
constructor (options) {
super()

this.opts = Object.assign({
Expand All @@ -20,7 +20,7 @@ module.exports = class ValidateCommit extends EE {
this.loadBaseRules()
}

loadBaseRules() {
loadBaseRules () {
const keys = Object.keys(RULES)
for (const key of keys) {
this.rules.set(key, new BaseRule(RULES[key]))
Expand All @@ -32,15 +32,15 @@ module.exports = class ValidateCommit extends EE {
}
}

disableRule(id) {
disableRule (id) {
if (!this.rules.has(id)) {
throw new TypeError(`Invalid rule: "${id}"`)
}

this.rules.get(id).disabled = true
}

lint(str) {
lint (str) {
if (Array.isArray(str)) {
for (const item of str) {
this.lint(item)
Expand All @@ -54,22 +54,21 @@ module.exports = class ValidateCommit extends EE {

setImmediate(() => {
this.emit('commit', {
commit: commit
, messages: this.messages.get(commit.sha) || []
commit: commit,
messages: this.messages.get(commit.sha) || []
})
})
}
}

report(opts) {
report (opts) {
const commit = opts.commit
const sha = commit.sha
if (!sha) {
throw new Error('Invalid report. Missing commit sha')
}

if (opts.data.level === 'fail')
this.errors++
if (opts.data.level === 'fail') { this.errors++ }
const ar = this.messages.get(sha) || []
ar.push(opts.data)
this.messages.set(sha, ar)
Expand Down
10 changes: 5 additions & 5 deletions lib/rule.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict'

module.exports = class Rule {
constructor(opts) {
constructor (opts) {
opts = Object.assign({
options: {}
, defaults: {}
, meta: {}
options: {},
defaults: {},
meta: {}
}, opts)

if (!opts.id) {
Expand All @@ -24,7 +24,7 @@ module.exports = class Rule {
this._validate = opts.validate
}

validate(commit) {
validate (commit) {
this._validate(commit, this)
}
}
Loading