From 1af40375aeff944c522f80bc244c7fe8ae0df02b Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Sun, 5 Nov 2017 22:49:02 +0530 Subject: [PATCH] Rename util to utils --- index.js | 4 ++-- util/isIP.js | 25 -------------------- util/nginxConf.js | 7 ------ util/nginxPath.js | 27 ---------------------- util/nginxReload.js | 14 ----------- util/parseToInt.js | 8 ------- util/requirements.js | 23 ------------------ util/validate.js | 64 --------------------------------------------------- utils/isIP.js | 25 ++++++++++++++++++++ utils/nginxConf.js | 7 ++++++ utils/nginxPath.js | 27 ++++++++++++++++++++++ utils/nginxReload.js | 14 +++++++++++ utils/parseToInt.js | 8 +++++++ utils/requirements.js | 23 ++++++++++++++++++ utils/validate.js | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ 15 files changed, 170 insertions(+), 170 deletions(-) delete mode 100644 util/isIP.js delete mode 100644 util/nginxConf.js delete mode 100644 util/nginxPath.js delete mode 100644 util/nginxReload.js delete mode 100644 util/parseToInt.js delete mode 100644 util/requirements.js delete mode 100644 util/validate.js create mode 100644 utils/isIP.js create mode 100644 utils/nginxConf.js create mode 100644 utils/nginxPath.js create mode 100644 utils/nginxReload.js create mode 100644 utils/parseToInt.js create mode 100644 utils/requirements.js create mode 100644 utils/validate.js diff --git a/index.js b/index.js index 40d5334..f95b0f2 100644 --- a/index.js +++ b/index.js @@ -5,8 +5,8 @@ var program = require('commander'); var chalk = require('chalk'); // Requiring utils -var validate = require('./util/validate'); -var requirements = require('./util/requirements'); +var validate = require('./utils/validate'); +var requirements = require('./utils/requirements'); // Requiring Actions var createProxyServer = require('./actions/createProxyServer'); diff --git a/util/isIP.js b/util/isIP.js deleted file mode 100644 index 0a25224..0000000 --- a/util/isIP.js +++ /dev/null @@ -1,25 +0,0 @@ -// Parses a string, and returns true if it is an IP Address. - -function isIP(str) { - var segments = str - .split(".") - .map(Number); - if (!segments.length === 4) { - 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 true; -} - -module.exports = isIP; \ No newline at end of file diff --git a/util/nginxConf.js b/util/nginxConf.js deleted file mode 100644 index 5599e60..0000000 --- a/util/nginxConf.js +++ /dev/null @@ -1,7 +0,0 @@ -// Simple function that takes a path and domain name, concatenates them with ".conf" and returns it. - -function conf(path, domain, outPort) { - return (path + domain + "." + outPort + ".conf"); -} - -module.exports = conf; \ No newline at end of file diff --git a/util/nginxPath.js b/util/nginxPath.js deleted file mode 100644 index 91dbe8c..0000000 --- a/util/nginxPath.js +++ /dev/null @@ -1,27 +0,0 @@ -// 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 deleted file mode 100644 index 8661220..0000000 --- a/util/nginxReload.js +++ /dev/null @@ -1,14 +0,0 @@ -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); - return; - } - }); -} - -module.exports = nginxReload; \ No newline at end of file diff --git a/util/parseToInt.js b/util/parseToInt.js deleted file mode 100644 index 891d3f6..0000000 --- a/util/parseToInt.js +++ /dev/null @@ -1,8 +0,0 @@ -// Parse an input string and return a number if it is an integer. If it's a float, string, or array, return undefined. - -function parseToInt(inputString) { - var parsing = /^\d+$/.exec(inputString); - return (parsing || [])[0]; -} - -module.exports = parseToInt; \ No newline at end of file diff --git a/util/requirements.js b/util/requirements.js deleted file mode 100644 index 90727e4..0000000 --- a/util/requirements.js +++ /dev/null @@ -1,23 +0,0 @@ -var shell = require('shelljs'); -var chalk = require('chalk'); - -function requirements() { - - // Detect Linux or BSD - var isLin = /^linux|^bsd/.test(process.platform); - - // Throw if OS is not Linux or BSD. This should be changed to throw if not Debian based distro. Eventually, we can add more exceptions as `up` handles more cases. - if(!isLin) { - shell.echo("\nThis is not a Linux or freeBSD distribution. This tool not written for this 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); - } - - // Throw if Nginx is not found - if (!shell.which('nginx')) { - shell.echo('I need nginx to work. Install nginx first. https://nginx.org/'); - shell.exit(1); - } - -} - -module.exports = requirements; \ No newline at end of file diff --git a/util/validate.js b/util/validate.js deleted file mode 100644 index 29e3589..0000000 --- a/util/validate.js +++ /dev/null @@ -1,64 +0,0 @@ -var validator = require('validator'); -var parseToInt = require('./parseToInt'); -var isIP = require('./isIP'); - -// Using Validator -var isDomain = validator.isFQDN; - -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."]; - var portInvalidMsg = ["\nPort should be a number.", "\nPort should be a number from 1 and 65535."]; - - // ARGV returns a string as input. Port numbers should be parsed to int to validate them. If validation fails, these will return undefined and will fail the subsequent test. - var validInPort = parseToInt(inPort); - var validOutPort = parseToInt(outPort); - - // The value of isInvalid will be returned back. If none of the `if`s are true, the default value `true` is returned `domain`, `inPort` and `outPort` are considered validated. - var isValid = true; - - // Throw if IP is given instead of domain name. - if (isIP(domain)) { - console.log(domainInvalidMsg[0]); - return isValid = false; - } - - // Throw if input is not a Fully Qualified Domain Name (FQDN) - if (!isDomain(domain)) { - console.log(domainInvalidMsg[1]); - return isValid = false; - } - - // Enter if `inPort` is not defined. This happens for `up static` where no inbound ports are required. - if (typeof inPort == undefined) { - if (!validOutPort) { - console.log(portInvalidMsg[0]); // `outPort` is not an integer. - return isValid = false; - } - if (!(validOutPort > 0 && validOutPort <= 65535)) { - console.log(portInvalidMsg[1]); // `outPort` is not within port range. - return isValid = false; - } - } - - // Enter if `inPort` is defined. This happens for `up proxy` where inbound port is required. - if (typeof inPort !== undefined) { - if (!validInPort || !validOutPort) { - console.log(portInvalidMsg[0]); // Either `inPort` or `outPort` is not an integer. - return isValid = false; - } - if (typeof outPort !== undefined) { - if (!((validInPort > 0 && validInPort <= 65535) && (validOutPort > 0 && validOutPort <= 65535))) { - console.log(portInvalidMsg[1]); // Either `inPort` or `outPort` are not within port range. - return isValid = false; - } - } - return isValid; // If any of the `if`s were true, `isInvalid = false`. If not, `isInvalid = true`. - } -} - -module.exports = validate; \ No newline at end of file diff --git a/utils/isIP.js b/utils/isIP.js new file mode 100644 index 0000000..0a25224 --- /dev/null +++ b/utils/isIP.js @@ -0,0 +1,25 @@ +// Parses a string, and returns true if it is an IP Address. + +function isIP(str) { + var segments = str + .split(".") + .map(Number); + if (!segments.length === 4) { + 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 true; +} + +module.exports = isIP; \ No newline at end of file diff --git a/utils/nginxConf.js b/utils/nginxConf.js new file mode 100644 index 0000000..5599e60 --- /dev/null +++ b/utils/nginxConf.js @@ -0,0 +1,7 @@ +// Simple function that takes a path and domain name, concatenates them with ".conf" and returns it. + +function conf(path, domain, outPort) { + return (path + domain + "." + outPort + ".conf"); +} + +module.exports = conf; \ No newline at end of file diff --git a/utils/nginxPath.js b/utils/nginxPath.js new file mode 100644 index 0000000..91dbe8c --- /dev/null +++ b/utils/nginxPath.js @@ -0,0 +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) { + 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/utils/nginxReload.js b/utils/nginxReload.js new file mode 100644 index 0000000..8661220 --- /dev/null +++ b/utils/nginxReload.js @@ -0,0 +1,14 @@ +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); + return; + } + }); +} + +module.exports = nginxReload; \ No newline at end of file diff --git a/utils/parseToInt.js b/utils/parseToInt.js new file mode 100644 index 0000000..891d3f6 --- /dev/null +++ b/utils/parseToInt.js @@ -0,0 +1,8 @@ +// Parse an input string and return a number if it is an integer. If it's a float, string, or array, return undefined. + +function parseToInt(inputString) { + var parsing = /^\d+$/.exec(inputString); + return (parsing || [])[0]; +} + +module.exports = parseToInt; \ No newline at end of file diff --git a/utils/requirements.js b/utils/requirements.js new file mode 100644 index 0000000..90727e4 --- /dev/null +++ b/utils/requirements.js @@ -0,0 +1,23 @@ +var shell = require('shelljs'); +var chalk = require('chalk'); + +function requirements() { + + // Detect Linux or BSD + var isLin = /^linux|^bsd/.test(process.platform); + + // Throw if OS is not Linux or BSD. This should be changed to throw if not Debian based distro. Eventually, we can add more exceptions as `up` handles more cases. + if(!isLin) { + shell.echo("\nThis is not a Linux or freeBSD distribution. This tool not written for this 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); + } + + // Throw if Nginx is not found + if (!shell.which('nginx')) { + shell.echo('I need nginx to work. Install nginx first. https://nginx.org/'); + shell.exit(1); + } + +} + +module.exports = requirements; \ No newline at end of file diff --git a/utils/validate.js b/utils/validate.js new file mode 100644 index 0000000..29e3589 --- /dev/null +++ b/utils/validate.js @@ -0,0 +1,64 @@ +var validator = require('validator'); +var parseToInt = require('./parseToInt'); +var isIP = require('./isIP'); + +// Using Validator +var isDomain = validator.isFQDN; + +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."]; + var portInvalidMsg = ["\nPort should be a number.", "\nPort should be a number from 1 and 65535."]; + + // ARGV returns a string as input. Port numbers should be parsed to int to validate them. If validation fails, these will return undefined and will fail the subsequent test. + var validInPort = parseToInt(inPort); + var validOutPort = parseToInt(outPort); + + // The value of isInvalid will be returned back. If none of the `if`s are true, the default value `true` is returned `domain`, `inPort` and `outPort` are considered validated. + var isValid = true; + + // Throw if IP is given instead of domain name. + if (isIP(domain)) { + console.log(domainInvalidMsg[0]); + return isValid = false; + } + + // Throw if input is not a Fully Qualified Domain Name (FQDN) + if (!isDomain(domain)) { + console.log(domainInvalidMsg[1]); + return isValid = false; + } + + // Enter if `inPort` is not defined. This happens for `up static` where no inbound ports are required. + if (typeof inPort == undefined) { + if (!validOutPort) { + console.log(portInvalidMsg[0]); // `outPort` is not an integer. + return isValid = false; + } + if (!(validOutPort > 0 && validOutPort <= 65535)) { + console.log(portInvalidMsg[1]); // `outPort` is not within port range. + return isValid = false; + } + } + + // Enter if `inPort` is defined. This happens for `up proxy` where inbound port is required. + if (typeof inPort !== undefined) { + if (!validInPort || !validOutPort) { + console.log(portInvalidMsg[0]); // Either `inPort` or `outPort` is not an integer. + return isValid = false; + } + if (typeof outPort !== undefined) { + if (!((validInPort > 0 && validInPort <= 65535) && (validOutPort > 0 && validOutPort <= 65535))) { + console.log(portInvalidMsg[1]); // Either `inPort` or `outPort` are not within port range. + return isValid = false; + } + } + return isValid; // If any of the `if`s were true, `isInvalid = false`. If not, `isInvalid = true`. + } +} + +module.exports = validate; \ No newline at end of file