Browse Source

converted strings to template literals

pull/15/head
Prabhanjan 6 years ago
parent
commit
014ed1dc62
  1. 23
      actions/createNewServer.js
  2. 30
      actions/createProxyServer.js
  3. 10
      actions/killALL.js
  4. 6
      actions/killAllConfirm.js
  5. 4
      actions/listServers.js
  6. 8
      utils/listFile.js
  7. 7
      utils/nginxConf.js
  8. 15
      utils/requirements.js

23
actions/createNewServer.js

@ -8,6 +8,7 @@ const conf = require('../utils/nginxConf');
const nginxReload = require('../utils/nginxReload'); const nginxReload = require('../utils/nginxReload');
const { appendToList } = require('../utils/listFile'); const { appendToList } = require('../utils/listFile');
// Are EOL's neccessary since template literals handle new lines? PS: Unused import
const { EOL } = require('os'); // \n if used on Linux, \r\n if used on Windows. const { EOL } = require('os'); // \n if used on Linux, \r\n if used on Windows.
function createStaticServer(domain, path, outPort) { function createStaticServer(domain, path, outPort) {
@ -18,17 +19,17 @@ function createStaticServer(domain, path, outPort) {
fs.outputFileSync((conf(npath.enabledSites(), domain, outPort)), fs.outputFileSync((conf(npath.enabledSites(), domain, outPort)),
// Gets nginx's paths from nginxPath.js // Gets nginx's paths from nginxPath.js
"server {" + EOL + `server {
" listen " + outPort + ";" + EOL + listen ${outPort};
" listen [::]:" + outPort + ";" + EOL + listen [::]:" ${outPort};
" root " + npath.webRoot() + domain + "." + outPort + ";" + EOL + root ${npath.webRoot()}${domain}.${outPort};
" index index.html index.htm;" + EOL + index index.html index.htm;
"" + EOL +
" server_name " + domain + ";" + EOL + server_name ${domain};
" location / {" + EOL + location / {
" try_files $uri $uri/ =404;" + EOL + try_files $uri $uri/ =404;
" }" + EOL + }
"}" }`
); );
shell.rm('-rf', npath.webRootDomain(domain, outPort)); shell.rm('-rf', npath.webRootDomain(domain, outPort));

30
actions/createProxyServer.js

@ -17,21 +17,21 @@ function createProxyServer(domain, inPort, outPort) {
// Creates directories if doesn't exist // Creates directories if doesn't exist
fs.outputFileSync((conf(npath.enabledSites(), domain, outPort)), fs.outputFileSync((conf(npath.enabledSites(), domain, outPort)),
"server {" + EOL + `server {
" listen " + outPort + ";" + EOL + listen ${outPort};
" listen [::]:" + outPort + ";" + EOL + listen [::]:${outPort};
" index index.html index.htm;" + EOL + index index.html index.htm;
"" + EOL +
" server_name " + domain + ";" + EOL + server_name ${domain};
" location / {" + EOL + location / {
" proxy_pass http://localhost:" + inPort + ";" + EOL + proxy_pass http://localhost:${inPort};
" proxy_http_version 1.1;" + EOL + proxy_http_version 1.1;
" proxy_set_header Upgrade $http_upgrade;" + EOL + proxy_set_header Upgrade $http_upgrade;
" proxy_set_header Connection 'upgrade';" + EOL + proxy_set_header Connection 'upgrade';
" proxy_set_header Host $host;" + EOL + proxy_set_header Host $host;
" proxy_cache_bypass $http_upgrade;" + EOL + proxy_cache_bypass $http_upgrade;
" }" + EOL + }
"}" }`
); );
shell.mkdir('-p', npath.enabledSites()); shell.mkdir('-p', npath.enabledSites());

10
actions/killALL.js

@ -21,15 +21,13 @@ function killALL () {
conf(npath.enabledSites())); conf(npath.enabledSites()));
// Create the default.conf file // Create the default.conf file
nginxReload(); nginxReload();
console.log(EOL + "All servers were killed and reverted to default." + console.log(`\nAll servers were killed and reverted to default.
EOL + "A backup of your old servers.up is " + A backup of your old servers.up is saved in /etc/up-serve/servers.bak.up.\
"saved in /etc/up-serve/servers.bak.up." + Check this if you need to.`);
"Check this if you need to." + EOL
);
} }
function noKill () { function noKill () {
return(EOL + "kill-all was interrupted by user."); return(`\nkill-all was interrupted by user.`);
} }
module.exports.kill = killALL; module.exports.kill = killALL;

6
actions/killAllConfirm.js

@ -8,9 +8,9 @@ const killALL = require('./killALL').kill;
const { noKill } = require('./killALL'); const { noKill } = require('./killALL');
function killAllConfirm() { function killAllConfirm() {
console.log(EOL + "This action will destroy all nginx servers and return " console.log(`This action will destroy all nginx servers and return\
+ "to default configuration." + EOL); to default configuration.`);
if (readlineSync.keyInYN("Are you sure you want to do this?")) { if (readlineSync.keyInYN(`Are you sure you want to do this?`)) {
killALL(); killALL();
} }
else { else {

4
actions/listServers.js

@ -7,8 +7,8 @@ const { EOL } = require('os');
function listServers() { function listServers() {
const serversList = readServers(); const serversList = readServers();
if(serversList) return(EOL + prettyjson.render(serversList)); if(serversList) return(`${prettyjson.render(serversList)}`);
else throw new Error("No servers were found! Create some using `up`!"); else throw new Error(`No servers were found! Create some using \`up\`!`);
} }
module.exports = listServers; module.exports = listServers;

8
utils/listFile.js

@ -45,9 +45,9 @@ function removeFromList (domain, outPort) {
return; return;
} }
} }
throw new Error("This domain does not exist in servers.up"); throw new Error(`This domain does not exist in servers.up`);
} }
else throw new Error("No servers were created using `up` yet."); else throw new Error(`No servers were created using \`up\` yet.`);
} }
function readServers () { function readServers () {
@ -55,11 +55,11 @@ function readServers () {
if (fs.existsSync(listFilePath())) { if (fs.existsSync(listFilePath())) {
serversList = JSON.parse(fs.readFileSync(listFilePath())); serversList = JSON.parse(fs.readFileSync(listFilePath()));
if(!serversList.domains[0]) { if(!serversList.domains[0]) {
throw new Error("No domains exist in servers.up"); throw new Error(`No domains exist in servers.up`);
} }
} }
else { else {
throw new Error("No servers were created using `up` yet."); throw new Error(`No servers were created using \`up\` yet.`);
} }
return serversList; return serversList;
} }

7
utils/nginxConf.js

@ -3,11 +3,6 @@
// Simple function that takes a path and domain name, // Simple function that takes a path and domain name,
// concatenates them with ".conf" and returns it. // concatenates them with ".conf" and returns it.
function conf(path, domain, outPort) { const conf = (path, domain, outPort) => path + (domain || "default") + (outPort ? "." + outPort : "") + ".conf";
domain = domain || "default";
outPort = outPort || "";
if (outPort != "") outPort = "." + outPort;
return (path + domain + outPort + ".conf");
}
module.exports = conf; module.exports = conf;

15
utils/requirements.js

@ -14,26 +14,23 @@ function requirements() {
// This should be changed to throw if not Debian based distro. // This should be changed to throw if not Debian based distro.
// Eventually, we can add more exceptions as `up` handles more cases. // Eventually, we can add more exceptions as `up` handles more cases.
if(!isLin) { if(!isLin) {
shell.echo(EOL + shell.echo(`\nThis is not a Linux or freeBSD distribution. This tool not written for this distro.
"This is not a Linux or freeBSD distribution. " + Please raise an issue at ${chalk.cyan("https://github.com/codefeathers/up-serve")}\
"This tool not written for this distro. " + if you want \`up\` to be ported for your distro`);
"Please raise an issue at " +
chalk.cyan("https://github.com/codefeathers/up-serve") +
" if you want `up` to be ported for your distro");
shell.exit(1); shell.exit(1);
} }
// Check if sudo // Check if sudo
if (process.getuid() != 0) { if (process.getuid() != 0) {
shell.echo("`up` requires root privileges to work." shell.echo(`\`up\` requires root privileges to work.\
+ "Please use `sudo up <command>`"); Please use \`sudo up <command>\``);
shell.exit(1); shell.exit(1);
} }
// Throw if Nginx is not found // Throw if Nginx is not found
if (!shell.which('nginx')) { if (!shell.which('nginx')) {
shell.echo( shell.echo(
'I need nginx to work. Install nginx first. https://nginx.org/'); `I need nginx to work. Install nginx first. ${chalk.cyan("https://nginx.org/")}`);
shell.exit(1); shell.exit(1);
} }
} }

Loading…
Cancel
Save