Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add check for overwrite readme #143

Merged
merged 4 commits into from
Oct 4, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/ask-overwrite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const inquirer = require('inquirer')

const question = {
type: 'list',
message:
'⚠ Readme-md-generator will overwrite your current README.md. Are you sure you want to continue? ',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'⚠ Readme-md-generator will overwrite your current README.md. Are you sure you want to continue? ',
'⚠ readme-md-generator will overwrite your current README file. Are you sure you want to continue? ',

name: 'overwriteReadme',
choices: [
{
name: 'No',
value: false
},
{
name: 'Yes ',
value: true
}
]
}

/**
* Ask user if he wants overwrite the existed README
*/
module.exports = async () => {
const { overwriteReadme } = await inquirer.prompt([question])
return overwriteReadme
}
44 changes: 44 additions & 0 deletions src/ask-overwrite.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const inquirer = require('inquirer')

const askOverwrite = require('./ask-overwrite')

const expectedQuestion = {
type: 'list',
message:
'⚠ Readme-md-generator will overwrite your current README.md. Are you sure you want to continue? ',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'⚠ Readme-md-generator will overwrite your current README.md. Are you sure you want to continue? ',
'⚠ readme-md-generator will overwrite your current README file. Are you sure you want to continue? ',

name: 'overwriteReadme',
choices: [
{
name: 'No',
value: false
},
{
name: 'Yes ',
value: true
}
]
}

inquirer.prompt = jest.fn(items =>
Promise.resolve(
items.reduce((result, item) => {
result[item.name] = 'value'
return result
}, {})
)
)

describe('ask-overwrite', () => {
beforeEach(() => {
inquirer.prompt.mockClear()
})

it('should call prompt right questions', async () => {
await askOverwrite()
expect(inquirer.prompt).toHaveBeenCalledWith([expectedQuestion])
})

it('should return the right value', async () => {
expect(await askOverwrite()).toEqual('value')
})
})
12 changes: 7 additions & 5 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ const askQuestions = require('./ask-questions')

/**
* Main process:
* 1) Get README template path
* 2) Gather project infos
* 3) Ask user questions
* 4) Build README content
* 5) Create README.md file
* 1) Check overwrite README.md
* 2) Get README template path
* 3) Gather project infos
* 4) Ask user questions
* 5) Build README content
* 6) Create README.md file
*
* @param {Object} args
*/
module.exports = async ({ customTemplatePath, useDefaultAnswers }) => {
if (!(await readme.checkOverwriteReadme())) return
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!(await readme.checkOverwriteReadme())) return
if (!(await readme.checkOverwriteReadme())) return

const templatePath = await readme.getReadmeTemplatePath(
customTemplatePath,
useDefaultAnswers
Expand Down
20 changes: 20 additions & 0 deletions src/cli.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ describe('mainProcess', () => {
askQuestions.mockClear()
})

it('should stop immediatly if user dont want overwrite', async () => {
const customTemplatePath = undefined
const useDefaultAnswers = true
infos.getProjectInfos = jest.fn()
readme.buildReadmeContent = jest.fn()
readme.getReadmeTemplatePath = jest.fn()
readme.writeReadme = jest.fn()
readme.checkOverwriteReadme = jest.fn(() => Promise.resolve(false))
utils.showEndMessage = jest.fn()

await mainProcess({ customTemplatePath, useDefaultAnswers })

expect(infos.getProjectInfos).not.toHaveBeenCalled()
expect(readme.buildReadmeContent).not.toHaveBeenCalled()
expect(readme.getReadmeTemplatePath).not.toHaveBeenCalled()
expect(readme.writeReadme).not.toHaveBeenCalled()
expect(utils.showEndMessage).not.toHaveBeenCalled()
})

it('should call main functions with correct args', async () => {
const customTemplatePath = undefined
const useDefaultAnswers = true
Expand All @@ -52,6 +71,7 @@ describe('mainProcess', () => {
infos.getProjectInfos = jest.fn(() => Promise.resolve(projectInformations))
readme.buildReadmeContent = jest.fn(() => Promise.resolve(readmeContent))
readme.getReadmeTemplatePath = jest.fn(() => Promise.resolve(templatePath))
readme.checkOverwriteReadme = jest.fn(() => Promise.resolve(true))
readme.writeReadme = jest.fn()
utils.showEndMessage = jest.fn()

Expand Down
10 changes: 9 additions & 1 deletion src/readme.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const fs = require('fs')
const { isNil, unescape } = require('lodash')

const chooseTemplate = require('./choose-template')
const askOverwriteReadme = require('./ask-overwrite')

const README_PATH = 'README.md'

Expand Down Expand Up @@ -95,9 +96,16 @@ const getReadmeTemplatePath = async (customTemplate, useDefaultAnswers) => {
return templatePath
}

/**
* Check if readme generator can overwrite the existed readme
*/
const checkOverwriteReadme = () =>
!fs.existsSync(README_PATH) || askOverwriteReadme()

module.exports = {
writeReadme,
buildReadmeContent,
README_PATH,
getReadmeTemplatePath
getReadmeTemplatePath,
checkOverwriteReadme
}
22 changes: 21 additions & 1 deletion src/readme.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require('fs')
const ora = require('ora')
const path = require('path')
const chooseTemplate = require('./choose-template')
const askOverwriteReadme = require('./ask-overwrite')

const defaultTemplatePath = path.resolve(__dirname, '../templates/default.md')
const defaultNoHtmlTemplatePath = path.resolve(
Expand All @@ -14,7 +15,8 @@ const {
writeReadme,
buildReadmeContent,
README_PATH,
getReadmeTemplatePath
getReadmeTemplatePath,
checkOverwriteReadme
} = require('./readme')

describe('readme', () => {
Expand Down Expand Up @@ -206,7 +208,25 @@ describe('readme', () => {
)
})
})

describe('checkOverwrite', () => {
it('should return true if README does not exist', async () => {
fs.existsSync = jest.fn(p => p !== README_PATH)
expect(await checkOverwriteReadme()).toEqual(true)
})
it('should return true if README exist and user want to overwrite it', async () => {
fs.existsSync = jest.fn(p => p === README_PATH)
askOverwriteReadme.mockResolvedValue(true)
expect(await checkOverwriteReadme()).toEqual(true)
})
it('should return false if README exist and user dont want to overwrite it', async () => {
fs.existsSync = jest.fn(p => p === README_PATH)
askOverwriteReadme.mockResolvedValue(false)
expect(await checkOverwriteReadme()).toEqual(false)
})
})
})

jest.mock('ora')
jest.mock('./choose-template')
jest.mock('./ask-overwrite')