Commit 6bb9827f authored by Aral Balkan's avatar Aral Balkan

Fixes #153: serve fails if path to serve does not exist

parent a28607b8
......@@ -152,6 +152,7 @@ function serve (args) {
process.exit(1)
} else {
const options = {
path,
port,
......@@ -161,7 +162,19 @@ function serve (args) {
}
// Start serving the site.
const site = new Site(options)
let site
try {
site = new Site(options)
} catch (error) {
if (error instanceof errors.InvalidPathToServeError) {
console.log(` 🤯 ${clr('Error:', 'red')} The path to serve ${clr(options.path, 'yellow')} does not exist.\n`)
process.exit(1)
} else {
// Rethrow
throwError(error)
}
}
const server = site.serve()
// Exit on known errors as we have already logged them to console.
......@@ -362,3 +375,4 @@ const ensure = require('../lib/ensure')
const status = require('../lib/status')
const tcpPortUsed = require('tcp-port-used')
const clr = require('../../lib/clr')
const errors = require('../../lib/errors')
......@@ -40,6 +40,8 @@ const childProcess = require('child_process')
const getRoutes = require('@ind.ie/web-routes-from-files')
const Stats = require('./lib/Stats')
const errors = require('./lib/errors')
class Site {
// Emitted when the address the server is trying to use is already in use by a different process on the system.
......@@ -103,6 +105,7 @@ class Site {
// object are optional. Check and populate the defaults.
if (options === undefined) options = {}
this.pathToServe = typeof options.path === 'string' ? options.path : '.'
this.absolutePathToServe = path.resolve(this.pathToServe)
this.port = typeof options.port === 'number' ? options.port : 443
this.global = typeof options.global === 'boolean' ? options.global : false
this.aliases = Array.isArray(options.aliases) ? options.aliases : []
......@@ -181,6 +184,11 @@ class Site {
// Middleware and routes that are unique to regular sites
// (not used on proxy servers).
configureAppRoutes () {
// Ensure that the requested path to serve actually exists.
if (!fs.existsSync(this.absolutePathToServe)) {
throw new errors.InvalidPathToServeError(`Path ${this.pathToServe} does not exist.`)
}
this.add4042302Support()
this.addCustomErrorPagesSupport()
......@@ -770,7 +778,7 @@ class Site {
// my-lovely-site-archive-1, etc.
appAddArchiveCascade () {
const archiveCascade = []
const absolutePathToServe = path.resolve(this.pathToServe)
const absolutePathToServe = this.absolutePathToServe
// (Windows uses forward slashes in paths so write the RegExp accordingly for that platform.)
const pathName = process.platform === 'win32' ? absolutePathToServe.match(/.*\\(.*?)$/)[1] : absolutePathToServe.match(/.*\/(.*?)$/)[1]
......
class InvalidPathToServeError extends Error {};
module.exports = {
InvalidPathToServeError
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment