Skip to content

Commit

Permalink
✨ Add Author Github homepage url if exists (fixes kefranabg#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
anku255 authored and kefranabg committed Oct 13, 2019
1 parent 5b8bc08 commit 19b4946
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 5 deletions.
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"load-json-file": "^6.0.0",
"lodash": "^4.17.11",
"ora": "4.0.1",
"node-fetch": "^2.6.0",
"yargs": "^14.0.0"
},
"devDependencies": {
Expand Down
6 changes: 6 additions & 0 deletions src/__snapshots__/readme.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ npm run test
👤 **Franck Abgrall**
* Homepage: https://www.franck-abgrall.me/
* Twitter: [@FranckAbgrall](https://twitter.com/FranckAbgrall)
* Github: [@kefranabg](https://github.com/kefranabg)
Expand Down Expand Up @@ -122,6 +125,9 @@ npm run test
👤 **Franck Abgrall**
* Homepage: https://www.franck-abgrall.me/
* Twitter: [@FranckAbgrall](https://twitter.com/FranckAbgrall)
* Github: [@kefranabg](https://github.com/kefranabg)
Expand Down
18 changes: 17 additions & 1 deletion src/project-infos.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ const has = require('lodash/has')
const ora = require('ora')
const { execSync } = require('child_process')

const { getPackageJson, getProjectName } = require('./utils')
const {
getPackageJson,
getProjectName,
getAuthorHomepageFromGithubAPI
} = require('./utils')

const GITHUB_URL = 'https://github.com/'

Expand Down Expand Up @@ -84,6 +88,14 @@ const isGithubRepository = repositoryUrl =>
const getGithubUsernameFromRepositoryUrl = repositoryUrl =>
repositoryUrl.replace(GITHUB_URL, '').split('/')[0]

/**
* Get author's homepage from github username
*
* @param {string} githubUsername
*/
const getAuthorHomepageFromGithubUsername = async githubUsername =>
getAuthorHomepageFromGithubAPI(githubUsername)

/**
* Get license url from github repository url
*
Expand Down Expand Up @@ -140,6 +152,9 @@ const getProjectInfos = async () => {
const githubUsername = isGithubRepos
? getGithubUsernameFromRepositoryUrl(repositoryUrl)
: undefined
const authorHomepage = githubUsername
? await getAuthorHomepageFromGithubUsername(githubUsername)
: undefined
const licenseUrl = isGithubRepos
? getLicenseUrlFromGithubRepositoryUrl(repositoryUrl)
: undefined
Expand All @@ -151,6 +166,7 @@ const getProjectInfos = async () => {
description,
version,
author,
authorHomepage,
homepage,
repositoryUrl,
contributingUrl,
Expand Down
11 changes: 10 additions & 1 deletion src/project-infos.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ jest.mock('child_process', () => ({
}))
jest.mock('./utils', () => ({
getPackageJson: jest.fn(),
getProjectName: jest.fn(() => 'readme-md-generator')
getProjectName: jest.fn(() => 'readme-md-generator'),
getAuthorHomepageFromGithubAPI: jest.fn(
() => 'https://www.franck-abgrall.me/'
)
}))

const succeed = jest.fn()
Expand Down Expand Up @@ -70,6 +73,7 @@ describe('projectInfos', () => {
homepage: 'https://github.com/kefranabg/readme-md-generator',
contributingUrl:
'https://github.com/kefranabg/readme-md-generator/issues',
authorHomepage: 'https://www.franck-abgrall.me/',
githubUsername: 'kefranabg',
engines: {
npm: '>=5.5.0',
Expand Down Expand Up @@ -123,6 +127,7 @@ describe('projectInfos', () => {
'https://gitlab.com/kefranabg/readme-md-generator/issues',
homepage: 'https://gitlab.com/kefranabg/readme-md-generator',
githubUsername: undefined,
authorHomepage: undefined,
engines: {
npm: '>=5.5.0',
node: '>=9.3.0'
Expand Down Expand Up @@ -154,6 +159,7 @@ describe('projectInfos', () => {
'https://github.com/kefranabg/readme-md-generator/issues',
homepage: undefined,
githubUsername: 'kefranabg',
authorHomepage: 'https://www.franck-abgrall.me/',
engines: undefined,
licenseName: undefined,
licenseUrl:
Expand Down Expand Up @@ -210,6 +216,7 @@ describe('projectInfos', () => {
repositoryUrl: undefined,
contributingUrl: undefined,
homepage: undefined,
authorHomepage: undefined,
githubUsername: undefined,
engines: undefined,
licenseName: undefined,
Expand Down Expand Up @@ -258,6 +265,7 @@ describe('projectInfos', () => {
'https://github.com/kefranabg/readme-md-generator/issues',
homepage: 'https://github.com/kefranabg/readme-md-generator',
githubUsername: 'kefranabg',
authorHomepage: 'https://www.franck-abgrall.me/',
engines: {
npm: '>=5.5.0',
node: '>=9.3.0'
Expand Down Expand Up @@ -314,6 +322,7 @@ describe('projectInfos', () => {
contributingUrl:
'https://github.com/kefranabg/readme-md-generator/issues',
githubUsername: 'kefranabg',
authorHomepage: 'https://www.franck-abgrall.me/',
engines: {
npm: '>=5.5.0',
node: '>=9.3.0'
Expand Down
6 changes: 6 additions & 0 deletions src/questions/author-homepage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = projectInfos => ({
type: 'input',
message: '🏠 Author homepage (use empty value to skip)',
name: 'authorHomepage',
default: projectInfos.authorHomepage
})
17 changes: 17 additions & 0 deletions src/questions/author-homepage.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const askAuthorHomepage = require('./author-homepage')

describe('askAuthorHomepage', () => {
it('should return correct question format', () => {
const authorHomepage = 'authorHomepage'
const projectInfos = { authorHomepage }

const result = askAuthorHomepage(projectInfos)

expect(result).toEqual({
type: 'input',
message: '🏠 Author homepage (use empty value to skip)',
name: 'authorHomepage',
default: authorHomepage
})
})
})
1 change: 1 addition & 0 deletions src/questions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
askProjectDemoUrl: require('./project-demo-url'),
askProjectDocumentationUrl: require('./project-documentation-url'),
askAuhtorName: require('./author-name'),
askAuthorHomepage: require('./author-homepage'),
askAuthorGithub: require('./author-github'),
askAuthorTwitter: require('./author-twitter'),
askAuthorPatreon: require('./author-patreon'),
Expand Down
1 change: 1 addition & 0 deletions src/questions/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('questions', () => {
'askProjectDemoUrl',
'askProjectDocumentationUrl',
'askAuhtorName',
'askAuthorHomepage',
'askAuthorGithub',
'askAuthorTwitter',
'askAuthorPatreon',
Expand Down
1 change: 1 addition & 0 deletions src/readme.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ describe('readme', () => {
'https://github.com/kefranabg/readme-md-generator#readme',
projectDemoUrl: 'https://github.com/kefranabg/readme-md-generator#-demo',
authorName: 'Franck Abgrall',
authorHomepage: 'https://www.franck-abgrall.me/',
authorGithubUsername: 'kefranabg',
authorTwitterUsername: 'FranckAbgrall',
authorPatreonUsername: 'FranckAbgrall',
Expand Down
24 changes: 23 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ const loadJsonFile = require('load-json-file')
const boxen = require('boxen')
const path = require('path')
const getReposName = require('git-repo-name')
const fetch = require('node-fetch')
const { execSync } = require('child_process')

const END_MSG = `README.md was successfully generated.
Thanks for using readme-md-generator!`

const GITHUB_API_URL = 'https://api.github.com'

const BOXEN_CONFIG = {
padding: 1,
margin: { top: 2, bottom: 3 },
Expand Down Expand Up @@ -120,6 +123,24 @@ const getDefaultAnswers = questions =>
*/
const cleanSocialNetworkUsername = input => input.replace(/^@/, '')

/**
* Get author's homepage from Github API
*
* @param {string} githubUsername
* @returns {string} authorHomepage
*/
const getAuthorHomepageFromGithubAPI = async githubUsername => {
try {
const userData = await fetch(
`${GITHUB_API_URL}/users/${githubUsername}`
).then(res => res.json())
const authorHomepage = userData.blog
return authorHomepage === '' ? undefined : authorHomepage
} catch (err) {
return undefined
}
}

module.exports = {
getPackageJson,
showEndMessage,
Expand All @@ -129,5 +150,6 @@ module.exports = {
getDefaultAnswers,
getDefaultAnswer,
cleanSocialNetworkUsername,
isProjectAvailableOnNpm
isProjectAvailableOnNpm,
getAuthorHomepageFromGithubAPI
}
3 changes: 3 additions & 0 deletions templates/default-no-html.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
<% if (authorName) { %>
👤 **<%= authorName %>**
<% } %>
<% if (authorHomepage) { %>
* Homepage: <%= authorHomepage %>
<% } %>
<% if (authorTwitterUsername) { -%>
* Twitter: [@<%= authorTwitterUsername %>](https://twitter.com/<%= authorTwitterUsername %>)
<% } -%>
Expand Down
3 changes: 3 additions & 0 deletions templates/default.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@
<% if (authorName) { %>
👤 **<%= authorName %>**
<% } %>
<% if (authorHomepage) { %>
* Homepage: <%= authorHomepage %>
<% } %>
<% if (authorTwitterUsername) { -%>
* Twitter: [@<%= authorTwitterUsername %>](https://twitter.com/<%= authorTwitterUsername %>)
<% } -%>
Expand Down

0 comments on commit 19b4946

Please sign in to comment.