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

Refactor to create generic command-line warning box class

parent 7aa92885
......@@ -8,29 +8,72 @@
const prompts = require('prompts')
const Graceful = require('node-graceful')
const actualStringLength = require('string-length')
const status = require('../lib/status')
const webServer = require('../../index')
const clr = require('../../lib/clr')
class WarningBox {
constructor () {
this.lines = []
}
line (line) {
this.lines.push(line)
}
emptyLine() {
this.lines.push('')
}
render() {
// Create the box based on the length of the longest line.
// With 1 space padding on each side of a passed line.
const boxWidth = this.lines.reduce((longestLineLengthSoFar, currentLine) => Math.max(longestLineLengthSoFar, actualStringLength(currentLine)), /* initial longestLineLengthSoFar value is */ 0) + 2
const repeat = (thisMany, character) => Array(thisMany).fill(character).join('')
const renderLine = (line) => ` ║ ${line}${repeat(boxWidth - actualStringLength(line) - 1, ' ')}║`
const horizontalLine = repeat(boxWidth, '')
const top = ` 🔔 ╔${horizontalLine}╗\n`
const body = this.lines.reduce((body, currentLine) => `${body}${renderLine(currentLine)}\n`, /* initial body is */ '')
const bottom = ` ╚${horizontalLine}╝\n`
return top + body + bottom
}
print() {
const box = this.render()
console.log(box)
}
}
async function uninstall (options) {
console.log(webServer.version())
const { isActive, isEnabled } = status()
let serverStatusMessage = ''
const warning = new WarningBox()
warning.line(`${clr('WARNING!', 'yellow')} ${clr('About to uninstall Indie Web Server.', 'green')}`)
if (isActive && isEnabled) {
serverStatusMessage = `\n ║ ║\n ║ • ${clr('The server is active and enabled.', 'yellow')} ║\n ║ It will be stopped and disabled. ║\n ║ ║`
warning.emptyLine()
warning.line(`• ${clr('The server is active and enabled.', 'yellow')}`)
warning.line(' It will be stopped and disabled.')
} else if (isActive) {
serverStatusMessage = '\n 🐭 The server is active.\nIt will be stopped.\n'
warning.emptyLine()
warning.line(`• ${clr('The server is active.', 'yellow')}`)
warning.line(' It will be stopped.')
} else if (isEnabled) {
serverStatusMessage = '\n 🐭 The server is enabled.\nIt will be disabled.\n'
warning.emptyLine()
warning.line(`• ${clr('The server is enabled.', 'yellow')}`)
warning.line(' It will be disabled.')
}
console.log(' 🔔 ╔═══════════════════════════════════════════════╗ ')
console.log(` ║ ${clr('WARNING!', 'yellow')} ${clr('About to uninstall Indie Web Server.', 'green')}${serverStatusMessage}`)
console.log(' ╚═══════════════════════════════════════════════╝\n')
warning.print()
const response = await prompts({
type: 'confirm',
......
......@@ -415,6 +415,11 @@
"resolved": "https://registry.npmjs.org/assign-symbols/-/assign-symbols-1.0.0.tgz",
"integrity": "sha1-WWZ/QfrdTyDMvCu5a41Pf3jsA2c="
},
"astral-regex": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/astral-regex/-/astral-regex-1.0.0.tgz",
"integrity": "sha512-+Ryf6g3BKoRc7jfp7ad8tM4TtMiaWvbF/1/sQcZPkkS7ag3D5nMBCe2UfOTONtAkaG0tO0ij3C5Lwmf1EiyjHg=="
},
"async-each": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/async-each/-/async-each-1.0.3.tgz",
......@@ -5131,6 +5136,30 @@
"integrity": "sha1-J5siXfHVgrH1TmWt3UNS4Y+qBxM=",
"dev": true
},
"string-length": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/string-length/-/string-length-3.1.0.tgz",
"integrity": "sha512-Ttp5YvkGm5v9Ijagtaz1BnN+k9ObpvS0eIBblPMp2YWL8FBmi9qblQ9fexc2k/CXFgrTIteU3jAw3payCnwSTA==",
"requires": {
"astral-regex": "^1.0.0",
"strip-ansi": "^5.2.0"
},
"dependencies": {
"ansi-regex": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz",
"integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg=="
},
"strip-ansi": {
"version": "5.2.0",
"resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz",
"integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==",
"requires": {
"ansi-regex": "^4.1.0"
}
}
}
},
"string-width": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/string-width/-/string-width-3.1.0.tgz",
......
......@@ -40,6 +40,7 @@
"redirect-https": "^1.3.0",
"rsync": "^0.6.1",
"shelljs": "^0.8.3",
"string-length": "^3.1.0",
"tcp-port-used": "^1.0.1"
},
"devDependencies": {
......
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