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 5 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
16 changes: 13 additions & 3 deletions start.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,14 @@ 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()
}

Expand All @@ -86,9 +89,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('', `${opts._[0]} doesn't exist within ${process.cwd()}`)
jamesgeorge007 marked this conversation as resolved.
Show resolved Hide resolved
}

let file = null

try {
file = require(path.resolve(process.cwd(), opts._[0]))
file = require(filePath)
} catch (e) {
return module.exports.stop(e)
}
Expand Down
11 changes: 6 additions & 5 deletions test/start.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ test('should throw on file not found', t => {

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.ok(/.*not-found.js doesn't exist within/.test(warn), warn)
}

const argv = [ '-p', getPort(), './data/not-found.js' ]
Expand All @@ -160,8 +161,8 @@ test('should throw on package not found', t => {

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) { // eslint-disable-line
jamesgeorge007 marked this conversation as resolved.
Show resolved Hide resolved
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' ]
Expand All @@ -173,7 +174,7 @@ test('should throw on parsing error', t => {

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(err.constructor, SyntaxError)
}

Expand Down