Browse Source

Enforcing max width and some line ending fixes

pull/6/head
Thomas Rory Gummerson 7 years ago
parent
commit
ecf735430c
  1. 3
      .eslintrc.json
  2. 7
      actions/createProxyServer.js
  3. 22
      actions/createStaticServer.js
  4. 7
      actions/killALL.js
  5. 8
      actions/killAllConfirm.js
  6. 4
      actions/listServers.js
  7. 5
      build/fetchTLDS.js
  8. 64
      index.js
  9. 3
      utils/nginxConf.js
  10. 3
      utils/nginxPath.js
  11. 3
      utils/parseToInt.js
  12. 16
      utils/requirements.js
  13. 46
      utils/validate.js

3
.eslintrc.json

@ -22,6 +22,7 @@
"prefer-destructuring": "error",
"no-var": "error",
"strict": "error",
"eol-last": "error"
"eol-last": "error",
"max-len": "error"
}
}

7
actions/createProxyServer.js

@ -32,8 +32,11 @@ function createProxyServer(domain, inPort, outPort) {
"}"
);
shell.mkdir('-p', npath.confD());
shell.mkdir('-p', npath.enabledSites()); // Creates directories if doesn't exist
shell.ln('-sf', conf(npath.confD(), domain, outPort), conf(npath.enabledSites(), domain, outPort)); // Symlink the conf file from sites-available to sites-enabled
shell.mkdir('-p', npath.enabledSites());
// Creates directories if doesn't exist
shell.ln('-sf', conf(npath.confD(), domain, outPort),
conf(npath.enabledSites(), domain, outPort));
// Symlink the conf file from sites-available to sites-enabled
appendToList(domain, outPort, inPort);
nginxReload();

22
actions/createStaticServer.js

@ -16,7 +16,8 @@ function createStaticServer(domain, outPort) {
outPort = outPort || 80;
shell.mkdir('-p', npath.confD());
fs.outputFileSync((conf(npath.confD(), domain, outPort)), // Gets nginx's paths from nginxPath.js
fs.outputFileSync((conf(npath.confD(), domain, outPort)),
// Gets nginx's paths from nginxPath.js
"server {" + EOL +
" listen " + outPort + ";" + EOL +
" listen [::]:" + outPort + ";" + EOL +
@ -29,12 +30,19 @@ function createStaticServer(domain, outPort) {
" }" + EOL +
"}"
);
shell.mkdir('-p', npath.enabledSites()); // Creates directories if doesn't exist
shell.rm('-rf', conf(npath.enabledSites(), domain, outPort)); // Removes domain from sites-enabled if exists
shell.ln('-sf', conf(npath.confD(), domain, outPort), conf(npath.enabledSites(), domain, outPort)); // Symlink the conf file from confD 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
shell.mkdir('-p', npath.enabledSites());
// Creates directories if doesn't exist
shell.rm('-rf', conf(npath.enabledSites(), domain, outPort));
// Removes domain from sites-enabled if exists
shell.ln('-sf', conf(npath.confD(), domain, outPort),
conf(npath.enabledSites(), domain, outPort));
// Symlink the conf file from confD 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
appendToList(domain, outPort);
nginxReload();

7
actions/killALL.js

@ -15,8 +15,11 @@ function killALL () {
shell.mkdir('-p', npath.confD());
shell.mkdir('-p', npath.enabledSites());
shell.mkdir('-p', npath.webRoot());
shell.cp('./build/defaultNginx.conf', conf(npath.confD())); // Create the default.conf file
shell.ln('-sf', npath.confD() + "default.conf", npath.enabledSites() + "default.conf"); // Symlink the default.conf file from confD to sites-enabled
shell.cp('./build/defaultNginx.conf', conf(npath.confD()));
// Create the default.conf file
shell.ln('-sf', npath.confD() + "default.conf",
npath.enabledSites() + "default.conf");
// Symlink the default.conf file from confD to sites-enabled
}
module.exports = killALL;

8
actions/killAllConfirm.js

@ -1,5 +1,7 @@
'use strict';
const { EOL } = require('os');
const prompt = require('prompt');
const shell = require('shelljs');
@ -12,7 +14,8 @@ function killAllConfirm () {
const property = {
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]?/,
warning: 'Must respond yes or no',
default: 'no'
@ -26,7 +29,8 @@ function killAllConfirm () {
else {
console.log("Deleting all servers...");
killAll();
console.log("\nDone. All configs have been destroyed. Hope you're happy.");
console.log(EOL +
"Done. All configs have been destroyed. Hope you're happy.");
}
});
}

4
actions/listServers.js

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

5
build/fetchTLDS.js

@ -4,6 +4,5 @@ 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) {
response.pipe(file);
});
https.get("https://data.iana.org/TLD/tlds-alpha-by-domain.txt",response =>
response.pipe(file));

64
index.js

