mirror of
https://github.com/all-contributors/cli.git
synced 2025-01-09 21:46:29 +00:00
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:
parent
9aa79485c2
commit
febecff496
3 changed files with 35 additions and 0 deletions
|
@ -1,4 +1,6 @@
|
||||||
|
import {unlink} from 'fs'
|
||||||
import {addContributorsList} from '../init-content'
|
import {addContributorsList} from '../init-content'
|
||||||
|
import ensureFileExists from '../file-exist'
|
||||||
|
|
||||||
test('insert list under contributors section', () => {
|
test('insert list under contributors section', () => {
|
||||||
const content = [
|
const content = [
|
||||||
|
@ -27,3 +29,21 @@ test('create contributors section if content is empty', () => {
|
||||||
|
|
||||||
expect(result).toMatchSnapshot()
|
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
11
src/init/file-exist.js
Normal 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)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
const util = require('../util')
|
const util = require('../util')
|
||||||
const prompt = require('./prompt')
|
const prompt = require('./prompt')
|
||||||
const initContent = require('./init-content')
|
const initContent = require('./init-content')
|
||||||
|
const ensureFileExists = require('./file-exist')
|
||||||
|
|
||||||
const configFile = util.configFile
|
const configFile = util.configFile
|
||||||
const markdown = util.markdown
|
const markdown = util.markdown
|
||||||
|
@ -13,6 +14,9 @@ module.exports = function init() {
|
||||||
return prompt().then(result => {
|
return prompt().then(result => {
|
||||||
return configFile
|
return configFile
|
||||||
.writeConfig('.all-contributorsrc', result.config)
|
.writeConfig('.all-contributorsrc', result.config)
|
||||||
|
.then(() => {
|
||||||
|
ensureFileExists(result.contributorFile)
|
||||||
|
})
|
||||||
.then(() =>
|
.then(() =>
|
||||||
injectInFile(result.contributorFile, initContent.addContributorsList),
|
injectInFile(result.contributorFile, initContent.addContributorsList),
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue