Skip to content

Commit

Permalink
✨ Add log infos during process
Browse files Browse the repository at this point in the history
  • Loading branch information
Franck committed Jun 8, 2019
1 parent d8c8e45 commit 912c602
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 5 deletions.
141 changes: 140 additions & 1 deletion package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"readme": "src/index.js"
},
"dependencies": {
"boxen": "^4.0.0",
"date-fns": "^1.30.1",
"ejs": "^2.6.1",
"eslint": "^5.3.0",
Expand All @@ -15,6 +16,7 @@
"inquirer": "^6.3.1",
"load-json-file": "^6.0.0",
"lodash": "^4.17.11",
"ora": "^3.4.0",
"prettier": "^1.17.1",
"project-name": "^1.0.0",
"yargs": "^13.2.4"
Expand Down
31 changes: 28 additions & 3 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const path = require('path')
const inquirer = require('inquirer')
const { isNil } = require('lodash')
const getYear = require('date-fns/get_year')
const boxen = require('boxen')
const { getProjectInfos } = require('./utils')

const { getTemplate, createReadme } = require('./utils')
Expand All @@ -23,8 +24,7 @@ const {
/**
* Ask user questions and return context to generate a README
*/
const askQuestions = async () => {
const projectInfos = await getProjectInfos()
const askQuestions = async projectInfos => {
let answersContext = {
isGithubRepos: projectInfos.isGithubRepos,
repositoryUrl: projectInfos.repositoryUrl,
Expand Down Expand Up @@ -60,13 +60,36 @@ const askQuestions = async () => {
return answersContext
}

/**
* Display end message
*/
const displayEndMessage = () => {
process.stdout.write(
boxen(
`
README.md was successfully generated.
Thanks for using readme-md-generator !
`,
{
padding: 1,
margin: { top: 2, bottom: 3 },
borderColor: 'cyan',
align: 'center',
borderStyle: 'double'
}
)
)
}

module.exports = async args => {
const templatePath = path.resolve(
__dirname,
`../templates/${args.template}.md`
)

const template = await getTemplate(templatePath)
const context = await askQuestions()
const projectInfos = await getProjectInfos()
const context = await askQuestions(projectInfos)
const currentYear = getYear(new Date())

const readmeContent = ejs.render(template, {
Expand All @@ -76,4 +99,6 @@ module.exports = async args => {
})

await createReadme(readmeContent)

displayEndMessage()
}
18 changes: 17 additions & 1 deletion src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const get = require('lodash/get')
const isNil = require('lodash/isNil')
const exec = util.promisify(require('child_process').exec)
const getProjectName = require('project-name')
const ora = require('ora')

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

Expand Down Expand Up @@ -33,7 +34,18 @@ const createReadme = async readmeContent =>
*
* @param {string} templatePath
*/
const getTemplate = async templatePath => await readFile(templatePath, 'utf8')
const getTemplate = async templatePath => {
const spinner = ora('Loading README template').start()

try {
const template = await readFile(templatePath, 'utf8')
spinner.succeed('README template loaded')
return template
} catch (err) {
spinner.fail('README template loading fail')
throw err
}
}

/**
* Get package.json content
Expand Down Expand Up @@ -119,6 +131,8 @@ const getLicenseUrlFromGithubRepositoryUrl = repositoryUrl =>
* Get project informations from git and package.json
*/
const getProjectInfos = async () => {
const spinner = ora('Gathering project infos').start()

const packageJson = await getPackageJson()
const name = getProjectName() || undefined
const description = get(packageJson, 'description', undefined)
Expand All @@ -137,6 +151,8 @@ const getProjectInfos = async () => {
? getLicenseUrlFromGithubRepositoryUrl(repositoryUrl)
: undefined

spinner.succeed('Project infos gathered')

return {
name,
description,
Expand Down

0 comments on commit 912c602

Please sign in to comment.