From 22537db6d1dbb3335f67f07492efb28e1e050227 Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Fri, 3 Nov 2017 19:38:11 +0530 Subject: [PATCH] Linting and refactoring to consistent style --- .eslintrc.json | 21 +++++++++++++ actions/createProxyServer.js | 66 ++++++++++++++++++++-------------------- actions/createStaticServer.js | 71 ++++++++++++++++++++++--------------------- index.js | 15 ++++----- util/isIP.js | 23 +++++++------- util/nginxPath.js | 52 +++++++++++++++---------------- util/nginxReload.js | 16 +++++----- util/validate.js | 5 ++- 8 files changed, 148 insertions(+), 121 deletions(-) create mode 100644 .eslintrc.json diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..2a8c5a6 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,21 @@ +{ + "env": { + "node": true + }, + "extends": "eslint:recommended", + "rules": { + "no-console": "off", + "indent": [ + "error", + "tab" + ], + "linebreak-style": [ + "error", + "unix" + ], + "semi": [ + "error", + "always" + ] + } +} \ No newline at end of file diff --git a/actions/createProxyServer.js b/actions/createProxyServer.js index 4f539e5..39e70fd 100644 --- a/actions/createProxyServer.js +++ b/actions/createProxyServer.js @@ -1,34 +1,34 @@ -var fs = require('fs-extra'); -var shell = require('shelljs'); - -var npath = require('../util/nginxPath'); -var conf = require('../util/nginxConf'); -var nginxReload = require('../util/nginxReload'); - -var { EOL } = require('os'); // \n if used on Linux, \r\n if used on Windows. - -function createProxyServer(domain, inPort, outPort) { - fs.outputFileSync((conf(npath.availableSites(), domain, outPort)), - "server {" + EOL + - " listen " + outPort + ";" + EOL + - " listen [::]:" + outPort + ";" + EOL + - " index index.html index.htm;" + EOL + - "" + EOL + - " server_name " + domain + ";" + EOL + - " location / {" + EOL + - " proxy_pass http://localhost:" + inPort + ";" + EOL + - " proxy_http_version 1.1;" + EOL + - " proxy_set_header Upgrade $http_upgrade;" + EOL + - " proxy_set_header Connection 'upgrade';" + EOL + - " proxy_set_header Host $host;" + EOL + - " proxy_cache_bypass $http_upgrade;" + EOL + - " }" + EOL + - "}" - ) - shell.mkdir('-p', npath.enabledSites()); // Creates directory if doesn't exist - shell.ln('-sf', conf(npath.availableSites(), domain, outPort), conf(npath.enabledSites(), domain, outPort)); // Symlink the conf file from sites-available to sites-enabled - - nginxReload(); -}; - +var fs = require('fs-extra'); +var shell = require('shelljs'); + +var npath = require('../util/nginxPath'); +var conf = require('../util/nginxConf'); +var nginxReload = require('../util/nginxReload'); + +var EOL = require('os').EOL; // \n if used on Linux, \r\n if used on Windows. + +function createProxyServer(domain, inPort, outPort) { + fs.outputFileSync((conf(npath.availableSites(), domain, outPort)), + "server {" + EOL + + " listen " + outPort + ";" + EOL + + " listen [::]:" + outPort + ";" + EOL + + " index index.html index.htm;" + EOL + + "" + EOL + + " server_name " + domain + ";" + EOL + + " location / {" + EOL + + " proxy_pass http://localhost:" + inPort + ";" + EOL + + " proxy_http_version 1.1;" + EOL + + " proxy_set_header Upgrade $http_upgrade;" + EOL + + " proxy_set_header Connection 'upgrade';" + EOL + + " proxy_set_header Host $host;" + EOL + + " proxy_cache_bypass $http_upgrade;" + EOL + + " }" + EOL + + "}" + ); + shell.mkdir('-p', npath.enabledSites()); // Creates directory if doesn't exist + shell.ln('-sf', conf(npath.availableSites(), domain, outPort), conf(npath.enabledSites(), domain, outPort)); // Symlink the conf file from sites-available to sites-enabled + + nginxReload(); +} + module.exports = createProxyServer; \ No newline at end of file diff --git a/actions/createStaticServer.js b/actions/createStaticServer.js index 801e5ed..53c3c6a 100644 --- a/actions/createStaticServer.js +++ b/actions/createStaticServer.js @@ -1,36 +1,37 @@ -var fs = require('fs-extra'); -var shell = require('shelljs'); -var path = require('path'); - -var npath = require('../util/nginxPath'); -var conf = require('../util/nginxConf'); -var nginxReload = require('../util/nginxReload'); - -var currentPath = path.normalize(process.cwd()); -var { EOL } = require('os'); // \n if used on Linux, \r\n if used on Windows. - -function createStaticServer(domain, outPort = 80) { - fs.outputFileSync((conf(npath.availableSites(), domain, outPort)), // Gets nginx's paths from nginxPath.js - "server {" + EOL + - " listen " + outPort + ";" + EOL + - " listen [::]:" + outPort + ";" + EOL + - " root " + npath.webRoot() + domain + ";" + EOL + - " index index.html index.htm;" + EOL + - "" + EOL + - " server_name " + domain + ";" + EOL + - " location / {" + EOL + - " try_files $uri $uri/ =404;" + EOL + - " }" + EOL + - "}" - ) - shell.mkdir('-p', npath.enabledSites()); // Creates directory if doesn't exist - shell.rm('-rf', conf(npath.enabledSites(), domain, outPort)); // Removes domain from sites-enabled if exists - shell.ln('-sf', conf(npath.availableSites(), domain, outPort), conf(npath.enabledSites(), domain, outPort)); // Symlink the conf file from sites-available to sites-enabled - shell.rm('-rf', npath.webRootDomain(domain, outPort)); // Removes domain from webroot if exists - shell.mkdir('-p', npath.webRoot()); // Creating the nginx www path if it doesn't exist so symlink doesn't fail - shell.ln('-sf', currentPath, npath.webRootDomain(domain, outPort)); // Symlink current directory to nginx's web root - - nginxReload(); -}; - +var fs = require('fs-extra'); +var shell = require('shelljs'); +var path = require('path'); + +var npath = require('../util/nginxPath'); +var conf = require('../util/nginxConf'); +var nginxReload = require('../util/nginxReload'); + +var currentPath = path.normalize(process.cwd()); +var EOL = require('os').EOL; // \n if used on Linux, \r\n if used on Windows. + +function createStaticServer(domain, outPort) { + outPort = outPort || 80; + fs.outputFileSync((conf(npath.availableSites(), domain, outPort)), // Gets nginx's paths from nginxPath.js + "server {" + EOL + + " listen " + outPort + ";" + EOL + + " listen [::]:" + outPort + ";" + EOL + + " root " + npath.webRoot() + domain + ";" + EOL + + " index index.html index.htm;" + EOL + + "" + EOL + + " server_name " + domain + ";" + EOL + + " location / {" + EOL + + " try_files $uri $uri/ =404;" + EOL + + " }" + EOL + + "}" + ); + shell.mkdir('-p', npath.enabledSites()); // Creates directory if doesn't exist + shell.rm('-rf', conf(npath.enabledSites(), domain, outPort)); // Removes domain from sites-enabled if exists + shell.ln('-sf', conf(npath.availableSites(), domain, outPort), conf(npath.enabledSites(), domain, outPort)); // Symlink the conf file from sites-available to sites-enabled + shell.rm('-rf', npath.webRootDomain(domain, outPort)); // Removes domain from webroot if exists + shell.mkdir('-p', npath.webRoot()); // Creating the nginx www path if it doesn't exist so symlink doesn't fail + shell.ln('-sf', currentPath, npath.webRootDomain(domain, outPort)); // Symlink current directory to nginx's web root + + nginxReload(); +} + module.exports = createStaticServer; \ No newline at end of file diff --git a/index.js b/index.js index 9d200ea..872a2e6 100644 --- a/index.js +++ b/index.js @@ -2,13 +2,11 @@ // Requiring npm modules var program = require('commander'); -var shell = require('shelljs'); -var fs = require('fs-extra'); var chalk = require('chalk'); // Requiring utils var validate = require('./util/validate'); -var requirements = require('./util/requirements') +var requirements = require('./util/requirements'); // Requiring Actions var createProxyServer = require('./actions/createProxyServer'); @@ -24,7 +22,8 @@ program program .command('static [outPort]') .description('Create a static server at this folder.') - .action(function (domain, outPort = 80) { //If outport is not given, 80 is set as default. Later, change this default to reflect nginx's settings. + .action(function (domain, outPort) { //If outport is not given, 80 is set as default. Later, change this default to reflect nginx's settings. + outPort = outPort || "80"; // This is a string because regex needs to validate it. if (!validate(domain, outPort)) return; //Validates domain and outport, and if invalid, throws and returns. createStaticServer(domain, outPort); console.log("Done! Your static server has been set up!\nPoint your domain to this server and check " + chalk.cyan(domain) + " to verify!"); @@ -33,7 +32,8 @@ program program .command('proxy [outPort]') .description('Create a proxy server, listening at port number.') - .action(function (domain, inPort, outPort = "80") { //Inbound port is necessary, but outbound is set to 80 by default. Again, will change this to reflect nginx's settings. + .action(function (domain, inPort, outPort) { //Inbound port is necessary, but outbound is set to 80 by default. Again, will change this to reflect nginx's settings. + outPort = outPort || "80"; // This is a string because regex needs to validate it. if (!validate(domain, inPort, outPort)) return; createProxyServer(domain, inPort, outPort); console.log("Done! Your reverse proxy server has been set up!\nPoint your domain to this server and check " + chalk.cyan(domain) + " to verify!"); @@ -49,7 +49,8 @@ program program .command('kill [ourPort]') .description('Kill a server.') - .action(function (domain, outPort = 80) { + .action(function (domain, outPort) { + outPort = outPort || "80"; // This is a string because regex needs to validate it. killServer(domain, outPort); console.log("\nDone! Your server has been killed!\n"); }); @@ -57,7 +58,7 @@ program program .command('*') // This should pick invalid commands, but it doesn't, yet. .action(function () { - console.log("Invalid command. Type " + chalk.cyan('up --help') + " for help.") + console.log("Invalid command. Type " + chalk.cyan('up --help') + " for help."); }); // Adds custom help text to the automatically generated help. diff --git a/util/isIP.js b/util/isIP.js index 27690f5..0a25224 100644 --- a/util/isIP.js +++ b/util/isIP.js @@ -2,21 +2,22 @@ function isIP(str) { var segments = str - .split('.') - .map(Number); + .split(".") + .map(Number); if (!segments.length === 4) { - return false; - } - for (var segment of segments) { - if (Number.isNaN(segment)) { - return false; - } - if (segment < 1 || segment > 255) { return false; - } + } + for(var i = 0; i < segments.length; i++) { + var segment = segments[i]; + if (Number.isNaN(segment)) { + return false; + } + if (segment < 1 || segment > 255) { + return false; + } } if (segments[3] > 254) { - return false; + return false; } return true; } diff --git a/util/nginxPath.js b/util/nginxPath.js index aa509e9..91dbe8c 100644 --- a/util/nginxPath.js +++ b/util/nginxPath.js @@ -1,27 +1,27 @@ -// These functions just return paths. Later, these should be modified to poll from nginx's config. - -var available = "/etc/nginx/sites-available/"; -var enabled = "/etc/nginx/sites-enabled/"; -var wwwRoot = "/var/www/"; - -function availableSites() { - return available; -} - -function enabledSites() { - return enabled; -} - -function webRoot() { - return wwwRoot; -} - -function webRootDomain(domain, outPort) { - rootWithDomain = wwwRoot + domain + "." + outPort; - return rootWithDomain; -} - -module.exports.availableSites = availableSites; -module.exports.enabledSites = enabledSites; -module.exports.webRoot = webRoot; +// These functions just return paths. Later, these should be modified to poll from nginx's config. + +var available = "/etc/nginx/sites-available/"; +var enabled = "/etc/nginx/sites-enabled/"; +var wwwRoot = "/var/www/"; + +function availableSites() { + return available; +} + +function enabledSites() { + return enabled; +} + +function webRoot() { + return wwwRoot; +} + +function webRootDomain(domain, outPort) { + var rootWithDomain = wwwRoot + domain + "." + outPort; + return rootWithDomain; +} + +module.exports.availableSites = availableSites; +module.exports.enabledSites = enabledSites; +module.exports.webRoot = webRoot; module.exports.webRootDomain = webRootDomain; \ No newline at end of file diff --git a/util/nginxReload.js b/util/nginxReload.js index a0e86b9..8661220 100644 --- a/util/nginxReload.js +++ b/util/nginxReload.js @@ -1,14 +1,14 @@ -var { execSync } = require('child_process'); +var execSync = require('child_process').execSync; function nginxReload() { - execSync('service nginx reload', function (error, stdout, stderr) { - if (error) { - console.error(`exec error: ${error}`); - console.log(`stdout: ${stdout}`); - console.log(`stderr: ${stderr}`); + execSync('service nginx reload', function (error, stdout, stderr) { + if (error) { + console.error("exec error: " + error); + console.log("stdout: " + stdout); + console.log("stderr: " + stderr); return; - } - }) + } + }); } module.exports = nginxReload; \ No newline at end of file diff --git a/util/validate.js b/util/validate.js index cd9996e..29e3589 100644 --- a/util/validate.js +++ b/util/validate.js @@ -5,7 +5,10 @@ var isIP = require('./isIP'); // Using Validator var isDomain = validator.isFQDN; -function validate(domain, inPort = undefined, outPort = "80") { +function validate(domain, inPort, outPort) { + // + inPort = inPort || undefined; + outPort = outPort || 80; // Error messages var domainInvalidMsg = ["\nPlease use a domain name instead of an IP address.", "\nDomain is not valid. Please use a valid domain name."];