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

Add restart command

parent 3577d762
......@@ -376,6 +376,8 @@ __Key:__ `[]` = optional   `|` = or
- `stop`: Stop server daemon.
- `restart`: Restart server daemon.
- `logs`: Display and tail server logs.
- `status`: Display detailed server information.
......
......@@ -2,7 +2,7 @@
//
// Command: disable
//
// Disables the web server daemon (stops it and removes it
// Disables the Site.js daemon (stops it and removes it
// from startup items).
//
//////////////////////////////////////////////////////////////////////
......
......@@ -49,6 +49,7 @@ function help () {
const commandDisable = command('disable')
const commandStart = command('start')
const commandStop = command('stop')
const commandRestart = command('restart')
const commandLogs = command('logs')
const commandStatus = command('status')
......@@ -83,7 +84,7 @@ function help () {
${prompt} ${clr(appName, 'bold')} [${usageCommand}] [${usageFolderOrPort}] [${usageHostAndPort}] [${usageOptions}]
${usageCommand}\t\t${commandServe}${systemdExists ? ` | ${commandEnable} | ${commandDisable} | ${commandStart} | ${commandStop} | ${commandLogs} | ${commandStatus}` : ''} | ${commandUpdate} | ${commandUninstall} | ${commandVersion} | ${commandHelp}
${usageCommand}\t\t${commandServe}${systemdExists ? ` | ${commandEnable} | ${commandDisable} | ${commandStart} | ${commandStop} | ${commandRestart} | ${commandLogs} | ${commandStatus}` : ''} | ${commandUpdate} | ${commandUninstall} | ${commandVersion} | ${commandHelp}
${usageFolderOrPort}\tPath of folder to serve (defaults to current folder) or port on localhost to proxy.
${usageHostAndPort}\tHost (and, optionally port) to sync. Valid hosts are @localhost and @hostname.
${usageOptions}\t\tSettings that alter command behaviour.
......@@ -106,6 +107,7 @@ function help () {
${commandDisable}\tStop server daemon and remove from startup.
${commandStart}\tStart server as daemon with globally-trusted certificates.
${commandStop}\tStop server daemon.
${commandRestart}\tRestart server daemon.
${commandLogs}\tDisplay and tail server logs.
${commandStatus}\tDisplay detailed server information.
` : ''}
......
//////////////////////////////////////////////////////////////////////
//
// Command: restart
//
// Restarts the Site.js daemon.
//
//////////////////////////////////////////////////////////////////////
const _restart = require('../lib/restart')
const ensure = require('../lib/ensure')
const Site = require('../../index')
function restart () {
ensure.systemctl()
ensure.root('restart')
Site.logAppNameAndVersion()
try {
// Start the web server.
_restart()
} catch (error) {
process.exit(1)
}
}
module.exports = restart
......@@ -2,7 +2,7 @@
//
// Command: start
//
// Starts the web server daemon.
// Starts the Site.js daemon.
//
//////////////////////////////////////////////////////////////////////
......
......@@ -2,7 +2,7 @@
//
// Command: stop
//
// Stops the web server daemon.
// Stops the Site.js daemon.
//
//////////////////////////////////////////////////////////////////////
......
......@@ -24,6 +24,7 @@ class CommandLineInterface {
['disable', arg0 === 'disable'],
['start', arg0 === 'start'],
['stop', arg0 === 'stop'],
['restart', arg0 === 'restart'],
['logs', arg0 === 'logs'],
['status', arg0 === 'status'],
......
//////////////////////////////////////////////////////////////////////
//
// Function: restart
//
// Restarts the Site.js server daemon.
//
//////////////////////////////////////////////////////////////////////
const childProcess = require('child_process')
const status = require('../lib/status')
function throwError(errorMessage) {
console.log(` 👿 Error: ${errorMessage}\n`)
throw new Error(errorMessage)
}
function restart () {
// Note: Ensure that systemctl exists and app is root before calling this function.
const { isEnabled } = status()
if (!isEnabled) {
throwError('Site.js daemon is not enabled. Please run site enable to enable it.')
}
// Note: we mirror systemctl’s behaviour: even if the service is stopped, running
// ===== restart will start it instead of throwing an error. That’s why we don’t check
// if the server is running here.
try {
// Restart the web server.
childProcess.execSync('sudo systemctl restart site.js', {env: process.env, stdio: 'pipe'})
} catch (error) {
throwError(`Could not restart Site.js server (${error}).`)
}
console.log(' 🎈 Server restarted.\n')
}
module.exports = restart
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