Skip to content

Commit

Permalink
♻️ Utils code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Franck committed Jun 7, 2019
1 parent d0f61f9 commit 97d675e
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 30 deletions.
14 changes: 7 additions & 7 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const ejs = require('ejs')
const path = require('path')
const inquirer = require('inquirer')
const getYear = require('date-fns/get_year')
const { getPackageJson } = require('./utils')
const { getProjectInfos } = require('./utils')

const { getTemplate, createReadme } = require('./utils')
const {
Expand All @@ -19,16 +19,16 @@ const {
* Ask user questions and return context to generate a README
*/
const askQuestions = async () => {
const packageJson = await getPackageJson()
const projectInfos = await getProjectInfos()

const questions = [
askProjectName(),
askProjectDescription(packageJson),
askAuhtorName(packageJson),
askProjectName(projectInfos),
askProjectDescription(projectInfos),
askAuhtorName(projectInfos),
askAuhtorGithub(),
askAuhtorTwitter(),
askLicenseUrl(),
await askContributing(packageJson)
askLicenseUrl(projectInfos),
askContributing(projectInfos)
]

return inquirer.prompt(questions)
Expand Down
6 changes: 2 additions & 4 deletions src/questions/author-name.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const get = require('lodash/get')

module.exports = packageJson => ({
module.exports = projectInfos => ({
type: 'input',
message: 'Enter your name',
name: 'authorName',
default: get(packageJson, 'author', undefined)
default: projectInfos.author
})
6 changes: 2 additions & 4 deletions src/questions/contributing.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const { getReposIssuesUrl } = require('../utils')

module.exports = async packageJson => ({
module.exports = packageJson => ({
type: 'input',
message: 'Enter the url of your issues (use empty value to skip)',
name: 'contributingUrl',
default: await getReposIssuesUrl(packageJson)
default: packageJson.contributingUrl
})
6 changes: 2 additions & 4 deletions src/questions/project-description.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const get = require('lodash/get')

module.exports = packageJson => ({
module.exports = projectInfos => ({
type: 'input',
message: 'Enter your project description',
name: 'projectDescription',
default: get(packageJson, 'description', undefined)
default: projectInfos.description
})
6 changes: 2 additions & 4 deletions src/questions/project-name.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const getProjectName = require('project-name')

module.exports = () => ({
module.exports = projectInfos => ({
type: 'input',
message: 'Enter your project name',
name: 'projectName',
default: getProjectName() || undefined
default: projectInfos.name
})
29 changes: 23 additions & 6 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ const loadJsonFile = require('load-json-file')
const get = require('lodash/get')
const isNil = require('lodash/isNil')
const exec = util.promisify(require('child_process').exec)
const getProjectName = require('project-name')

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

/**
Expand Down Expand Up @@ -93,12 +97,25 @@ const getReposIssuesUrl = async packageJson => {
return reposIssuesUrl
}

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)

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

module.exports = {
getPackageJson,
getTemplate,
createReadme,
getReposUrlFromPackageJson,
getReposUrlFromGit,
getReposUrl,
getReposIssuesUrl
getProjectInfos
}
2 changes: 1 addition & 1 deletion templates/default.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Contributions, issues and feature requests are welcome. Feel free to check [issu
Please ⭐️ this repository if you like it.
<% if (licenseUrl) { -%>

## License
## License

<% if (authorName && authorGithubUsername) { -%>
Copyright © <%= currentYear %> [<%= authorName %>](https://github.com/<%= authorGithubUsername %>).
Expand Down

0 comments on commit 97d675e

Please sign in to comment.