From d2ee432d5cd09b163efcfccf7e37354754eb589b Mon Sep 17 00:00:00 2001 From: Franck Date: Sat, 8 Jun 2019 14:43:44 +0200 Subject: [PATCH] :sparkles: Add license url and name auto detection --- README.md | 7 ++++-- src/cli.js | 42 +++++++++++++++++++++++------------ src/questions/index.js | 2 ++ src/questions/license-name.js | 6 +++++ src/questions/license-url.js | 16 ++++++++----- src/utils/index.js | 23 +++++++++++++------ templates/default.md | 13 +++++++---- 7 files changed, 77 insertions(+), 32 deletions(-) create mode 100644 src/questions/license-name.js diff --git a/README.md b/README.md index d8f5e3a..2e6d0ea 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,9 @@

+ + License: MIT +

> Generates beautiful README files from git config & package.json infos @@ -29,7 +32,7 @@ readme-cli ## Contributing -Contributions, issues and feature requests are welcome. Feel free to check [issues page](test) if you want to contribute. +Contributions, issues and feature requests are welcome. Feel free to check [issues page](https://github.com/kefranabg/readme-cli/issues) if you want to contribute. ## Author @@ -46,7 +49,7 @@ Please ⭐️ this repository if you like it. Copyright © 2019 [Franck Abgrall](https://github.com/kefranabg). -📜 This project is under [license](https://github.com/kefranabg/readme-cli/blob/master/LICENSE). +📜 This project is [MIT](https://github.com/kefranabg/readme-cli/blob/master/LICENSE) licensed. --- diff --git a/src/cli.js b/src/cli.js index 429c885..97bfa7a 100644 --- a/src/cli.js +++ b/src/cli.js @@ -15,7 +15,8 @@ const { askLicenseUrl, askContributing, askProjectVersion, - askProjectPrerequisites + askProjectPrerequisites, + askLicenseName } = require('./questions') /** @@ -23,20 +24,33 @@ const { */ const askQuestions = async () => { const projectInfos = await getProjectInfos() + let answersContext = {} + const questionFns = [ + askProjectName, + askProjectVersion, + askProjectDescription, + askAuhtorName, + askAuthorGithub, + askAuthorTwitter, + askProjectPrerequisites, + askLicenseName, + askLicenseUrl, + askContributing + ] - const questions = [ - askProjectName(projectInfos), - askProjectVersion(projectInfos), - askProjectDescription(projectInfos), - askAuhtorName(projectInfos), - askAuthorGithub(projectInfos), - askAuthorTwitter(), - askProjectPrerequisites(projectInfos), - askLicenseUrl(projectInfos), - askContributing(projectInfos) - ].filter(negate(isNil)) // Remove undefined items from array - - return inquirer.prompt(questions) + for (const questionFn of questionFns) { + const question = questionFn(projectInfos, answersContext) + + if (!isNil(question)) { + const currentAnswerContext = await inquirer.prompt([question]) + answersContext = { + ...answersContext, + ...currentAnswerContext + } + } + } + + return answersContext } module.exports = async args => { diff --git a/src/questions/index.js b/src/questions/index.js index 8e52051..f60b171 100644 --- a/src/questions/index.js +++ b/src/questions/index.js @@ -3,6 +3,7 @@ const askProjectDescription = require('./project-description') const askAuhtorName = require('./author-name') const askAuthorGithub = require('./author-github') const askAuthorTwitter = require('./author-twitter') +const askLicenseName = require('./license-name') const askLicenseUrl = require('./license-url') const askContributing = require('./contributing') const askProjectVersion = require('./project-version') @@ -14,6 +15,7 @@ module.exports = { askAuhtorName, askAuthorGithub, askAuthorTwitter, + askLicenseName, askLicenseUrl, askContributing, askProjectVersion, diff --git a/src/questions/license-name.js b/src/questions/license-name.js new file mode 100644 index 0000000..35ccb37 --- /dev/null +++ b/src/questions/license-name.js @@ -0,0 +1,6 @@ +module.exports = packageJson => ({ + type: 'input', + message: '🔒 License name (use empty value to skip)', + name: 'licenseName', + default: packageJson.licenseName +}) diff --git a/src/questions/license-url.js b/src/questions/license-url.js index 5cdbab1..1f2bd18 100644 --- a/src/questions/license-url.js +++ b/src/questions/license-url.js @@ -1,5 +1,11 @@ -module.exports = () => ({ - type: 'input', - message: '🔒 License url (use empty value to skip)', - name: 'licenseUrl' -}) +const isEmpty = require('lodash/isEmpty') + +module.exports = (projectInfos, answersContext) => + isEmpty(answersContext.licenseName) + ? undefined + : { + type: 'input', + message: '🔒 License url (use empty value to skip)', + name: 'licenseUrl', + default: projectInfos.licenseUrl + } diff --git a/src/utils/index.js b/src/utils/index.js index 7fbc64e..27d1a58 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -101,16 +101,20 @@ const getReposIssuesUrl = async packageJson => { * * @param {string} repositoryUrl */ -const isGithubRepository = repositoryUrl => repositoryUrl.includes(GITHUB_URL) +const isGithubRepository = repositoryUrl => + !isNil(repositoryUrl) && repositoryUrl.includes(GITHUB_URL) /** * Get github username from repository url * * @param {string} repositoryUrl */ -const getGithubUsernameFromRepositoryUrl = async repositoryUrl => +const getGithubUsernameFromRepositoryUrl = repositoryUrl => repositoryUrl.replace(GITHUB_URL, '').split('/')[0] +const getLicenseUrlFromGithubRepositoryUrl = repositoryUrl => + `${repositoryUrl}/blob/master/LICENSE` + /** * Get project informations from git and package.json */ @@ -121,12 +125,15 @@ const getProjectInfos = async () => { const engines = get(packageJson, 'engines', undefined) const author = get(packageJson, 'author', undefined) const version = get(packageJson, 'version', undefined) + const licenseName = get(packageJson, 'license', undefined) const repositoryUrl = await getReposUrl(packageJson) const contributingUrl = await getReposIssuesUrl(packageJson) - const githubUsername = - !isNil(repositoryUrl) && isGithubRepository(repositoryUrl) - ? await getGithubUsernameFromRepositoryUrl(repositoryUrl) - : undefined + const githubUsername = isGithubRepository(repositoryUrl) + ? getGithubUsernameFromRepositoryUrl(repositoryUrl) + : undefined + const licenseUrl = isGithubRepository(repositoryUrl) + ? getLicenseUrlFromGithubRepositoryUrl(repositoryUrl) + : undefined return { name, @@ -136,7 +143,9 @@ const getProjectInfos = async () => { repositoryUrl, contributingUrl, githubUsername, - engines + engines, + licenseName, + licenseUrl } } diff --git a/templates/default.md b/templates/default.md index e7a902a..11adf8d 100644 --- a/templates/default.md +++ b/templates/default.md @@ -3,6 +3,11 @@ <% if (projectVersion) { -%> <% } -%> +<% if (licenseName && licenseUrl) { -%> + + License: <%= licenseName %> + +<% } -%>

<% if (projectDescription) { -%> @@ -32,11 +37,11 @@ npm run start ```sh npm run test ``` -<% if (licenseUrl) { -%> +<% if (contributingUrl) { -%> ## Contributing -Contributions, issues and feature requests are welcome. Feel free to check [issues page](<%= licenseUrl %>) if you want to contribute. +Contributions, issues and feature requests are welcome. Feel free to check [issues page](<%= contributingUrl %>) if you want to contribute. <% } -%> <% if (authorName || authorTwitterUsername || authorGithubUsername) { -%> @@ -55,14 +60,14 @@ Contributions, issues and feature requests are welcome. Feel free to check [issu ## Show your support Please ⭐️ this repository if you like it. -<% if (licenseUrl) { -%> +<% if (licenseName && licenseUrl) { -%> ## License <% if (authorName && authorGithubUsername) { -%> Copyright © <%= currentYear %> [<%= authorName %>](https://github.com/<%= authorGithubUsername %>). <% } %> -📜 This project is under [license](<%= licenseUrl %>). +📜 This project is [<%= licenseName %>](<%= licenseUrl %>) licensed. <% } -%> ***