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. 6
      actions/createProxyServer.js
  3. 9
      actions/createStaticServer.js
  4. 15
      index.js
  5. 5
      util/isIP.js
  6. 2
      util/nginxPath.js
  7. 10
      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"
]
}
}

6
actions/createProxyServer.js

@ -5,7 +5,7 @@ var npath = require('../util/nginxPath');
var conf = require('../util/nginxConf'); var conf = require('../util/nginxConf');
var nginxReload = require('../util/nginxReload'); var nginxReload = require('../util/nginxReload');
var { EOL } = require('os'); // \n if used on Linux, \r\n if used on Windows. var EOL = require('os').EOL; // \n if used on Linux, \r\n if used on Windows.
function createProxyServer(domain, inPort, outPort) { function createProxyServer(domain, inPort, outPort) {
fs.outputFileSync((conf(npath.availableSites(), domain, outPort)), fs.outputFileSync((conf(npath.availableSites(), domain, outPort)),
@ -24,11 +24,11 @@ function createProxyServer(domain, inPort, outPort) {
" proxy_cache_bypass $http_upgrade;" + EOL + " proxy_cache_bypass $http_upgrade;" + EOL +
" }" + EOL + " }" + EOL +
"}" "}"
) );
shell.mkdir('-p', npath.enabledSites()); // Creates directory if doesn't exist 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 shell.ln('-sf', conf(npath.availableSites(), domain, outPort), conf(npath.enabledSites(), domain, outPort)); // Symlink the conf file from sites-available to sites-enabled
nginxReload(); nginxReload();
}; }
module.exports = createProxyServer; module.exports = createProxyServer;

9
actions/createStaticServer.js

@ -7,9 +7,10 @@ var conf = require('../util/nginxConf');
var nginxReload = require('../util/nginxReload'); var nginxReload = require('../util/nginxReload');
var currentPath = path.normalize(process.cwd()); var currentPath = path.normalize(process.cwd());
var { EOL } = require('os'); // \n if used on Linux, \r\n if used on Windows. var EOL = require('os').EOL; // \n if used on Linux, \r\n if used on Windows.
function createStaticServer(domain, outPort = 80) { function createStaticServer(domain, outPort) {
outPort = outPort || 80;
fs.outputFileSync((conf(npath.availableSites(), domain, outPort)), // Gets nginx's paths from nginxPath.js fs.outputFileSync((conf(npath.availableSites(), domain, outPort)), // Gets nginx's paths from nginxPath.js
"server {" + EOL + "server {" + EOL +
" listen " + outPort + ";" + EOL + " listen " + outPort + ";" + EOL +
@ -22,7 +23,7 @@ function createStaticServer(domain, outPort = 80) {
" try_files $uri $uri/ =404;" + EOL + " try_files $uri $uri/ =404;" + EOL +
" }" + EOL + " }" + EOL +
"}" "}"
) );
shell.mkdir('-p', npath.enabledSites()); // Creates directory if doesn't exist 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.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.ln('-sf', conf(npath.availableSites(), domain, outPort), conf(npath.enabledSites(), domain, outPort)); // Symlink the conf file from sites-available to sites-enabled
@ -31,6 +32,6 @@ function createStaticServer(domain, outPort = 80) {
shell.ln('-sf', currentPath, npath.webRootDomain(domain, outPort)); // Symlink current directory to nginx's web root shell.ln('-sf', currentPath, npath.webRootDomain(domain, outPort)); // Symlink current directory to nginx's web root
nginxReload(); nginxReload();
}; }
module.exports = createStaticServer; module.exports = createStaticServer;

15
index.js

@ -2,13 +2,11 @@
// Requiring npm modules // Requiring npm modules
var program = require('commander'); var program = require('commander');
var shell = require('shelljs');
var fs = require('fs-extra');
var chalk = require('chalk'); var chalk = require('chalk');
// Requiring utils // Requiring utils
var validate = require('./util/validate'); var validate = require('./util/validate');
var requirements = require('./util/requirements') var requirements = require('./util/requirements');
// Requiring Actions // Requiring Actions
var createProxyServer = require('./actions/createProxyServer'); var createProxyServer = require('./actions/createProxyServer');
@ -24,7 +22,8 @@ program
program program
.command('static <domain> [outPort]') .command('static <domain> [outPort]')
.description('Create a static server at this folder.') .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. if (!validate(domain, outPort)) return; //Validates domain and outport, and if invalid, throws and returns.
createStaticServer(domain, outPort); 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!"); 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 program
.command('proxy <domain> <inPort> [outPort]') .command('proxy <domain> <inPort> [outPort]')
.description('Create a proxy server, listening at port number.') .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; if (!validate(domain, inPort, outPort)) return;
createProxyServer(domain, inPort, outPort); 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!"); 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 program
.command('kill <domain> [ourPort]') .command('kill <domain> [ourPort]')
.description('Kill a server.') .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); killServer(domain, outPort);
console.log("\nDone! Your server has been killed!\n"); console.log("\nDone! Your server has been killed!\n");
}); });
@ -57,7 +58,7 @@ program
program program
.command('*') // This should pick invalid commands, but it doesn't, yet. .command('*') // This should pick invalid commands, but it doesn't, yet.
.action(function () { .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. // Adds custom help text to the automatically generated help.

5
util/isIP.js

@ -2,12 +2,13 @@
function isIP(str) { function isIP(str) {
var segments = str var segments = str
.split('.') .split(".")
.map(Number); .map(Number);
if (!segments.length === 4) { if (!segments.length === 4) {
return false; return false;
} }
for (var segment of segments) { for(var i = 0; i < segments.length; i++) {
var segment = segments[i];
if (Number.isNaN(segment)) { if (Number.isNaN(segment)) {
return false; return false;
} }

2
util/nginxPath.js

@ -17,7 +17,7 @@ function webRoot() {
} }
function webRootDomain(domain, outPort) { function webRootDomain(domain, outPort) {
rootWithDomain = wwwRoot + domain + "." + outPort; var rootWithDomain = wwwRoot + domain + "." + outPort;
return rootWithDomain; return rootWithDomain;
} }

10
util/nginxReload.js

@ -1,14 +1,14 @@
var { execSync } = require('child_process'); var execSync = require('child_process').execSync;
function nginxReload() { function nginxReload() {
execSync('service nginx reload', function (error, stdout, stderr) { execSync('service nginx reload', function (error, stdout, stderr) {
if (error) { if (error) {
console.error(`exec error: ${error}`); console.error("exec error: " + error);
console.log(`stdout: ${stdout}`); console.log("stdout: " + stdout);
console.log(`stderr: ${stderr}`); console.log("stderr: " + stderr);
return; return;
} }
}) });
} }
module.exports = nginxReload; module.exports = nginxReload;

5
util/validate.js

@ -5,7 +5,10 @@ var isIP = require('./isIP');
// Using Validator // Using Validator
var isDomain = validator.isFQDN; var isDomain = validator.isFQDN;
function validate(domain, inPort = undefined, outPort = "80") { function validate(domain, inPort, outPort) {
//
inPort = inPort || undefined;
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."]; 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