Skip to content

Commit

Permalink
Merge pull request #84 from nmrony/feature/82-add-nice-spinner
Browse files Browse the repository at this point in the history
feat(cli): beautify cli output and add a spinner
  • Loading branch information
Nur Rony authored Jan 4, 2019
2 parents 8348539 + 16acf32 commit 564fa06
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 40 deletions.
71 changes: 64 additions & 7 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
"@babel/polyfill": "^7.2.5",
"async-each": "^1.0.1",
"async-waterfall": "^0.1.5",
"chalk": "^2.4.1",
"git-url-parse": "^11.1.1",
"lodash.map": "^4.6.0",
"lodash.omit": "^4.5.0",
"ora": "^3.0.0",
"path-exists": "^3.0.0",
"shelljs": "^0.8.3",
"yargs": "7.1.0"
Expand Down
8 changes: 6 additions & 2 deletions src/gitops/clone.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import ora from 'ora'
import shell from 'shelljs'
import { log, prepareArguments } from './../libs/utils'
import { prepareArguments } from './../libs/utils'

/**
* Build clone operation arguments and runs it
* @param {Object} argv Arguments passed by user
* @param {Function} done callback function
*/
export default function clone (argv, done) {
const spinner = ora('Cloning your repository').start()
const repoNPath = argv._[1] + ' ' + (typeof argv._[2] === 'undefined' ? '' : argv._[2])
const args = argv.b ? `-b ${argv.b}${argv.d ? ' -v' : ''}` : prepareArguments(argv)
log.info('cloning gtni your repository...')
shell.exec(
`git clone ${args} ${repoNPath}`,
{
Expand All @@ -17,8 +19,10 @@ export default function clone (argv, done) {
},
(exitCode, output, errOutput) => {
if (!exitCode) {
spinner.succeed('Cloning repository is completed successfully')
return done(null, output)
}
spinner.fail('Cloning repository is not completed successfully')
return done(exitCode, errOutput)
}
)
Expand Down
15 changes: 6 additions & 9 deletions src/gitops/fetch.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import ora from 'ora'
import shell from 'shelljs'
import { isGitRepo, log, prepareArguments } from './../libs/utils'

import { isGitRepo, prepareArguments } from './../libs/utils'
/**
* Build fetch operation arguments and runs it
* @param {Object} argv Arguments passed by user
* @param {Function} done callback function
*/
export default function fetch (argv, done) {
let args = ''
let cmd = ''
const branchToFetch = argv.b || false
const repoToFetch = argv.repo || false

if (isGitRepo()) {
log.info(`fetching ${!branchToFetch ? 'current' : branchToFetch} branch...`)

const spinner = ora(`Fetching ${!branchToFetch ? 'current' : branchToFetch} branch.`).start()
if (branchToFetch) {
args = (argv.d ? '-v ' : '') + 'origin ' + branchToFetch
} else if (repoToFetch) {
Expand All @@ -23,19 +21,18 @@ export default function fetch (argv, done) {
args = prepareArguments(argv)
}

cmd = 'git fetch ' + args

shell.exec(
cmd,
`git fetch ${args}`,
{
silent: true,
async: true
},
(exitCode, output, errOutput) => {
if (!exitCode) {
spinner.succeed(`Fetching ${!branchToFetch ? 'current' : branchToFetch} branch is successful.`)
return done(null, output)
}

spinner.fail(`Fetching ${!branchToFetch ? 'current' : branchToFetch} branch is failed.`)
return done(exitCode, errOutput)
}
)
Expand Down
11 changes: 6 additions & 5 deletions src/gitops/pull.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ora from 'ora'
import shell from 'shelljs'
import { isGitRepo, log, prepareArguments } from './../libs/utils'

import { isGitRepo, prepareArguments } from './../libs/utils'
/**
* Build pull operation arguments and runs it
* @param {Object} argv Arguments passed by user
Expand All @@ -12,7 +12,7 @@ export default function pull (argv, done) {
const repoToPull = argv.repo || false

if (isGitRepo()) {
log.info(`pulling ${!branchToPull ? 'current' : branchToPull} branch...`)
const spinner = ora(`Pulling ${!branchToPull ? 'current' : branchToPull} branch.`).start()

if (branchToPull) {
args = (argv.d ? '-v ' : '') + 'origin ' + branchToPull
Expand All @@ -23,16 +23,17 @@ export default function pull (argv, done) {
}

shell.exec(
'git pull ' + args,
`git pull ${args}`,
{
silent: true,
async: true
},
(exitCode, output, errOutput) => {
if (!exitCode) {
spinner.succeed(`Pulling ${!branchToPull ? 'current' : branchToPull} branch is successful.`)
return done(null, output)
}

spinner.succeed(`Pulling ${!branchToPull ? 'current' : branchToPull} branch is failed.`)
return done(exitCode, errOutput)
}
)
Expand Down
32 changes: 16 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import each from 'async-each'
import waterfall from 'async-waterfall'
import ora from 'ora'
import shell from 'shelljs'
import yargs from 'yargs'
import gitops from './gitops'
import { cloneSubCommands, fetchSubCommands, pullSubCommands } from './helpers'
import { checkOutBranch, currentBranchName, detectPackageManager, getRepoName, log, packagePaths } from './libs/utils'
import npmInstall from './npm'
import shellConfig from './shellconfig'

// User defined constants
const errorLog = []
const warningLog = []
Expand Down Expand Up @@ -39,6 +39,7 @@ shell.config = shellConfig
*/
function executeGitOperation (done) {
const command = argv._[0]
console.log()
switch (command) {
case 'pull':
return gitops.pull(argv, done)
Expand All @@ -59,23 +60,21 @@ function executeNPMInstall (done) {
const activeBranchName = currentBranchName()
const branchToCheckout = argv.b && typeof argv.b === 'string' && activeBranchName !== argv.b ? argv.b : false
const branchName = branchToCheckout ? checkOutBranch(branchToCheckout) : activeBranchName

log.info('listing all package.json files in this project...')

const listSpinner = ora('Listing all package.json files in this project').start()
const npmInstallSpinner = ora()
packagePaths(branchName, (error, packagePaths) => {
if (error) {
listSpinner.fail('Listing package.json is not successful.')
return done(error)
}

// is there any package.json?
if (!packagePaths.length) {
return done(
NO_PACKAGE_FOUND,
'No package.json not found in your project. ' + 'Skipping dependency installation.'
)
listSpinner.info('No package.json is found in your project. Skipping dependency installation.')
return done(NO_PACKAGE_FOUND)
}

log.info(`installing dependencies for branch ${branchName}`)
listSpinner.succeed(`Found ${packagePaths.length} package.json files.`)
npmInstallSpinner.start(`Installing dependencies for branch ${branchName}`)
each(
packagePaths,
(path, cb) => {
Expand All @@ -97,6 +96,7 @@ function executeNPMInstall (done) {
}

if (argv.d) {
console.log('')
log.info('Log for ' + path + 'package.json')
log.info(output)
}
Expand All @@ -117,13 +117,18 @@ function executeNPMInstall (done) {
}

if (warningLog.length) {
npmInstallSpinner.warn('Warnings given by npm during installing dependencies')
return done(null, HAS_WARNING)
}

if (errorLog.length) {
npmInstallSpinner.fail(
'Error given by package manager during installing dependencies. Please check npm-debug.log under given directory'
)
return done(null, HAS_ERROR)
}

npmInstallSpinner.succeed('Dependencies are installed successfully.')
return done(null, NO_ERROR)
}
)
Expand All @@ -139,7 +144,6 @@ function installNPMPackages (gitOpOutput, done) {
const cmd = argv._[0]
let cloneDir = ''

log.success('git ' + cmd + ' ends successfully!!')
if (argv.d) {
log.info(gitOpOutput)
}
Expand All @@ -162,18 +166,14 @@ waterfall([executeGitOperation, installNPMPackages], (err, cmdOutput) => {
}

if (cmdOutput === HAS_WARNING) {
log.info('warnings given by npm during installing dependencies')
warningLog.forEach(function iterateWarnings (warning) {
log.warn(warning.packagePath + '\r\n' + warning.messages.join('\r\n'))
console.log('')
})
}

if (cmdOutput === HAS_ERROR) {
log.info(`npm modules installation has finished with error(s).
Please check npm-debug.log file in reported package.json directory`)
return log.error(errorLog.join('\r\n'))
}

return log.success('all dependencies installed successfully!!!')
console.log()
})

0 comments on commit 564fa06

Please sign in to comment.