Browse Source

ESLint fixes: const, newlines, destructuring

pull/6/head
Thomas Rory Gummerson 7 years ago
parent
commit
af4ed5d211
  1. 6
      .eslintrc.json
  2. 18
      actions/createProxyServer.js
  3. 22
      actions/createStaticServer.js
  4. 10
      actions/killALL.js
  5. 6
      actions/killAllConfirm.js
  6. 14
      actions/killServer.js
  7. 12
      actions/listServers.js
  8. 10
      build/fetchTLDS.js
  9. 20
      index.js
  10. 14
      utils/isFQDN.js
  11. 10
      utils/isIP.js
  12. 22
      utils/listFile.js
  13. 4
      utils/nginxConf.js
  14. 14
      utils/nginxPath.js
  15. 6
      utils/nginxReload.js
  16. 6
      utils/parseToInt.js
  17. 10
      utils/removeFromArray.js
  18. 10
      utils/requirements.js
  19. 20
      utils/validate.js

6
.eslintrc.json

@ -18,6 +18,10 @@
"error", "error",
"always" "always"
], ],
"prefer-const": "error" "prefer-const": "error",
"prefer-destructuring": "error",
"no-var": "error",
"strict": "error",
"eol-last": "error"
} }
} }

18
actions/createProxyServer.js

