Skip to content

Commit

Permalink
🎨 (format code using prettier)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngryman committed Aug 21, 2020
1 parent 4c3060c commit c5101dd
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 30 deletions.
6 changes: 6 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "ngryman",
"rules": {
"space-before-function-paren": 0
}
}
13 changes: 8 additions & 5 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
const meow = require('meow')
const contributors = require('./')

const cli = meow(`
const cli = meow(
`
Usage
$ contributor-faces [<directory>]
Expand All @@ -13,10 +14,12 @@ const cli = meow(`
Examples
$ contributor-faces --exclude "*-bot"
`, {
alias: {
e: 'exclude'
`,
{
alias: {
e: 'exclude',
}
}
})
)

contributors.update(cli.input[0], cli.flags)
47 changes: 26 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ function getRepo(baton) {
const repo = pkg.repository && (pkg.repository.url || pkg.repository)

if (!repo) {
throw new Error(`${path.join(baton.dir, 'package.json')}: repository is not set.`)
throw new Error(
`${path.join(baton.dir, 'package.json')}: repository is not set.`
)
}
baton.repo = repo.replace(/https?:\/\/[^\/]+\//, '').replace('.git', '')
return baton
Expand All @@ -27,7 +29,8 @@ function filter(baton) {

const isExcluded = mm.matcher(baton.exclude)
baton.contributors = baton.contributors.filter(
contrib => !isExcluded(contrib.login))
contrib => !isExcluded(contrib.login)
)
return baton
}

Expand All @@ -46,20 +49,25 @@ function html(baton) {
}

function update(baton) {
return readmeFilename(baton.dir)
.then(filename => new Promise((resolve, reject) => {
replace({
files: path.resolve(baton.dir, filename),
// eslint-disable-next-line
from: /\[\/\/\]: contributor-faces(?:(?:\n.*)+\[\/\/\]: contributor-faces)?/,
to: `[//]: contributor-faces\n${baton.html}\n[//]: contributor-faces`
}, err => {
/* istanbul ignore if */
if (err) return reject(err)
baton.filename = filename
resolve(baton)
return readmeFilename(baton.dir).then(
filename =>
new Promise((resolve, reject) => {
replace(
{
files: path.resolve(baton.dir, filename),
// eslint-disable-next-line
from: /\[\/\/\]: contributor-faces(?:(?:\n.*)+\[\/\/\]: contributor-faces)?/,
to: `[//]: contributor-faces\n${baton.html}\n[//]: contributor-faces`
},
err => {
/* istanbul ignore if */
if (err) return reject(err)
baton.filename = filename
resolve(baton)
}
)
})
}))
)
}

function end(prop) {
Expand All @@ -69,10 +77,7 @@ function end(prop) {
function core(dir, opts) {
opts = opts || {}
opts.dir = dir || '.'
return Promise.resolve(opts)
.then(getRepo)
.then(fetch)
.then(filter)
return Promise.resolve(opts).then(getRepo).then(fetch).then(filter)
}

/* ────────────────────────────────────────────────────────────────────────── */
Expand All @@ -81,11 +86,11 @@ function contributors(dir, opts) {
return core(dir, opts).then(end('contributors'))
}

contributors.html = function(dir, opts) {
contributors.html = function (dir, opts) {
return core(dir, opts).then(html).then(end('html'))
}

contributors.update = function(dir, opts) {
contributors.update = function (dir, opts) {
return core(dir, opts).then(html).then(update).then(end('filename'))
}

Expand Down
13 changes: 9 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ test('list contributors as html', async t => {
t.regex(html, /<img src=".*" title="baxterthehacker" width="80" height="80">/)
})

test('update a virgin readme', async t => {
test('update a pristine readme', async t => {
const filename = path.join('fixtures', 'readme.md')
const oldReadme = fs.readFileSync(filename, 'utf8')

await contributors.update(path.dirname(filename))

const readme = fs.readFileSync(filename, 'utf8')
const expected = fs.readFileSync(
filename.replace('.md', '-expected.md'), 'utf8')
filename.replace('.md', '-expected.md'),
'utf8'
)
t.is(readme, expected)

fs.writeFileSync(filename, oldReadme)
Expand All @@ -45,7 +47,9 @@ test('update a readme with an existing contributors list', async t => {

const readme = fs.readFileSync(filename, 'utf8')
const expected = fs.readFileSync(
filename.replace('.md', '-expected.md'), 'utf8')
filename.replace('.md', '-expected.md'),
'utf8'
)
t.is(readme, expected)

fs.writeFileSync(filename, oldReadme)
Expand All @@ -68,7 +72,8 @@ test('throw an error if repository is not present', async t => {
})

test('succeed on repository being an object', async t => {
const contribs = await contributors(path.join('fixtures', 'repository-object'))
const repoDir = path.join('fixtures', 'repository-object')
const contribs = await contributors(repoDir)

t.true(Array.isArray(contribs))
t.is(contribs.length, 1)
Expand Down

0 comments on commit c5101dd

Please sign in to comment.