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

Add colour to the server status messages

parent c127d08b
......@@ -121,7 +121,29 @@ switch (true) {
// Status (proxy: systemctl status web-server)
case command.isStatus:
ensureSystemctl()
childProcess.spawn('systemctl', ['status', 'web-server'], {env: process.env, stdio: 'inherit'})
let isActive
try {
childProcess.execSync('systemctl is-active web-server', {env: process.env, stdio: 'pipe'})
isActive = true
} catch (error) {
isActive = false
}
let isEnabled
try {
childProcess.execSync('systemctl is-enabled web-server', {env: process.env, stdio: 'pipe'})
isEnabled = true
} catch (error) {
isEnabled = false
}
const activeState = isActive ? clr('active', 'green') : clr('inactive', 'red')
const enabledState = isEnabled ? clr('enabled', 'green') : clr('disabled', 'red')
const stateEmoji = (isActive && isEnabled) ? '' : ''
console.log(`\n ${stateEmoji} Indie Web Server is ${activeState} and ${enabledState}.\n`)
break
// Off (turn off the server daemon and remove it from startup items).
......@@ -239,7 +261,7 @@ switch (true) {
try {
// Start.
childProcess.execSync('sudo systemctl start web-server', {env: process.env, stdio: 'pipe'})
console.log(`${webServer.version()}\n 😈 Launched as daemon on https://${os.hostname()} serving ${pathToServe}\n`)
console.log(`${webServer.version()}\n 😈 Launched as daemon on ${clr(`https://${os.hostname()}`, 'green')} serving ${clr(pathToServe, 'cyan')}\n`)
// Enable.
childProcess.execSync('sudo systemctl enable web-server', {env: process.env, stdio: 'pipe'})
......
......@@ -5,6 +5,8 @@ const path = require('path')
const os = require('os')
const childProcess = require('child_process')
const ansi = require('ansi-escape-sequences')
const express = require('express')
const helmet = require('helmet')
const morgan = require('morgan')
......@@ -29,7 +31,7 @@ class WebServer {
// the version set in the package.json file. (Synchronous.)
version () {
const version = JSON.parse(fs.readFileSync(path.join(__dirname, './package.json'), 'utf-8')).version
return `\n 💖 Indie Web Server v${version} running on Node ${process.version}\n`
return `\n 💖 Indie Web Server v${version} ${clr(`(running on Node ${process.version})`, 'italic')}\n`
}
// Returns an https server instance – the same as you’d get with
......@@ -78,7 +80,7 @@ class WebServer {
portSuffix = `:${serverPort}`
}
const location = global ? os.hostname() : `localhost${portSuffix}`
console.log(`\n 🎉 Serving ${pathToServe} on https://${location}\n`)
console.log(`\n 🎉 Serving ${clr(pathToServe, 'cyan')} on ${clr(`https://${location}`, 'green')}\n`)
}
// Check if a 4042302 (404 → 302) redirect has been requested.
......@@ -315,3 +317,13 @@ class WebServer {
}
module.exports = new WebServer()
//
// Helpers.
//
// Format ansi strings.
// Courtesy Bankai (https://github.com/choojs/bankai/blob/master/bin.js#L142)
function clr (text, color) {
return process.stdout.isTTY ? ansi.format(text, color) : text
}
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