diff --git a/src/init/__tests__/add-contributors-list.js b/src/init/__tests__/add-contributors-list.js index a6f05a6..cb690ce 100644 --- a/src/init/__tests__/add-contributors-list.js +++ b/src/init/__tests__/add-contributors-list.js @@ -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() + }) + }) +}) diff --git a/src/init/file-exist.js b/src/init/file-exist.js new file mode 100644 index 0000000..9266104 --- /dev/null +++ b/src/init/file-exist.js @@ -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) + }) + }) +} diff --git a/src/init/index.js b/src/init/index.js index 14759f6..59fa9bd 100644 --- a/src/init/index.js +++ b/src/init/index.js @@ -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), )