Browse Source

Linting and refactoring to consistent style

tags/v0.2.0
Muthu Kumar 7 years ago
parent
commit
22537db6d1
  1. 21
      .eslintrc.json
  2. 66
      actions/createProxyServer.js
  3. 71
      actions/createStaticServer.js
  4. 15
      index.js
  5. 23
      util/isIP.js
  6. 52
      util/nginxPath.js
  7. 16
      util/nginxReload.js
  8. 5
      util/validate.js

21
.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"
]
}
}

66
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;

71
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;

15
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 <domain> [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 <domain> <inPort> [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 <domain> [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.

23
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;
}

52
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;

16
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;

5
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."];

Loading…
Cancel
Save