diff --git a/src/cli.js b/src/cli.js index 164958c..2a5913f 100644 --- a/src/cli.js +++ b/src/cli.js @@ -9,8 +9,8 @@ const { askProjectName, askProjectDescription, askAuhtorName, - askAuhtorGithub, - askAuhtorTwitter, + askAuthorGithub, + askAuthorTwitter, askLicenseUrl, askContributing } = require('./questions') @@ -25,8 +25,8 @@ const askQuestions = async () => { askProjectName(projectInfos), askProjectDescription(projectInfos), askAuhtorName(projectInfos), - askAuhtorGithub(), - askAuhtorTwitter(), + askAuthorGithub(projectInfos), + askAuthorTwitter(), askLicenseUrl(projectInfos), askContributing(projectInfos) ] diff --git a/src/questions/author-github.js b/src/questions/author-github.js index 5b4deff..51f0e0b 100644 --- a/src/questions/author-github.js +++ b/src/questions/author-github.js @@ -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 }) diff --git a/src/questions/index.js b/src/questions/index.js index 6b84953..c8a1274 100644 --- a/src/questions/index.js +++ b/src/questions/index.js @@ -1,8 +1,8 @@ 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') @@ -10,8 +10,8 @@ module.exports = { askProjectName, askProjectDescription, askAuhtorName, - askAuhtorGithub, - askAuhtorTwitter, + askAuthorGithub, + askAuthorTwitter, askLicenseUrl, askContributing } diff --git a/src/utils/index.js b/src/utils/index.js index 7f2dcd1..602597e 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -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 @@ -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) @@ -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 } @@ -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)) || @@ -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) @@ -97,6 +96,24 @@ 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 @@ -104,13 +121,19 @@ const getProjectInfos = async () => { 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 } }