Verified Commit f51e8ffa authored by Aral Balkan's avatar Aral Balkan
Browse files

Closes #217: serve site root if started within a special subfolder

(Only if the site command is invoked without explicitly specifying a directory. Works for .dynamic, .hugo, .db., and .wildcard folders and their subfolders.
parent b457c84b
......@@ -12,6 +12,8 @@ This release implements a lot of small improvements, some of which have been lon
#### General:
- Running `site` without specifying a path while inside a special subfolder of your site (`.dynamic`, `.hugo`, `.wildcard`, `.db`) now magically does the right thing and serves the site root instead of the folder you’re in. If you really do want to serve one of these folders or a subfolder thereof, specifically state your intent by passing the current folder (`.`) as an argument.
- Status command now displays daemon details if daemon is enabled (#36).
- Status command now displays statistics URL if daemon is active (#232).
......
......@@ -197,7 +197,9 @@ Start serving the current directory at https://localhost as a regular process us
$ site
```
This is a shorthand for the full form of the `serve` command which, for the above example, is:
Note that if your current working directory is inside a special subfolder of your site (`.dynamic`, `.hugo`, `.wildcard`, `.db`) Site.js (as of version 15.4.0) magically does the right thing and serves the site root instead of the folder you’re in. If you really do want to serve one of these folders or a subfolder thereof, specifically state your intent by passing the current folder (`.`) as an argument.
The above caveat aside, the command above is a shorthand for the full form of the `serve` command:
```shell
$ site serve . @localhost:443
......
......@@ -21,6 +21,8 @@ const runtime = require('../lib/runtime')
const ensure = require('../lib/ensure')
const clr = require('../../lib/clr')
const Util = require('../../lib/Util')
const Site = require('../../index')
function enable (args) {
......@@ -69,13 +71,15 @@ function enable (args) {
//
// Create the systemd service unit.
//
const pathToServe = args.positional.length === 1 ? args.positional[0] : '.'
const _pathToServe = args.positional.length === 1 ? args.positional[0] : '.'
const binaryExecutable = '/usr/local/bin/site'
const sourceDirectory = path.resolve(__dirname, '..', '..')
const nodeExecutable = `node ${path.join(sourceDirectory, 'bin/site.js')}`
const executable = runtime.isBinary ? binaryExecutable : nodeExecutable
const absolutePathToServe = path.resolve(pathToServe)
// It is a common mistake to start the server in a .dynamic folder (or subfolder)
// or a .hugo folder or subfolder. In these cases, try to recover and do the right thing.
const {pathToServe, absolutePathToServe} = Util.magicallyRewritePathToServeIfNecessary(args.positional[0], _pathToServe)
// If there are aliase, we will add them to the configuration so they can
// be passed to the serve command when Site.js is started.
......
......@@ -110,7 +110,7 @@ function serve (args) {
// Add defaults for any arguments not provided.
global = global === null ? false : global
port = port === null ? 443 : port
path = path === null ? '.' : path
path = path === null ? null : path
//
// Parse named arguments.
......
......@@ -47,6 +47,7 @@ const cli = require('./bin/lib/cli')
const Stats = require('./lib/Stats')
const asyncForEach = require('./lib/async-foreach')
const errors = require('./lib/errors')
const Util = require('./lib/Util')
class Site {
......@@ -267,8 +268,15 @@ class Site {
if (typeof options.domain === 'string') {
Site.hostname = options.domain
}
this.pathToServe = typeof options.path === 'string' ? options.path : '.'
this.absolutePathToServe = path.resolve(this.pathToServe)
const _pathToServe = typeof options.path === 'string' ? options.path : '.'
// It is a common mistake to start the server in a .dynamic folder (or subfolder)
// or a .hugo folder or subfolder. In these cases, try to recover and do the right thing.
const {pathToServe, absolutePathToServe} = Util.magicallyRewritePathToServeIfNecessary(options.path, _pathToServe)
this.pathToServe = pathToServe
this.absolutePathToServe = absolutePathToServe
this.databasePath = path.join(this.absolutePathToServe, '.db')
this.port = typeof options.port === 'number' ? options.port : 443
this.global = typeof options.global === 'boolean' ? options.global : false
......
//////////////////////////////////////////////////////////////////////
//
// Util
//
// General utilities.
//
//////////////////////////////////////////////////////////////////////
const path = require('path')
const clr = require('../lib/clr')
class Util {
// It is a common mistake to start the server in a .dynamic folder (or subfolder)
// or a .hugo folder or subfolder, etc. In these cases, try to recover and do the right thing.
static magicallyRewritePathToServeIfNecessary (pathSpecified, pathToServe) {
// Only attempt to magically fix the path to serve (if necessary)
// if the current directory way not specifically requested by the person.
let absolutePathToServe = path.resolve(pathToServe)
if (pathSpecified !== '.' && pathToServe === '.') {
const specialFolders = /\.dynamic.*$|\.hugo.*$|\.db.*$|\.wildcard.*$/
const intelligentAbsolutePathToServe = absolutePathToServe.replace(specialFolders, '')
if (absolutePathToServe !== intelligentAbsolutePathToServe) {
pathToServe = path.relative(absolutePathToServe, intelligentAbsolutePathToServe)
absolutePathToServe = intelligentAbsolutePathToServe
console.log(` 🧙 ❨site.js❩ ${clr('Magically changed path to serve to the site root.', 'yellow')}`)
}
}
return {pathToServe, absolutePathToServe}
}
}
module.exports = Util
\ No newline at end of file
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