@ -1,12 +1,14 @@
var fs = require('fs-extra'); 'use strict';
var shell = require('shelljs');
var npath = require('../utils/nginxPath'); const fs = require('fs-extra');
var conf = require('../utils/nginxConf'); const shell = require('shelljs');
var nginxReload = require('../utils/nginxReload');
var appendToList = require('../utils/listFile').appendToList;
var EOL = require('os').EOL; // \n if used on Linux, \r\n if used on Windows. const npath = require('../utils/nginxPath');
const conf = require('../utils/nginxConf');
const nginxReload = require('../utils/nginxReload');
const { appendToList } = require('../utils/listFile');
const { EOL } = require('os'); // \n if used on Linux, \r\n if used on Windows.
function createProxyServer(domain, inPort, outPort) { function createProxyServer(domain, inPort, outPort) {
outPort = outPort || 80; outPort = outPort || 80;
@ -37,4 +39,4 @@ function createProxyServer(domain, inPort, outPort) {
nginxReload(); nginxReload();
} }
module.exports = createProxyServer; module.exports = createProxyServer;

22
actions/createStaticServer.js

@ -1,14 +1,16 @@
var fs = require('fs-extra'); 'use strict';
var shell = require('shelljs');
var path = require('path');
var npath = require('../utils/nginxPath'); const fs = require('fs-extra');
var conf = require('../utils/nginxConf'); const shell = require('shelljs');
var nginxReload = require('../utils/nginxReload'); const path = require('path');
var appendToList = require('../utils/listFile').appendToList;
var currentPath = path.normalize(process.cwd()); const npath = require('../utils/nginxPath');
var EOL = require('os').EOL; // \n if used on Linux, \r\n if used on Windows. const conf = require('../utils/nginxConf');
const nginxReload = require('../utils/nginxReload');
const { appendToList } = require('../utils/listFile');
const currentPath = path.normalize(process.cwd());
const { EOL } = require('os'); // \n if used on Linux, \r\n if used on Windows.
function createStaticServer(domain, outPort) { function createStaticServer(domain, outPort) {
outPort = outPort || 80; outPort = outPort || 80;
@ -38,4 +40,4 @@ function createStaticServer(domain, outPort) {
nginxReload(); nginxReload();
} }
module.exports = createStaticServer; module.exports = createStaticServer;

10
actions/killALL.js

@ -1,7 +1,9 @@
var shell = require('shelljs'); 'use strict';
var npath = require('../utils/nginxPath'); const shell = require('shelljs');
var conf = require('../utils/nginxConf');
const npath = require('../utils/nginxPath');
const conf = require('../utils/nginxConf');
function killALL () { function killALL () {
shell.rm('-Rf', npath.serversBakUp); shell.rm('-Rf', npath.serversBakUp);
@ -17,4 +19,4 @@ function killALL () {
shell.ln('-sf', npath.confD() + "default.conf", npath.enabledSites() + "default.conf"); // Symlink the default.conf file from confD to sites-enabled shell.ln('-sf', npath.confD() + "default.conf", npath.enabledSites() + "default.conf"); // Symlink the default.conf file from confD to sites-enabled
} }
module.exports = killALL; module.exports = killALL;

6
actions/killAllConfirm.js

@ -1,3 +1,5 @@
'use strict';
const prompt = require('prompt'); const prompt = require('prompt');
const shell = require('shelljs'); const shell = require('shelljs');
@ -8,7 +10,7 @@ function killAllConfirm () {
prompt.start(); prompt.start();
var property = { const property = {
name: 'yesno', name: 'yesno',
message: 'This will completely destroy all configs and reset nginx. Are you sure?', message: 'This will completely destroy all configs and reset nginx. Are you sure?',
validator: /y[es]*|n[o]?/, validator: /y[es]*|n[o]?/,
@ -29,4 +31,4 @@ function killAllConfirm () {
}); });
} }
module.exports = killAllConfirm; module.exports = killAllConfirm;

14
actions/killServer.js

@ -1,9 +1,11 @@
var shell = require('shelljs'); 'use strict';
var npath = require('../utils/nginxPath'); const shell = require('shelljs');
var conf = require('../utils/nginxConf');
var nginxReload = require('../utils/nginxReload'); const npath = require('../utils/nginxPath');
var removeFromList = require('../utils/listFile').removeFromList; const conf = require('../utils/nginxConf');
const nginxReload = require('../utils/nginxReload');
const { removeFromList } = require('../utils/listFile');
function killServer(domain, outPort) { function killServer(domain, outPort) {
shell.rm('-rf', conf(npath.enabledSites(), domain, outPort)); shell.rm('-rf', conf(npath.enabledSites(), domain, outPort));
@ -14,4 +16,4 @@ function killServer(domain, outPort) {
nginxReload(); nginxReload();
} }
module.exports = killServer; module.exports = killServer;

12
actions/listServers.js

@ -1,12 +1,14 @@
var readServers = require('../utils/listFile').readServers; 'use strict';
var prettyjson = require('prettyjson');
var EOL = require('os').EOL; const { readServers } = require('../utils/listFile');
const prettyjson = require('prettyjson');
const { EOL } = require('os');
function listServers() { function listServers() {
var serversList = readServers(); const serversList = readServers();
if(serversList) console.log(EOL + prettyjson.render(serversList) + EOL); if(serversList) console.log(EOL + prettyjson.render(serversList) + EOL);
else console.log("\nNo servers were found! Create some using `up`!\n"); else console.log("\nNo servers were found! Create some using `up`!\n");
} }
module.exports = listServers; module.exports = listServers;

10
build/fetchTLDS.js

@ -1,7 +1,9 @@
var https = require('https'); 'use strict';
var fs = require('fs-extra');
var file = fs.createWriteStream("./assets/tlds.txt"); const https = require('https');
const fs = require('fs-extra');
const file = fs.createWriteStream("./assets/tlds.txt");
https.get("https://data.iana.org/TLD/tlds-alpha-by-domain.txt", function(response) { https.get("https://data.iana.org/TLD/tlds-alpha-by-domain.txt", function(response) {
response.pipe(file); response.pipe(file);
}); });

20
index.js

@ -3,20 +3,20 @@
'use strict'; 'use strict';
// Requiring npm modules // Requiring npm modules
var program = require('commander'); const program = require('commander');
var chalk = require('chalk'); const chalk = require('chalk');
//var fs = require('fs-extra'); //var fs = require('fs-extra');
// Requiring Actions // Requiring Actions
var createProxyServer = require('./actions/createProxyServer'); const createProxyServer = require('./actions/createProxyServer');
var createStaticServer = require('./actions/createStaticServer'); const createStaticServer = require('./actions/createStaticServer');
var killServer = require('./actions/killServer'); const killServer = require('./actions/killServer');
var listServers = require('./actions/listServers'); const listServers = require('./actions/listServers');
var killAllConfirm = require('./actions/killAllConfirm'); const killAllConfirm = require('./actions/killAllConfirm');
// Requiring utils // Requiring utils
var validate = require('./utils/validate'); const validate = require('./utils/validate');
var requirements = require('./utils/requirements'); const requirements = require('./utils/requirements');
// Check for requirements such as OS version and nginx install. Throw and exit if requirements not found. // Check for requirements such as OS version and nginx install. Throw and exit if requirements not found.
// #Roadmap: Add ability to satisfy any possible requirements. // #Roadmap: Add ability to satisfy any possible requirements.
@ -93,4 +93,4 @@ program.on('--help', function () {
}); });
// Parses commands passed to `up` and chooses one of the above commands. // Parses commands passed to `up` and chooses one of the above commands.
program.parse(process.argv); program.parse(process.argv);

