Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: warns the user appropriately for non-existent files #182

Merged
merged 7 commits into from
Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions start.js
Original file line number Diff line number Diff line change
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)
jamesgeorge007 marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
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
jamesgeorge007 marked this conversation as resolved.
Show resolved Hide resolved
// 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)
jamesgeorge007 marked this conversation as resolved.
Show resolved Hide resolved
}

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
jamesgeorge007 marked this conversation as resolved.
Show resolved Hide resolved
t.equal(warn, undefined)
t.equal(err.constructor, SyntaxError)
}

Expand Down