Skip to content

Commit

Permalink
✨ Add license url and name auto detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Franck committed Jun 8, 2019
1 parent 9924f72 commit d2ee432
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 32 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<p>
<img src="https://img.shields.io/badge/version-1.0.0-blue.svg?cacheSeconds=2592000" />
<img src="https://badges.greenkeeper.io/kefranabg/readme-cli.svg" />
<a href="https://github.com/kefranabg/readme-cli/blob/master/LICENSE">
<img alt="License: MIT" src="https://img.shields.io/badge/License-MIT-yellow.svg" target="_blank" />
</a>
</p>
> Generates beautiful README files from git config & package.json infos
Expand Down Expand Up @@ -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

Expand All @@ -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.

---

Expand Down
42 changes: 28 additions & 14 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,42 @@ const {
askLicenseUrl,
askContributing,
askProjectVersion,
askProjectPrerequisites
askProjectPrerequisites,
askLicenseName
} = require('./questions')

/**
* Ask user questions and return context to generate a README
*/
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 => {
Expand Down
2 changes: 2 additions & 0 deletions src/questions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -14,6 +15,7 @@ module.exports = {
askAuhtorName,
askAuthorGithub,
askAuthorTwitter,
askLicenseName,
askLicenseUrl,
askContributing,
askProjectVersion,
Expand Down
6 changes: 6 additions & 0 deletions src/questions/license-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = packageJson => ({
type: 'input',
message: '🔒 License name (use empty value to skip)',
name: 'licenseName',
default: packageJson.licenseName
})
16 changes: 11 additions & 5 deletions src/questions/license-url.js
Original file line number Diff line number Diff line change
@@ -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
}
23 changes: 16 additions & 7 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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,
Expand All @@ -136,7 +143,9 @@ const getProjectInfos = async () => {
repositoryUrl,
contributingUrl,
githubUsername,
engines
engines,
licenseName,
licenseUrl
}
}

Expand Down
13 changes: 9 additions & 4 deletions templates/default.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
<% if (projectVersion) { -%>
<img src="https://img.shields.io/badge/version-<%= projectVersion %>-blue.svg?cacheSeconds=2592000" />
<% } -%>
<% if (licenseName && licenseUrl) { -%>
<a href="<%= licenseUrl %>">
<img alt="License: <%= licenseName %>" src="https://img.shields.io/badge/License-<%= licenseName %>-yellow.svg" target="_blank" />
</a>
<% } -%>
</p>
<% if (projectDescription) { -%>

Expand Down Expand Up @@ -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) { -%>

Expand All @@ -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.
<% } -%>

***
Expand Down

0 comments on commit d2ee432

Please sign in to comment.