Skip to content

Commit

Permalink
✨ Add default value for github username if repos is github
Browse files Browse the repository at this point in the history
  • Loading branch information
Franck committed Jun 7, 2019
1 parent 67d611b commit a59520f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 22 deletions.
8 changes: 4 additions & 4 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const {
askProjectName,
askProjectDescription,
askAuhtorName,
askAuhtorGithub,
askAuhtorTwitter,
askAuthorGithub,
askAuthorTwitter,
askLicenseUrl,
askContributing
} = require('./questions')
Expand All @@ -25,8 +25,8 @@ const askQuestions = async () => {
askProjectName(projectInfos),
askProjectDescription(projectInfos),
askAuhtorName(projectInfos),
askAuhtorGithub(),
askAuhtorTwitter(),
askAuthorGithub(projectInfos),
askAuthorTwitter(),
askLicenseUrl(projectInfos),
askContributing(projectInfos)
]
Expand Down
5 changes: 3 additions & 2 deletions src/questions/author-github.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = () => ({
module.exports = projectInfos => ({
type: 'input',
message: '👤 Github username (use empty value to skip)',
name: 'authorGithubUsername'
name: 'authorGithubUsername',
default: projectInfos.githubUsername
})
8 changes: 4 additions & 4 deletions src/questions/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const askProjectName = require('./project-name')
const askProjectDescription = require('./project-description')
const askAuhtorName = require('./author-name')
const askAuhtorGithub = require('./author-github')
const askAuhtorTwitter = require('./author-twitter')
const askAuthorGithub = require('./author-github')
const askAuthorTwitter = require('./author-twitter')
const askLicenseUrl = require('./license-url')
const askContributing = require('./contributing')

module.exports = {
askProjectName,
askProjectDescription,
askAuhtorName,
askAuhtorGithub,
askAuhtorTwitter,
askAuthorGithub,
askAuthorTwitter,
askLicenseUrl,
askContributing
}
47 changes: 35 additions & 12 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ const isNil = require('lodash/isNil')
const exec = util.promisify(require('child_process').exec)
const getProjectName = require('project-name')

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

/**
* Clean repository url by removing '.git' and 'git+'
*
* @param {string} cleanReposUrl
* @param {string} reposUrl
*/
const cleanReposUrl = reposUrl => {
return reposUrl
const cleanReposUrl = reposUrl =>
reposUrl
.replace('\n', '')
.replace('git+', '')
.replace('.git', '')
}

/**
* Create readme file from the given readmeContent
Expand Down Expand Up @@ -48,7 +49,7 @@ const getPackageJson = async () => {
/**
* Get repository url from pakage json
*
* @param {string} reposUrl
* @param {Object} reposUrl
*/
const getReposUrlFromPackageJson = async packageJson => {
const reposUrl = get(packageJson, 'repository.url', undefined)
Expand All @@ -57,13 +58,11 @@ const getReposUrlFromPackageJson = async packageJson => {

/**
* Get repository url from git
*
* @param {string} reposUrl
*/
const getReposUrlFromGit = async () => {
try {
const url = await exec('git config --get remote.origin.url')
return cleanReposUrl(url.stdout)
const result = await exec('git config --get remote.origin.url')
return cleanReposUrl(result.stdout)
} catch (err) {
return undefined
}
Expand All @@ -72,7 +71,7 @@ const getReposUrlFromGit = async () => {
/**
* Get repository url from package.json or git
*
* @param {string} packageJson
* @param {Object} packageJson
*/
const getReposUrl = async packageJson =>
(await getReposUrlFromPackageJson(packageJson)) ||
Expand All @@ -81,7 +80,7 @@ const getReposUrl = async packageJson =>
/**
* Get repository issues url from package.json or git
*
* @param {string} packageJson
* @param {Object} packageJson
*/
const getReposIssuesUrl = async packageJson => {
let reposIssuesUrl = get(packageJson, 'bugs.url', undefined)
Expand All @@ -97,20 +96,44 @@ const getReposIssuesUrl = async packageJson => {
return reposIssuesUrl
}

/**
* Check if repository is a Github repository
*
* @param {string} repositoryUrl
*/
const isGithubRepository = repositoryUrl => repositoryUrl.includes(GITHUB_URL)

/**
* Get github username from repository url
*
* @param {string} repositoryUrl
*/
const getGithubUsernameFromRepositoryUrl = async repositoryUrl =>
repositoryUrl.replace(GITHUB_URL, '').split('/')[0]

/**
* Get project informations from git and package.json
*/
const getProjectInfos = async () => {
const packageJson = await getPackageJson()
const name = getProjectName() || undefined
const description = get(packageJson, 'description', undefined)
const author = get(packageJson, 'author', undefined)
const repositoryUrl = await getReposUrl(packageJson)
const contributingUrl = await getReposIssuesUrl(packageJson)
let githubUsername = undefined

if (!isNil(repositoryUrl) && isGithubRepository(repositoryUrl)) {
githubUsername = await getGithubUsernameFromRepositoryUrl(repositoryUrl)
}

return {
name,
description,
author,
repositoryUrl,
contributingUrl
contributingUrl,
githubUsername
}
}

Expand Down

0 comments on commit a59520f

Please sign in to comment.