@ -2,6 +2,8 @@
'use strict';
const { EOL } = require('os');
// Requiring npm modules
const program = require('commander');
const chalk = require('chalk');
@ -18,10 +20,12 @@ const killAllConfirm = require('./actions/killAllConfirm');
const validate = require('./utils/validate');
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.
requirements(); // Comment in development and uncomment this line in production. This should check whether the OS is compatible with this version of `up`
requirements(); // Comment in development and uncomment this line in production.
// This should check whether the OS is compatible with this version of `up`
program
.version('0.1.5');
@ -29,23 +33,40 @@ program
program
.command('static <domain> [outPort]')
.description('Create a static server at this folder.')
.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.
.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);
if (outPort != "80" || "443") domain = domain + ":" + outPort;
console.log("\nDone! Your static server has been set up!\nPoint your domain to this server and check " + chalk.cyan(domain) + " to verify!");
console.log(EOL + [
"Done! Your static server has been set up!",
"Point your domain to this server and check " +
chalk.cyan(domain) +
" to verify!"
].join(EOL));
});
program
.command('proxy <domain> <inPort> [outPort]')
.description('Create a proxy server, listening at port number.')
.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.
.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);
if (outPort != "80" || "443") domain = domain + ":" + outPort;
console.log("\nDone! Your reverse proxy server has been set up!\nPoint your domain to this server and check " + chalk.cyan(domain) + " to verify!");
console.log(EOL + [
"Done! Your reverse proxy server has been set up!",
"Point your domain to this server and check " +
chalk.cyan(domain) +
" to verify!"
].join(EOL));
});
program
@ -59,9 +80,10 @@ program
.command('kill <domain> [ourPort]')
.description('Kill a server.')
.action(function (domain, outPort) {
outPort = outPort || "80"; // This is a string because regex needs to validate it.
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");
console.log(EOL + "Done! Your server has been killed!"+ EOL);
});
program
@ -69,14 +91,18 @@ program
.description('Warning! Will completely kill all servers and reset nginx')
.action(function() {
killAllConfirm();
console.log("\nA backup of your old servers.up is saved in /etc/up-serve/servers.bak.up.\n" +
"Check this if you need to.\n");
console.log(EOL + [
"A backup of your old servers.up is " +
"saved in /etc/up-serve/servers.bak.up.",
"Check this if you need to."
].join(EOL) + EOL);
});
program
.command('*') // This should pick invalid commands, but it doesn't, yet.
.action(function () {
console.log("\nInvalid command. Type " + chalk.cyan('up --help') + " for help.\n");
console.log(EOL + "Invalid command. Type " +
chalk.cyan('up --help') + " for help." + EOL);
});
// Adds custom help text to the automatically generated help.
@ -84,10 +110,16 @@ program.on('--help', function () {
console.log('');
console.log(' Usage:');
console.log('');
console.log(' ', chalk.yellow('$ up'), chalk.cyan('static'), chalk.blue('domain-name'));
console.log(' ',
chalk.yellow('$ up'),
chalk.cyan('static'),
chalk.blue('domain-name'));
console.log(' Set up a static server at domain-name');
console.log('');
console.log(' ', chalk.yellow('$ up'), chalk.cyan('proxy'), chalk.blue('domain-name port-number'));
console.log(' ',
chalk.yellow('$ up'),
chalk.cyan('proxy'),
chalk.blue('domain-name port-number'));
console.log(' Set up a proxy server listening at port-number');
console.log('');
});

3
utils/nginxConf.js

@ -1,6 +1,7 @@
'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) {
return (path + domain + "." + outPort + ".conf");

3
utils/nginxPath.js

@ -1,6 +1,7 @@
'use strict';
// 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.
const npath = "/etc/nginx/";
const enabled = npath + "sites-enabled/";

3
utils/parseToInt.js

@ -1,6 +1,7 @@
'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) {
const parsing = /^\d+$/.exec(inputString);

16
utils/requirements.js

@ -1,5 +1,7 @@
'use strict';
const { EOL } = require('os');
const shell = require('shelljs');
const chalk = require('chalk');
@ -8,15 +10,23 @@ function requirements() {
// Detect Linux or BSD
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) {
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.echo(EOL +
"This 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.echo(
'I need nginx to work. Install nginx first. https://nginx.org/');
shell.exit(1);
}

46
utils/validate.js

@ -1,5 +1,7 @@
'use strict';
const { EOL } = require('os');
const parseToInt = require('./parseToInt');
const isIP = require('./isIP');
@ -12,14 +14,26 @@ function validate(domain, inPort, outPort) {
outPort = outPort || 80;
// Error messages
const domainInvalidMsg = ["\nPlease use a domain name instead of an IP address.", "\nDomain is not valid. Please use a valid domain name."];
const portInvalidMsg = ["\nPort should be a number.", "\nPort should be a number from 1 and 65535."];
const domainInvalidMsg = [
EOL + "Please use a domain name instead of an IP address.",
EOL + "Domain is not valid. Please use a valid domain name."
];
const portInvalidMsg = [
EOL + "Port should be a number.",
EOL + "Port 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.
const validInPort = parseToInt(inPort);
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.
let isValid = true;
// Throw if IP is given instead of domain name.
@ -34,31 +48,41 @@ function validate(domain, inPort, outPort) {
return isValid = false;
}
// Enter if `inPort` is not defined. This happens for `up static` where no inbound ports are required.
// 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.
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.
// 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.
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.
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`.
return isValid;
// If any of the `if`s were true, `isInvalid = false`.
// If not, `isInvalid = true`.
}
}

Loading…
Cancel
Save