feat(init): creates the contributor file if not found (#158)

* 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 https://github.com/all-contributors/all-contributors-bot/issues/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
This commit is contained in:
Maximilian Berkmann 2019-05-07 16:48:58 +01:00 committed by GitHub
parent 9aa79485c2
commit febecff496
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 0 deletions

View file

@ -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 = [
@ -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
src/init/file-exist.js Normal file
View file

@ -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)
})
})
}

View file

@ -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
@ -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),
)