14
utils/isFQDN.js

@ -3,7 +3,7 @@
'use strict'; 'use strict';
var fs = require('fs-extra'); const fs = require('fs-extra');
// Official list of TLDs should be fetched from: // Official list of TLDs should be fetched from:
// https://data.iana.org/TLD/tlds-alpha-by-domain.txt // https://data.iana.org/TLD/tlds-alpha-by-domain.txt
@ -14,7 +14,7 @@ var fs = require('fs-extra');
function isFQDN(domain) { function isFQDN(domain) {
// Importing and parsing `tlds.txt` file // Importing and parsing `tlds.txt` file
var tlds = fs.readFileSync('./assets/tlds.txt', 'utf8') const tlds = fs.readFileSync('./assets/tlds.txt', 'utf8')
.split(/[\r\n]+/) .split(/[\r\n]+/)
.filter(x => !x.startsWith('#')); .filter(x => !x.startsWith('#'));
@ -22,19 +22,19 @@ function isFQDN(domain) {
return false; return false;
} }
var labels = domain.split('.').reverse(); const labels = domain.split('.').reverse();
if (labels.length < 2) { if (labels.length < 2) {
return false; return false;
} }
var tld = labels[0]; const [ tld ] = labels;
if (!tlds.includes(tld.toUpperCase())) { if (!tlds.includes(tld.toUpperCase())) {
return false; return false;
} }
for (var label of labels) { for (const label of labels) {
const len = label.length; const len = label.length;
@ -56,6 +56,6 @@ function isFQDN(domain) {
} }
} }
return true; return true;
}; }
module.exports = isFQDN; module.exports = isFQDN;

10
utils/isIP.js

