Added help text, handle uncaught errors, added .npmignore

This commit is contained in:
Todd Bluhm 2016-08-17 00:55:16 -05:00
parent 56d10fa34f
commit 39c0760630
4 changed files with 49 additions and 9 deletions

5
.npmignore Normal file
View File

@ -0,0 +1,5 @@
.gitignore
coverage/
node_modules/
test/
*.yml

View File

@ -1,7 +1,10 @@
# Changelog
## 1.0.1
Badge Fix
- Fixed badges
- Added .npmignore
- Added help text to be printed out on certain errors
- Handled uncaught errors nicely
## 1.0.0
Initial release
- Initial release

View File

@ -4,6 +4,14 @@ const spawn = require('cross-spawn').spawn
const path = require('path')
const fs = require('fs')
process.on('uncaughtException', function (e) {
if (e.message.match(/passed/gi)) {
console.log(PrintHelp())
}
console.log(e.message)
process.exit(1)
})
function EnvCmd (args) {
const parsedArgs = ParseArgs(args)
const env = ParseEnvFile(parsedArgs.envFilePath)
@ -20,7 +28,7 @@ function EnvCmd (args) {
function ParseArgs (args) {
if (args.length < 3) {
throw new Error('Too few arguments passed to envCmd.')
throw new Error('Error! Too few arguments passed to env-cmd.')
}
const envFileFlags = /(^\-e$|^\-\-env$)/g
@ -40,7 +48,7 @@ function ParseArgs (args) {
}
if (!envFilePath) {
throw new Error('Error, No -e or --env flag passed.')
throw new Error('Error! No -e or --env flag passed.')
}
return {
@ -64,7 +72,7 @@ function ParseEnvFile (envFilePath) {
const equalSign = line.indexOf('=')
if (equalSign === -1) {
throw new Error(`Error, Malformed line in ${path.parse(envFilePath).base}.`)
throw new Error(`Error! Malformed line in ${path.parse(envFilePath).base}.`)
}
// Set then new env var
@ -73,8 +81,21 @@ function ParseEnvFile (envFilePath) {
return envs
}
function PrintHelp () {
return `
Usage: env-cmd -e [file] command [command options]
A simple application for running a cli application using an env config file
Options:
-e, --env Relative path to the env file
`
}
module.exports = {
EnvCmd,
ParseArgs,
ParseEnvFile
ParseEnvFile,
PrintHelp
}

View File

@ -20,6 +20,7 @@ const lib = proxyquire('../lib', {
const EnvCmd = lib.EnvCmd
const ParseArgs = lib.ParseArgs
const ParseEnvFile = lib.ParseEnvFile
const PrintHelp = lib.PrintHelp
describe('env-cmd', function () {
describe('ParseArgs', function () {
@ -49,7 +50,7 @@ describe('env-cmd', function () {
try {
ParseArgs(['-e', './test/envFile'])
} catch (e) {
assert(e.message === 'Too few arguments passed to envCmd.')
assert(e.message === 'Error! Too few arguments passed to env-cmd.')
return
}
assert(!'No exepection thrown')
@ -59,7 +60,7 @@ describe('env-cmd', function () {
try {
ParseArgs(['./test/envFile', 'command', 'cmda1', 'cmda2'])
} catch (e) {
assert(e.message === 'Error, No -e or --env flag passed.')
assert(e.message === 'Error! No -e or --env flag passed.')
return
}
assert(!'No exepection thrown')
@ -78,7 +79,7 @@ describe('env-cmd', function () {
try {
ParseEnvFile(path.join(__dirname, '/.env-malformed'))
} catch (e) {
assert(e.message === 'Error, Malformed line in .env-malformed.')
assert(e.message === 'Error! Malformed line in .env-malformed.')
return
}
assert(!'No exepection thrown')
@ -98,4 +99,14 @@ describe('env-cmd', function () {
assert(spawnStub.args[0][2].env.NICE === '42')
})
})
describe('PrintHelp', function () {
it('should return help text when run', function () {
const helpText = PrintHelp()
assert(typeof helpText === 'string')
assert(helpText.match(/Usage/g).length !== 0)
assert(helpText.match(/env-cmd/).length !== 0)
assert(helpText.match(/-e/).length !== 0)
})
})
})