Skip to content

Commit

Permalink
chore: Make stop more readable (#190)
Browse files Browse the repository at this point in the history
* chore: make more readable for stop function

* chore: make more readable for stop function
  • Loading branch information
xtx1130 authored and mcollina committed Aug 1, 2019
1 parent 1a11528 commit be22ce2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
15 changes: 7 additions & 8 deletions start.js
Expand Up @@ -71,13 +71,12 @@ function start (args, cb) {
return runFastify(args, cb)
}

function stop (error, warn) {
if (error) {
console.log(error)
function stop (message) {
if (message instanceof Error) {
console.log(message)
process.exit(1)
}
if (warn) {
console.log(warn)
} else if (message) {
console.log(`Warn: ${message}`)
process.exit(1)
}
process.exit()
Expand All @@ -93,7 +92,7 @@ function runFastify (args, cb) {
const filePath = path.resolve(process.cwd(), opts._[0])

if (!fs.existsSync(filePath)) {
return module.exports.stop(null, `${opts._[0]} doesn't exist within ${process.cwd()}`)
return module.exports.stop(`${opts._[0]} doesn't exist within ${process.cwd()}`)
}

let file = null
Expand All @@ -106,7 +105,7 @@ function runFastify (args, cb) {

if (file.length !== 3 && file.constructor.name === 'Function') {
return module.exports.stop(new Error('Plugin function should contain 3 arguments. Refer to ' +
'documentation for more information.'))
'documentation for more information.'))
}
if (file.length !== 2 && file.constructor.name === 'AsyncFunction') {
return module.exports.stop(new Error('Async/Await plugin function should contain 2 arguments.' +
Expand Down
18 changes: 7 additions & 11 deletions test/start.test.js
Expand Up @@ -143,27 +143,24 @@ test('should error with a good timeout value', t => {
})

test('should warn on file not found', t => {
t.plan(2)
t.plan(1)

const oldStop = start.stop
t.tearDown(() => { start.stop = oldStop })
start.stop = function (err, warn) { // eslint-disable-line
// test case changes
t.equal(err, null)
t.ok(/.*not-found.js doesn't exist within/.test(warn), warn)
start.stop = function (message) {
t.ok(/.*not-found.js doesn't exist within/.test(message), message)
}

const argv = ['-p', getPort(), './data/not-found.js']
start.start(argv)
})

test('should throw on package not found', t => {
t.plan(2)
t.plan(1)

const oldStop = start.stop
t.tearDown(() => { start.stop = oldStop })
start.stop = function (err, warn) {
t.equal(warn, undefined)
start.stop = function (err) {
t.ok(/Cannot find module 'unknown-package'/.test(err.message), err.message)
}

Expand All @@ -172,12 +169,11 @@ test('should throw on package not found', t => {
})

test('should throw on parsing error', t => {
t.plan(2)
t.plan(1)

const oldStop = start.stop
t.tearDown(() => { start.stop = oldStop })
start.stop = function (err, warn) { // eslint-disable-line
t.equal(warn, undefined)
start.stop = function (err) {
t.equal(err.constructor, SyntaxError)
}

Expand Down

0 comments on commit be22ce2

Please sign in to comment.