Skip to content

Commit 16acf32

Browse files
committed
feat(cli): beautify cli output and add a spinner
"closes #82"
1 parent 8348539 commit 16acf32

File tree

6 files changed

+99
-40
lines changed

6 files changed

+99
-40
lines changed

package-lock.json

Lines changed: 64 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535
"@babel/polyfill": "^7.2.5",
3636
"async-each": "^1.0.1",
3737
"async-waterfall": "^0.1.5",
38-
"chalk": "^2.4.1",
3938
"git-url-parse": "^11.1.1",
4039
"lodash.map": "^4.6.0",
4140
"lodash.omit": "^4.5.0",
41+
"ora": "^3.0.0",
4242
"path-exists": "^3.0.0",
4343
"shelljs": "^0.8.3",
4444
"yargs": "7.1.0"

src/gitops/clone.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
import ora from 'ora'
12
import shell from 'shelljs'
2-
import { log, prepareArguments } from './../libs/utils'
3+
import { prepareArguments } from './../libs/utils'
4+
35
/**
46
* Build clone operation arguments and runs it
57
* @param {Object} argv Arguments passed by user
68
* @param {Function} done callback function
79
*/
810
export default function clone (argv, done) {
11+
const spinner = ora('Cloning your repository').start()
912
const repoNPath = argv._[1] + ' ' + (typeof argv._[2] === 'undefined' ? '' : argv._[2])
1013
const args = argv.b ? `-b ${argv.b}${argv.d ? ' -v' : ''}` : prepareArguments(argv)
11-
log.info('cloning gtni your repository...')
1214
shell.exec(
1315
`git clone ${args} ${repoNPath}`,
1416
{
@@ -17,8 +19,10 @@ export default function clone (argv, done) {
1719
},
1820
(exitCode, output, errOutput) => {
1921
if (!exitCode) {
22+
spinner.succeed('Cloning repository is completed successfully')
2023
return done(null, output)
2124
}
25+
spinner.fail('Cloning repository is not completed successfully')
2226
return done(exitCode, errOutput)
2327
}
2428
)

src/gitops/fetch.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1+
import ora from 'ora'
12
import shell from 'shelljs'
2-
import { isGitRepo, log, prepareArguments } from './../libs/utils'
3-
3+
import { isGitRepo, prepareArguments } from './../libs/utils'
44
/**
55
* Build fetch operation arguments and runs it
66
* @param {Object} argv Arguments passed by user
77
* @param {Function} done callback function
88
*/
99
export default function fetch (argv, done) {
1010
let args = ''
11-
let cmd = ''
1211
const branchToFetch = argv.b || false
1312
const repoToFetch = argv.repo || false
1413

1514
if (isGitRepo()) {
16-
log.info(`fetching ${!branchToFetch ? 'current' : branchToFetch} branch...`)
17-
15+
const spinner = ora(`Fetching ${!branchToFetch ? 'current' : branchToFetch} branch.`).start()
1816
if (branchToFetch) {
1917
args = (argv.d ? '-v ' : '') + 'origin ' + branchToFetch
2018
} else if (repoToFetch) {
@@ -23,19 +21,18 @@ export default function fetch (argv, done) {
2321
args = prepareArguments(argv)
2422
}
2523

26-
cmd = 'git fetch ' + args
27-
2824
shell.exec(
29-
cmd,
25+
`git fetch ${args}`,
3026
{
3127
silent: true,
3228
async: true
3329
},
3430
(exitCode, output, errOutput) => {
3531
if (!exitCode) {
32+
spinner.succeed(`Fetching ${!branchToFetch ? 'current' : branchToFetch} branch is successful.`)
3633
return done(null, output)
3734
}
38-
35+
spinner.fail(`Fetching ${!branchToFetch ? 'current' : branchToFetch} branch is failed.`)
3936
return done(exitCode, errOutput)
4037
}
4138
)

src/gitops/pull.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import ora from 'ora'
12
import shell from 'shelljs'
2-
import { isGitRepo, log, prepareArguments } from './../libs/utils'
3-
3+
import { isGitRepo, prepareArguments } from './../libs/utils'
44
/**
55
* Build pull operation arguments and runs it
66
* @param {Object} argv Arguments passed by user
@@ -12,7 +12,7 @@ export default function pull (argv, done) {
1212
const repoToPull = argv.repo || false
1313

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

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

2525
shell.exec(
26-
'git pull ' + args,
26+
`git pull ${args}`,
2727
{
2828
silent: true,
2929
async: true
3030
},
3131
(exitCode, output, errOutput) => {
3232
if (!exitCode) {
33+
spinner.succeed(`Pulling ${!branchToPull ? 'current' : branchToPull} branch is successful.`)
3334
return done(null, output)
3435
}
35-
36+
spinner.succeed(`Pulling ${!branchToPull ? 'current' : branchToPull} branch is failed.`)
3637
return done(exitCode, errOutput)
3738
}
3839
)

src/index.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
import each from 'async-each'
44
import waterfall from 'async-waterfall'
5+
import ora from 'ora'
56
import shell from 'shelljs'
67
import yargs from 'yargs'
78
import gitops from './gitops'
89
import { cloneSubCommands, fetchSubCommands, pullSubCommands } from './helpers'
910
import { checkOutBranch, currentBranchName, detectPackageManager, getRepoName, log, packagePaths } from './libs/utils'
1011
import npmInstall from './npm'
1112
import shellConfig from './shellconfig'
12-
1313
// User defined constants
1414
const errorLog = []
1515
const warningLog = []
@@ -39,6 +39,7 @@ shell.config = shellConfig
3939
*/
4040
function executeGitOperation (done) {
4141
const command = argv._[0]
42+
console.log()
4243
switch (command) {
4344
case 'pull':
4445
return gitops.pull(argv, done)
@@ -59,23 +60,21 @@ function executeNPMInstall (done) {
5960
const activeBranchName = currentBranchName()
6061
const branchToCheckout = argv.b && typeof argv.b === 'string' && activeBranchName !== argv.b ? argv.b : false
6162
const branchName = branchToCheckout ? checkOutBranch(branchToCheckout) : activeBranchName
62-
63-
log.info('listing all package.json files in this project...')
64-
63+
const listSpinner = ora('Listing all package.json files in this project').start()
64+
const npmInstallSpinner = ora()
6565
packagePaths(branchName, (error, packagePaths) => {
6666
if (error) {
67+
listSpinner.fail('Listing package.json is not successful.')
6768
return done(error)
6869
}
6970

7071
// is there any package.json?
7172
if (!packagePaths.length) {
72-
return done(
73-
NO_PACKAGE_FOUND,
74-
'No package.json not found in your project. ' + 'Skipping dependency installation.'
75-
)
73+
listSpinner.info('No package.json is found in your project. Skipping dependency installation.')
74+
return done(NO_PACKAGE_FOUND)
7675
}
77-
78-
log.info(`installing dependencies for branch ${branchName}`)
76+
listSpinner.succeed(`Found ${packagePaths.length} package.json files.`)
77+
npmInstallSpinner.start(`Installing dependencies for branch ${branchName}`)
7978
each(
8079
packagePaths,
8180
(path, cb) => {
@@ -97,6 +96,7 @@ function executeNPMInstall (done) {
9796
}
9897

9998
if (argv.d) {
99+
console.log('')
100100
log.info('Log for ' + path + 'package.json')
101101
log.info(output)
102102
}
@@ -117,13 +117,18 @@ function executeNPMInstall (done) {
117117
}
118118

119119
if (warningLog.length) {
120+
npmInstallSpinner.warn('Warnings given by npm during installing dependencies')
120121
return done(null, HAS_WARNING)
121122
}
122123

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

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

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

164168
if (cmdOutput === HAS_WARNING) {
165-
log.info('warnings given by npm during installing dependencies')
166169
warningLog.forEach(function iterateWarnings (warning) {
167170
log.warn(warning.packagePath + '\r\n' + warning.messages.join('\r\n'))
168171
console.log('')
169172
})
170173
}
171174

172175
if (cmdOutput === HAS_ERROR) {
173-
log.info(`npm modules installation has finished with error(s).
174-
Please check npm-debug.log file in reported package.json directory`)
175176
return log.error(errorLog.join('\r\n'))
176177
}
177-
178-
return log.success('all dependencies installed successfully!!!')
178+
console.log()
179179
})

0 commit comments

Comments
 (0)