diff --git a/src/__snapshots__/readme.spec.js.snap b/src/__snapshots__/readme.spec.js.snap index 1b73c86..56a6492 100644 --- a/src/__snapshots__/readme.spec.js.snap +++ b/src/__snapshots__/readme.spec.js.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`readme buildReadmeContent should return readme template content 1`] = ` +exports[`readme buildReadmeContent should return readme default template content 1`] = ` "
@@ -74,3 +74,70 @@ This project is [MIT](https://github.com/kefranabg/readme-md-generator/blob/mast
***
_This README was generated with ❤️ by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_"
`;
+
+exports[`readme buildReadmeContent should return readme default template no html content 1`] = `
+"# Welcome to readme-md-generator 👋
+![Version](https://img.shields.io/badge/version-0.1.3-blue.svg?cacheSeconds=2592000)
+![Prerequisite](https://img.shields.io/badge/npm-%3E%3D5.5.0-blue.svg)
+![Prerequisite](https://img.shields.io/badge/node-%3E%3D%209.3.0-blue.svg)
+[![Documentation](https://img.shields.io/badge/documentation-yes-brightgreen.svg)](https://github.com/kefranabg/readme-md-generator#readme)
+[![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://github.com/kefranabg/readme-md-generator/graphs/commit-activity)
+[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/kefranabg/readme-md-generator/blob/master/LICENSE)
+[![Twitter: FranckAbgrall](https://img.shields.io/twitter/follow/FranckAbgrall.svg?style=social)](https://twitter.com/FranckAbgrall)
+
+> Generates beautiful README files from git config & package.json infos
+
+### 🏠 [Homepage](https://github.com/kefranabg/readme-md-generator#readme)
+
+## Prerequisites
+
+- npm >=5.5.0
+- node >= 9.3.0
+
+## Install
+
+\`\`\`sh
+npm install
+\`\`\`
+
+## Usage
+
+\`\`\`sh
+npm start
+\`\`\`
+
+## Run tests
+
+\`\`\`sh
+npm run test
+\`\`\`
+
+## Author
+
+👤 **Franck Abgrall**
+
+* Twitter: [@FranckAbgrall](https://twitter.com/FranckAbgrall)
+* Github: [@kefranabg](https://github.com/kefranabg)
+
+## 🤝 Contributing
+
+Contributions, issues and feature requests are welcome!
+
+Feel free to check [issues page](https://github.com/kefranabg/readme-md-generator/issues).
+
+## Show your support
+
+Give a ⭐️ if this project helped you!
+
+[![support us](https://img.shields.io/badge/become-a patreon%20us-orange.svg?cacheSeconds=2592000)](https://www.patreon.com/FranckAbgrall)
+
+
+## 📝 License
+
+Copyright © 2019 [Franck Abgrall](https://github.com/kefranabg).
+
+This project is [MIT](https://github.com/kefranabg/readme-md-generator/blob/master/LICENSE) licensed.
+
+***
+_This README was generated with ❤️ by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_"
+`;
diff --git a/src/ask-questions.js b/src/ask-questions.js
index 34ac114..0594064 100644
--- a/src/ask-questions.js
+++ b/src/ask-questions.js
@@ -1,31 +1,21 @@
const inquirer = require('inquirer')
+const { flatMap } = require('lodash')
const questionsBuilders = require('./questions')
const utils = require('./utils')
-/**
- * Get questions
- *
- * @param {Object} projectInfos
- */
-const getQuestions = projectInfos =>
- Object.values(questionsBuilders).reduce(
- (questions, questionBuilder) => [
- ...questions,
- questionBuilder(projectInfos)
- ],
- []
- )
-
/**
* Ask user questions and return context to generate a README
*
* @param {Object} projectInfos
+ * @param {Boolean} useDefaultAnswers
*/
-module.exports = async (projectInfos, skipQuestions) => {
- const questions = getQuestions(projectInfos)
+module.exports = async (projectInfos, useDefaultAnswers) => {
+ const questions = flatMap(Object.values(questionsBuilders), questionBuilder =>
+ questionBuilder(projectInfos)
+ )
- const answersContext = skipQuestions
+ const answersContext = useDefaultAnswers
? utils.getDefaultAnswers(questions)
: await inquirer.prompt(questions)
diff --git a/src/ask-questions.spec.js b/src/ask-questions.spec.js
index 8c92796..c0a8999 100644
--- a/src/ask-questions.spec.js
+++ b/src/ask-questions.spec.js
@@ -40,7 +40,7 @@ describe('ask-questions', () => {
it('should call all builder functions exported by questions', async () => {
const projectInfos = { name: 'readme-md-generator' }
- await askQuestions(projectInfos)
+ await askQuestions(projectInfos, false)
expect(questions.askProjectName).toHaveBeenCalledTimes(1)
expect(questions.askProjectVersion).toHaveBeenCalledTimes(1)
@@ -66,7 +66,7 @@ describe('ask-questions', () => {
it('should return merged contexts', async () => {
const projectInfos = { name: 'readme-md-generator' }
- const context = await askQuestions(projectInfos)
+ const context = await askQuestions(projectInfos, false)
expect(context).toEqual({
projectName: 'value',
diff --git a/src/choose-template.js b/src/choose-template.js
new file mode 100644
index 0000000..064eb4d
--- /dev/null
+++ b/src/choose-template.js
@@ -0,0 +1,33 @@
+const inquirer = require('inquirer')
+const path = require('path')
+
+module.exports = async useDefaultAnswers => {
+ const defaultTemplate = path.resolve(__dirname, '../templates/default.md')
+ const defaultNoHtmlTemplate = path.resolve(
+ __dirname,
+ '../templates/default-no-html.md'
+ )
+
+ if (useDefaultAnswers) return defaultTemplate
+
+ const question = {
+ type: 'list',
+ message:
+ '🎨 Use HTML in your README.md for a nicer rendering? (not supported everywhere. ex: Bitbucket)',
+ name: 'templatePath',
+ choices: [
+ {
+ name: 'Yes ',
+ value: defaultTemplate
+ },
+ {
+ name: 'No',
+ value: defaultNoHtmlTemplate
+ }
+ ]
+ }
+
+ const { templatePath } = await inquirer.prompt([question])
+
+ return templatePath
+}
diff --git a/src/choose-template.spec.js b/src/choose-template.spec.js
new file mode 100644
index 0000000..4a1bb80
--- /dev/null
+++ b/src/choose-template.spec.js
@@ -0,0 +1,51 @@
+const inquirer = require('inquirer')
+const path = require('path')
+
+const chooseTemplate = require('./choose-template')
+
+const defaultTemplatePath = path.resolve(__dirname, '../templates/default.md')
+const defaultNoHtmlTemplatePath = path.resolve(
+ __dirname,
+ '../templates/default-no-html.md'
+)
+
+inquirer.prompt = jest.fn(() =>
+ Promise.resolve({ templatePath: defaultTemplatePath })
+)
+
+describe('choose-template', () => {
+ it('should return user choice', async () => {
+ const result = await chooseTemplate(false)
+
+ expect(result).toEqual(defaultTemplatePath)
+ })
+
+ it('should return default template', async () => {
+ const result = await chooseTemplate(true)
+
+ expect(result).toEqual(defaultTemplatePath)
+ })
+
+ it('should call prompt with correct parameters', async () => {
+ await chooseTemplate(false)
+
+ expect(inquirer.prompt).toHaveBeenNthCalledWith(1, [
+ {
+ type: 'list',
+ message:
+ '🎨 Use HTML in your README.md for a nicer rendering? (not supported everywhere. ex: Bitbucket)',
+ name: 'templatePath',
+ choices: [
+ {
+ name: 'Yes ',
+ value: defaultTemplatePath
+ },
+ {
+ name: 'No',
+ value: defaultNoHtmlTemplatePath
+ }
+ ]
+ }
+ ])
+ })
+})
diff --git a/src/cli.js b/src/cli.js
index cab5ab6..2cec288 100644
--- a/src/cli.js
+++ b/src/cli.js
@@ -1,21 +1,28 @@
const readme = require('./readme')
const infos = require('./project-infos')
-
const utils = require('./utils')
const askQuestions = require('./ask-questions')
/**
* Main process:
- * 1) Gather project infos
- * 2) Ask user questions
- * 3) Build README content
- * 4) Create README.md file
+ * 1) Get README template path
+ * 2) Gather project infos
+ * 3) Ask user questions
+ * 4) Build README content
+ * 5) Create README.md file
*
* @param {Object} args
*/
-module.exports = async ({ templatePath, yes }) => {
+module.exports = async ({ customTemplatePath, useDefaultAnswers }) => {
+ const templatePath = await readme.getReadmeTemplatePath(
+ customTemplatePath,
+ useDefaultAnswers
+ )
const projectInformations = await infos.getProjectInfos()
- const answersContext = await askQuestions(projectInformations, yes)
+ const answersContext = await askQuestions(
+ projectInformations,
+ useDefaultAnswers
+ )
const readmeContent = await readme.buildReadmeContent(
answersContext,
templatePath
diff --git a/src/cli.spec.js b/src/cli.spec.js
index dcd532f..d6a5a4a 100644
--- a/src/cli.spec.js
+++ b/src/cli.spec.js
@@ -44,21 +44,29 @@ describe('mainProcess', () => {
})
it('should call main functions with correct args', async () => {
- const templatePath = 'default'
+ const customTemplatePath = undefined
+ const useDefaultAnswers = true
const projectInformations = { name: 'readme-md-generator' }
const readmeContent = 'content'
+ const templatePath = 'path/to/template'
infos.getProjectInfos = jest.fn(() => Promise.resolve(projectInformations))
readme.buildReadmeContent = jest.fn(() => Promise.resolve(readmeContent))
+ readme.getReadmeTemplatePath = jest.fn(() => Promise.resolve(templatePath))
readme.writeReadme = jest.fn()
utils.showEndMessage = jest.fn()
- await mainProcess({ templatePath })
+ await mainProcess({ customTemplatePath, useDefaultAnswers })
+ expect(readme.getReadmeTemplatePath).toHaveBeenNthCalledWith(
+ 1,
+ customTemplatePath,
+ useDefaultAnswers
+ )
expect(infos.getProjectInfos).toHaveBeenCalledTimes(1)
expect(askQuestions).toHaveBeenNthCalledWith(
1,
projectInformations,
- undefined
+ useDefaultAnswers
)
expect(readme.buildReadmeContent).toHaveBeenNthCalledWith(
1,
@@ -68,19 +76,4 @@ describe('mainProcess', () => {
expect(readme.writeReadme).toHaveBeenNthCalledWith(1, readmeContent)
expect(utils.showEndMessage).toHaveBeenCalledTimes(1)
})
-
- it('should forward --yes option to askQuestions', async () => {
- const template = 'default'
- const projectInformations = { name: 'readme-md-generator' }
- const skipQuestions = true
- utils.showEndMessage = jest.fn()
-
- await mainProcess({ template, yes: skipQuestions })
-
- expect(askQuestions).toHaveBeenNthCalledWith(
- 1,
- projectInformations,
- skipQuestions
- )
- })
})
diff --git a/src/index.js b/src/index.js
index f7afa51..4ce4819 100755
--- a/src/index.js
+++ b/src/index.js
@@ -1,25 +1,16 @@
#!/usr/bin/env node
const yargs = require('yargs')
+const { noop } = require('lodash')
const mainProcess = require('./cli')
-const { getReadmeTemplatePath } = require('./readme')
yargs
.usage('Usage: $0