@ -1,14 +1,16 @@
'use strict';
// Parses a string, and returns true if it is an IP Address. // Parses a string, and returns true if it is an IP Address.
function isIP(str) { function isIP(str) {
var segments = str const segments = str
.split(".") .split(".")
.map(Number); .map(Number);
if (!segments.length === 4) { if (!segments.length === 4) {
return false; return false;
} }
for(var i = 0; i < segments.length; i++) { for(let i = 0; i < segments.length; i++) {
var segment = segments[i]; const segment = segments[i];
if (Number.isNaN(segment)) { if (Number.isNaN(segment)) {
return false; return false;
} }
@ -22,4 +24,4 @@ function isIP(str) {
return true; return true;
} }
module.exports = isIP; module.exports = isIP;

22
utils/listFile.js

@ -1,13 +1,15 @@
var fs = require('fs-extra'); 'use strict';
var removeFromArray = require('./removeFromArray'); const fs = require('fs-extra');
var listFilePath = require('./nginxPath').serversUp;
const removeFromArray = require('./removeFromArray');
const listFilePath = require('./nginxPath').serversUp;
function appendToList(domain, outPort, inPort) { function appendToList(domain, outPort, inPort) {
inPort = inPort || undefined; inPort = inPort || undefined;
var jsonFile = { "domains": [] }; let jsonFile = { "domains": [] };
var domBlock = { const domBlock = {
"domain": domain, "domain": domain,
"outPort": outPort "outPort": outPort
}; };
@ -20,7 +22,7 @@ function appendToList(domain, outPort, inPort) {
} }
if (fs.existsSync(listFilePath())) { if (fs.existsSync(listFilePath())) {
var jsonBuffer = JSON.parse(fs.readFileSync(listFilePath())); const jsonBuffer = JSON.parse(fs.readFileSync(listFilePath()));
jsonFile.domains = removeFromArray(jsonBuffer.domains, domain, outPort); jsonFile.domains = removeFromArray(jsonBuffer.domains, domain, outPort);
} }
jsonFile.domains.push(domBlock); jsonFile.domains.push(domBlock);
@ -30,8 +32,8 @@ function appendToList(domain, outPort, inPort) {
function removeFromList (domain, outPort) { function removeFromList (domain, outPort) {
if (fs.existsSync(listFilePath())) { if (fs.existsSync(listFilePath())) {
var jsonFile = { "domains": [] }; let jsonFile = { "domains": [] };
var jsonBuffer = JSON.parse(fs.readFileSync(listFilePath())); const jsonBuffer = JSON.parse(fs.readFileSync(listFilePath()));
jsonFile.domains = removeFromArray(jsonBuffer.domains, domain, outPort); jsonFile.domains = removeFromArray(jsonBuffer.domains, domain, outPort);
jsonFile = JSON.stringify(jsonBuffer, null, '\t'); jsonFile = JSON.stringify(jsonBuffer, null, '\t');
@ -41,7 +43,7 @@ function removeFromList (domain, outPort) {
} }
function readServers () { function readServers () {
var serversList = JSON.parse(fs.readFileSync(listFilePath())); const serversList = JSON.parse(fs.readFileSync(listFilePath()));
if(!serversList.domains[0]) return undefined; if(!serversList.domains[0]) return undefined;
return serversList; return serversList;
@ -49,4 +51,4 @@ function readServers () {
module.exports.appendToList = appendToList; module.exports.appendToList = appendToList;
module.exports.readServers = readServers; module.exports.readServers = readServers;
module.exports.removeFromList = removeFromList; module.exports.removeFromList = removeFromList;

4
utils/nginxConf.js

@ -1,7 +1,9 @@
'use strict';
// Simple function that takes a path and domain name, concatenates them with ".conf" and returns it. // Simple function that takes a path and domain name, concatenates them with ".conf" and returns it.
function conf(path, domain, outPort) { function conf(path, domain, outPort) {
return (path + domain + "." + outPort + ".conf"); return (path + domain + "." + outPort + ".conf");
} }
module.exports = conf; module.exports = conf;

14
utils/nginxPath.js

@ -2,12 +2,12 @@
// These functions just return paths. Later, these should be modified to poll from nginx's config. // These functions just return paths. Later, these should be modified to poll from nginx's config.
var npath = "/etc/nginx/"; const npath = "/etc/nginx/";
var enabled = npath + "sites-enabled/"; const enabled = npath + "sites-enabled/";
var confDpath = npath + "conf.d/"; const confDpath = npath + "conf.d/";
var upPath = "/etc/up-serve/"; const upPath = "/etc/up-serve/";
var wwwRoot = upPath + "static/"; const wwwRoot = upPath + "static/";
var serverListPath = upPath + "servers"; const serverListPath = upPath + "servers";
function nginxPath() { function nginxPath() {
return npath; return npath;
@ -46,4 +46,4 @@ module.exports.enabledSites = enabledSites;
module.exports.webRoot = webRoot; module.exports.webRoot = webRoot;
module.exports.webRootDomain = webRootDomain; module.exports.webRootDomain = webRootDomain;
module.exports.serversUp = serversUp; module.exports.serversUp = serversUp;
module.exports.serversBakUp = serversBakUp; module.exports.serversBakUp = serversBakUp;

6
utils/nginxReload.js

@ -1,4 +1,6 @@
var execSync = require('child_process').execSync; 'use strict';
const { execSync } = require('child_process');
function nginxReload() { function nginxReload() {
execSync('service nginx reload', function (error, stdout, stderr) { execSync('service nginx reload', function (error, stdout, stderr) {
@ -11,4 +13,4 @@ function nginxReload() {
}); });
} }
module.exports = nginxReload; module.exports = nginxReload;

6
utils/parseToInt.js

@ -1,8 +1,10 @@
'use strict';
// Parse an input string and return a number if it is an integer. If it's a float, string, or array, return undefined. // 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) { function parseToInt(inputString) {
var parsing = /^\d+$/.exec(inputString); const parsing = /^\d+$/.exec(inputString);
return (parsing || [])[0]; return (parsing || [])[0];
} }
module.exports = parseToInt; module.exports = parseToInt;

10
utils/removeFromArray.js

@ -1,7 +1,11 @@
'use strict';
function removeFromArray (arr, dom, port) { function removeFromArray (arr, dom, port) {
var shouldDelete = []; let shouldDelete = [];
for(var i = 0; i < arr.length; i++) if((arr[i].domain == dom) && (arr[i].outPort == port)) shouldDelete = [true, i]; for(let i = 0; i < arr.length; i++)
if((arr[i].domain == dom) && (arr[i].outPort == port))
shouldDelete = [true, i];
if (shouldDelete[0]) { if (shouldDelete[0]) {
arr.splice(shouldDelete[1], 1); arr.splice(shouldDelete[1], 1);
@ -9,4 +13,4 @@ function removeFromArray (arr, dom, port) {
return arr; return arr;
} }
module.exports = removeFromArray; module.exports = removeFromArray;

10
utils/requirements.js

@ -1,10 +1,12 @@
var shell = require('shelljs'); 'use strict';
var chalk = require('chalk');
const shell = require('shelljs');
const chalk = require('chalk');
function requirements() { function requirements() {
// Detect Linux or BSD // Detect Linux or BSD
var isLin = /^linux|^bsd/.test(process.platform); const 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. // 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) { if(!isLin) {
@ -20,4 +22,4 @@ function requirements() {
} }
module.exports = requirements; module.exports = requirements;

20
utils/validate.js

@ -1,8 +1,10 @@
var parseToInt = require('./parseToInt'); 'use strict';
var isIP = require('./isIP');
const parseToInt = require('./parseToInt');
const isIP = require('./isIP');
// Using Validator // Using Validator
var isDomain = require('./isFQDN'); const isDomain = require('./isFQDN');
function validate(domain, inPort, outPort) { function validate(domain, inPort, outPort) {
// //
@ -10,15 +12,15 @@ function validate(domain, inPort, outPort) {
outPort = outPort || 80; outPort = outPort || 80;
// Error messages // Error messages
var domainInvalidMsg = ["\nPlease use a domain name instead of an IP address.", "\nDomain is not valid. Please use a valid domain name."]; const 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."]; const 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. // 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); const validInPort = parseToInt(inPort);
var validOutPort = parseToInt(outPort); const 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. // 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; let isValid = true;
// Throw if IP is given instead of domain name. // Throw if IP is given instead of domain name.
if (isIP(domain)) { if (isIP(domain)) {
@ -60,4 +62,4 @@ function validate(domain, inPort, outPort) {
} }
} }
module.exports = validate; module.exports = validate;

Loading…
Cancel
Save