Commit 2bc485b8 authored by Aral Balkan's avatar Aral Balkan

Update console output formatting

parent 7f421aa9
......@@ -15,7 +15,7 @@ function disable () {
ensure.systemctl()
ensure.root('disable')
Site.logAppNameAndVersion()
Site.logAppNameAndVersion(/* compact = */ true)
try {
// Disable and stop the web server.
......
......@@ -185,7 +185,7 @@ function enable (args) {
try {
// Start.
childProcess.execSync('sudo systemctl start site.js', {env: process.env, stdio: 'pipe'})
Site.logAppNameAndVersion()
Site.logAppNameAndVersion(/* compact = */ true)
console.log(` 😈 Launched as daemon on ${clr(`https://${os.hostname()}`, 'green')} serving ${clr(pathToServe, 'cyan')}\n`)
// Enable.
......@@ -228,8 +228,8 @@ function displayConnectionInformation(pathToServe) {
const syncToValue = `${account}@${hostname}:${absolutePathToServe}`
console.log(` 💞 [Sync] To sync from your local machine, from within your site’s folder, use:`)
console.log(` 💞 [Sync] site --sync-to=${syncToValue} --exit-on-sync\n`)
console.log(` 💫 [Sync] To sync from your local machine, from within your site’s folder, use:`)
console.log(` 💫 [Sync] site --sync-to=${syncToValue} --exit-on-sync\n`)
} catch (error) {
console.error(error, `\n 👿 Error: could not get connection information.\n`)
process.exit(1)
......@@ -241,12 +241,12 @@ function displayConnectionInformation(pathToServe) {
// our rsync calls will take place via ssh as they should.)
function disableInsecureRsyncDaemon() {
try {
process.stdout.write(' 💞 [Sync] Securing Rsync… ')
process.stdout.write(' 💫 [Sync] Securing Rsync… ')
childProcess.execSync('sudo systemctl stop rsync', {env: process.env, stdio: 'pipe'})
childProcess.execSync('sudo systemctl disable rsync', {env: process.env, stdio: 'pipe'})
childProcess.execSync('sudo systemctl mask rsync', {env: process.env, stdio: 'pipe'})
console.log('done!')
console.log(` 💞 [Sync] Rsync set up to only allow secure access via ssh.\n`)
console.log(` 💫 [Sync] Rsync set up to only allow secure access via ssh.\n`)
} catch (error) {
console.error(error, `\n 👿 Error: could not disable insecure rsync daemon.\n`)
process.exit(1)
......
......@@ -77,7 +77,7 @@ function help () {
// Linux, macOS, and Windows).
const prompt = clr('', 'blue')
Site.logAppNameAndVersion()
Site.logAppNameAndVersion(/* compact = */ true)
const usage = `
${heading('Usage:')}
......
......@@ -13,7 +13,7 @@ const Site = require('../../index')
const ensure = require('../lib/ensure')
function logs () {
Site.logAppNameAndVersion()
Site.logAppNameAndVersion(/* compact = */ true)
ensure.journalctl()
console.log(` 📜 Tailing logs (press Ctrl+C to exit).\n`)
childProcess.spawn('journalctl', ['--follow', '--unit', 'site.js'], {env: process.env, stdio: 'inherit'})
......
......@@ -14,7 +14,7 @@ function restart () {
ensure.systemctl()
ensure.root('restart')
Site.logAppNameAndVersion()
Site.logAppNameAndVersion(/* compact = */ true)
try {
// Start the web server.
......
......@@ -14,7 +14,7 @@ function start () {
ensure.systemctl()
ensure.root('start')
Site.logAppNameAndVersion()
Site.logAppNameAndVersion(/* compact = */ true)
try {
// Start the web server.
......
......@@ -14,7 +14,7 @@ const clr = require('../../lib/clr')
const ensure = require('../lib/ensure')
function status () {
Site.logAppNameAndVersion()
Site.logAppNameAndVersion(/* compact = */ true)
// Ensure systemctl exists as it is required for getStatus().
// We cannot check in the function itself as it would create
......
......@@ -14,7 +14,7 @@ function stop () {
ensure.systemctl()
ensure.root('stop')
Site.logAppNameAndVersion()
Site.logAppNameAndVersion(/* compact = */ true)
try {
// Stop the web server.
......
......@@ -66,7 +66,7 @@ async function uninstall (options) {
ensure.root('uninstall')
}
Site.logAppNameAndVersion()
Site.logAppNameAndVersion(/* compact = */ true)
const { isActive: serverIsActive, isEnabled: serverIsEnabled } = status()
......
......@@ -28,7 +28,7 @@ async function update () {
ensure.root('update')
Site.logAppNameAndVersion()
Site.logAppNameAndVersion(/* compact = */ true)
console.log(' 🧐 Checking for updates…\n')
......
......@@ -7,7 +7,7 @@
const Site = require('../../index.js')
function version () {
Site.logAppNameAndVersion()
Site.logAppNameAndVersion(/* compact = */ true)
process.exit()
}
......
......@@ -62,9 +62,11 @@ class Site {
// the version set in the package.json file to console.
// (Only once per Site lifetime.)
// (Synchronous.)
static logAppNameAndVersion () {
static logAppNameAndVersion (compact = false) {
if (!Site.appNameAndVersionAlreadyLogged && !process.argv.includes('--dont-log-app-name-and-version')) {
console.log(`\n 💕 Site.js v${Site.versionNumber()} ${clr(`(running on Node ${process.version})`, 'italic')}\n`)
let appNameAndVersion = `\n 💕 Site.js v${Site.versionNumber()} ${clr(`(running on Node ${process.version})`, 'italic')}\n`
if (compact) { appNameAndVersion = appNameAndVersion.replace(' 💕 ', ' 💕 ')}
console.log(appNameAndVersion)
Site.appNameAndVersionAlreadyLogged = true
}
}
......@@ -171,26 +173,75 @@ class Site {
// Logging.
this.app.use(morgan(function (tokens, req, res) {
let hasWarning = false
let method = tokens.method(req, res)
if (method === 'GET') method = '↓ GET'
if (method === 'POST') method = '↑ POST'
let duration = `${parseFloat(tokens['response-time'](req, res)).toFixed(1)} ms`
let durationWarning = ''
let duration = parseFloat(tokens['response-time'](req, res)).toFixed(1)
if (duration > 500) { durationWarning = ' !'}
if (duration > 1000) { durationWarning = ' !!'}
if (durationWarning !== '') {
hasWarning = true
}
duration = `${duration} ms${clr(durationWarning, 'yellow')}`
let sizeWarning = ''
let size = (tokens.res(req, res, 'content-length')/1024).toFixed(1)
if (size > 500) { sizeWarning = ' !' }
if (size > 1000) { sizeWarning = ' !!'}
if (sizeWarning !== '') {
hasWarning = true
}
size = `${size} kb${clr(sizeWarning, 'yellow')}`
if (size === 'NaN kb') { size = ' - ' }
let size = (tokens.res(req, res, 'content-length')/1024).toFixed(1) + ' kb'
if (size === 'NaN kb') size = ' - '
let url = tokens.url(req, res)
if (url.endsWith('.png') || url.endsWith('.jpg') || url.endsWith('.jpeg') || url.endsWith('.svg') || url.endsWith('.gif')) {
url = `🌌 ${url}`
} else if (url.endsWith('.ico')) {
url = `💠 ${url}`
}
else if (url.endsWith('css')) {
url = `🎨 ${url}`
} else if (url === '/instant/client/bundle.js') {
url = `⚡ Live reload script load`
} else if (url.endsWith('js')) {
url = `⚡ ${url}`
} else if (url === '/instant/events') {
url = `✨ Live reload event`
} else {
url = `📄 ${url}`
}
let status = tokens.status(req, res)
const statusToTextColour = {
'404': 'red',
'304': 'cyan',
'200': 'green',
}
let textColour = statusToTextColour[status]
if (hasWarning) { textColour = 'yellow'}
const log = [
method,
clr(method, textColour),
'\t',
tokens.status(req, res),
clr(status, textColour),
'\t',
duration,
clr(duration, textColour),
'\t',
size,
clr(size, textColour),
'\t',
tokens.url(req, res),
clr(url, textColour),
].join(' ')
return ` 💞 ${log}`
}))
......
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