Skip to content

Commit

Permalink
Support .hallmarkrc
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers committed Oct 24, 2020
1 parent 3dc1c87 commit 6469e03
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
4 changes: 3 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ if (argv.help) {
} else if (argv.version) {
console.log(require('./package.json').version)
} else {
argv.files = argv._
if (argv._.length) {
argv.files = argv._
}

require('./index.js')(argv, function (err, code) {
if (err) throw err
Expand Down
55 changes: 32 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
'use strict'

const deglob = require('deglob')
const closest = require('read-closest-package')
const find = require('find-file-up')
const engine = require('unified-engine')
const color = require('supports-color').stdout
const processor = require('remark')
const path = require('path')
const fs = require('fs')

module.exports = function hallmark (options, callback) {
if (typeof options === 'function') {
return hallmark({}, options)
}

const fix = !!options.fix
const cwd = options.cwd || process.cwd()
const pkg = closest.sync({ cwd }) || {}
const packageOpts = pkg.hallmark || {}
const globs = options.files && options.files.length ? options.files : ['*.md']
const repository = repo(options.repository) || repo(packageOpts.repository) || repo(pkg.repository) || originRepo(cwd) || ''
const ignore = concat('ignore', packageOpts, options)

deglob(globs, { usePackageJson: false, cwd, ignore }, function (err, files) {
const cwd = path.resolve(options.cwd || '.')
const pkg = read('package.json', cwd) || {}
const rc = Object.assign({}, read('.hallmarkrc', cwd), pkg.hallmark)
const files = first('files', options, rc) || ['*.md']
const repository = repo(first('repository', options, rc, pkg)) || originRepo(cwd) || ''
const ignore = concat('ignore', rc, options)

deglob(files, { usePackageJson: false, cwd, ignore }, function (err, files) {
if (err) throw err

if (!files.length) {
Expand All @@ -41,17 +43,13 @@ module.exports = function hallmark (options, callback) {
}
}

const paddedTable = packageOpts.paddedTable !== false
const validateLinks = packageOpts.validateLinks !== false
const toc = packageOpts.toc !== false

const contributors = 'contributors' in packageOpts
? packageOpts.contributors
: packageOpts.community

const changelogOptions = Object.assign({}, packageOpts.changelog, options.changelog)
const plugins = { plugins: concat('plugins', packageOpts, options) }
const fixers = { plugins: concat('fixers', packageOpts, options) }
const paddedTable = rc.paddedTable !== false
const validateLinks = rc.validateLinks !== false
const toc = rc.toc !== false
const contributors = 'contributors' in rc ? rc.contributors : rc.community
const changelog = Object.assign({}, rc.changelog, options.changelog)
const plugins = { plugins: concat('plugins', rc, options) }
const fixers = { plugins: concat('fixers', rc, options) }

engine({
processor,
Expand All @@ -78,7 +76,7 @@ module.exports = function hallmark (options, callback) {
}]
: null,

[require('remark-changelog'), { cwd, fix, pkg, repository, ...changelogOptions }],
[require('remark-changelog'), { cwd, fix, pkg, repository, ...changelog }],
[require('remark-github'), { repository }],

// TODO: https://github.com/vweevers/hallmark/issues/36
Expand Down Expand Up @@ -113,12 +111,23 @@ module.exports = function hallmark (options, callback) {
})
}

function read (file, cwd) {
const fp = find.sync(file, cwd, 100)
return fp ? JSON.parse(fs.readFileSync(fp, 'utf8')) : null
}

function first (key, ...sources) {
for (const src of sources) {
if (src[key]) return src[key]
}
}

function repo (repository) {
return repository ? repository.url || repository : null
}

function concat (key, packageOpts, options) {
return [].concat(packageOpts[key] || []).concat(options[key] || [])
function concat (key, rc, options) {
return [].concat(rc[key] || []).concat(options[key] || [])
}

function collapseToc () {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
],
"dependencies": {
"deglob": "^4.0.0",
"find-file-up": "^2.0.1",
"github-url-from-git": "^1.5.0",
"read-closest-package": "^1.2.0",
"remark": "^12.0.1",
"remark-changelog": "^1.2.0",
"remark-collapse": "~0.1.2",
Expand Down

0 comments on commit 6469e03

Please sign in to comment.