Skip to content

Commit

Permalink
chore: warns the user appropriately for non-existent files (#182)
Browse files Browse the repository at this point in the history
* Warn the user appropriately for non-existent files

start command

* Minor fix

* Tweaks

* refactor

* fix: linting errors

* Minor refactor

remove eslint disable comment

* Minor tweaks
  • Loading branch information
jamesgeorge007 authored and mcollina committed Jul 9, 2019
1 parent 57166c5 commit 53c4df7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
17 changes: 14 additions & 3 deletions start.js
Expand Up @@ -71,11 +71,15 @@ function start (args, cb) {
return runFastify(args, cb)
}

function stop (error) {
function stop (error, warn) {
if (error) {
console.log(error)
process.exit(1)
}
if (warn) {
console.log(warn)
process.exit(1)
}
process.exit()
}

Expand All @@ -86,9 +90,16 @@ function runFastify (args, cb) {

loadModules(opts)

var file = null
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()}`)
}

let file = null

try {
file = require(path.resolve(process.cwd(), opts._[0]))
file = require(filePath)
} catch (e) {
return module.exports.stop(e)
}
Expand Down
22 changes: 13 additions & 9 deletions test/start.test.js
Expand Up @@ -142,38 +142,42 @@ test('should error with a good timeout value', t => {
start.start(argv)
})

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

const oldStop = start.stop
t.tearDown(() => { start.stop = oldStop })
start.stop = function (err) {
t.ok(/Cannot find module.*not-found/.test(err.message), err.message)
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)
}

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

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

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

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

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

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

Expand Down

0 comments on commit 53c4df7

Please sign in to comment.