Skip to content

Commit

Permalink
feat(init): creates the contributor file if not found (#158)
Browse files Browse the repository at this point in the history
* feat(init): creates the contributor file if not found

If not found, the specified contributor file (defaulting to `README.md`) will be created prior to
injections in it.

re all-contributors/app#105

* refactor(init): renamed a module and its export

Renamed `check-file` to `file-exist` and its exported function to `ensureFileExists`

* test(init): added contributors file test

Added two test cases for the contributors file existence check
  • Loading branch information
Berkmann18 committed May 7, 2019
1 parent 9aa7948 commit febecff
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/init/__tests__/add-contributors-list.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {unlink} from 'fs'
import {addContributorsList} from '../init-content'
import ensureFileExists from '../file-exist'

test('insert list under contributors section', () => {
const content = [
Expand Down Expand Up @@ -27,3 +29,21 @@ test('create contributors section if content is empty', () => {

expect(result).toMatchSnapshot()
})

test('README exists', done => {
const file = 'README.md'
ensureFileExists(file)
.then(data => expect(data).toStrictEqual(file))
.then(_ => done())
})

test("LOREM doesn't exists", done => {
const file = 'LOREM.md'
ensureFileExists(file).then(data => {
expect(data).toStrictEqual(file)
return unlink(file, err => {
if (err) throw err
done()
})
})
})
11 changes: 11 additions & 0 deletions src/init/file-exist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const fs = require('fs')

module.exports = function ensureFileExists(file) {
return new Promise((resolve, reject) => {
if (fs.existsSync(file)) return resolve(file)
fs.writeFile(file, '', err => {
if (err) reject(err)
resolve(file)
})
})
}
4 changes: 4 additions & 0 deletions src/init/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const util = require('../util')
const prompt = require('./prompt')
const initContent = require('./init-content')
const ensureFileExists = require('./file-exist')

const configFile = util.configFile
const markdown = util.markdown
Expand All @@ -13,6 +14,9 @@ module.exports = function init() {
return prompt().then(result => {
return configFile
.writeConfig('.all-contributorsrc', result.config)
.then(() => {
ensureFileExists(result.contributorFile)
})
.then(() =>
injectInFile(result.contributorFile, initContent.addContributorsList),
)
Expand Down

0 comments on commit febecff

Please sign in to comment.