Browse Source

Refactor into API and CLI

pull/10/head
Muthu Kumar 7 years ago
parent
commit
b45e7d3d17
  1. 6
      .vscode/settings.json
  2. 71
      api.js
  3. 103
      cli.js
  4. 12
      index.js
  5. 139
      package-lock.json
  6. 3
      package.json

6
.vscode/settings.json

@ -1,5 +1,3 @@
{
"editor.insertSpaces": false,
"editor.tabSize": 4,
"files.eol": "\n"
{
}

71
api.js

@ -0,0 +1,71 @@
'use strict';
const { EOL } = require('os');
// Requiring npm modules
const chalk = require('chalk');
// Requiring Actions
const createProxyServer = require('./actions/createProxyServer');
const createStaticServer = require('./actions/createStaticServer');
const killServer = require('./actions/killServer');
const listServers = require('./actions/listServers');
const killAllConfirm = require('./actions/killAllConfirm');
// Requiring utils
const validate = require('./utils/validate');
function server (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(EOL + [
"Done! Your static server has been set up!",
"Point your domain to this server and check " +
chalk.cyan(domain) +
" to verify!"
].join(EOL));
}
function proxy (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(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));
}
function list () {
listServers();
}
function kill (domain, outPort) {
outPort = outPort || "80";
// This is a string because regex needs to validate it.
killServer(domain, outPort);
console.log(EOL + "Done! Your server has been killed!"+ EOL);
}
function reset () {
killAllConfirm();
}
module.exports = {
server,
proxy,
list,
kill,
reset
};

103
cli.js

@ -0,0 +1,103 @@
#!/usr/bin/env node
'use strict';
const { EOL } = require('os');
// Requiring npm modules
const program = require('commander');
const chalk = require('chalk');
// Require API
const up = require('./api');
// Requiring utils
const requirements = require('./utils/requirements');
// 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`
let cmdValue;
program
.version('0.2.1')
.arguments('<cmd>')
.action(function (cmd) {
cmdValue = cmd;
});
program
.command('static <domain> [outPort]')
.description('Create a static server at this folder.')
.action(function (domain, outPort) {
up.server(domain, outPort);
});
program
.command('proxy <domain> <inPort> [outPort]')
.description('Create a proxy server, listening at port number.')
.action(function (domain, inPort, outPort) {
up.proxy(domain, inPort, outPort);
});
program
.command('list')
.description('List all available servers.')
.action(function () {
up.list();
});
program
.command('kill <domain> [ourPort]')
.description('Kill a server.')
.action(function (domain, outPort) {
up.kill(domain, outPort);
});
program
.command('kill-all')
.description('Warning! Will completely kill all servers and reset nginx')
.action(function() {
up.reset();
});
program
.command('*') // This should pick invalid commands, but it doesn't, yet.
.action(function () {
console.log(EOL + "Invalid command. Type " +
chalk.cyan('up --help') + " for help." + EOL);
});
// Adds custom help text to the automatically generated help.
program.on('--help', function () {
console.log(EOL
+ ' Usage:'
+ EOL
+ EOL
+ ' ' + chalk.yellow('$ up ')
+ chalk.cyan('static')
+ chalk.blue('[domain-name]')
+ EOL
+ ' Set up a static server at domain-name'
+ EOL
+ EOL
+ ' ' + chalk.yellow('$ up ')
+ chalk.cyan('proxy')
+ chalk.blue('[domain-name] <port-number>')
+ EOL
+ ' Set up a proxy server listening at port-number'
+ EOL);
});
// Parses commands passed to `up` and chooses one of the above commands.
program.parse(process.argv);
if (typeof cmdValue === 'undefined') {
console.log(EOL + "No command was given. `up --help` for help info.");
process.exit(1);
}

12
index.js

@ -25,14 +25,11 @@ const requirements = require('./utils/requirements');
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.2.1');
let cmdValue;
program
.version('0.1.0')
.version('0.2.1')
.arguments('<cmd>')
.action(function (cmd) {
cmdValue = cmd;
@ -113,14 +110,15 @@ program.on('--help', function () {
console.log(EOL
+ ' Usage:'
+ EOL
+ EOL +
+ ' ' + chalk.yellow('$ up') + chalk.cyan('static')
+ EOL
+ ' ' + chalk.yellow('$ up ')
+ chalk.cyan('static')
+ chalk.blue('[domain-name]')
+ EOL
+ ' Set up a static server at domain-name'
+ EOL
+ EOL
+ ' ' + chalk.yellow('$ up')
+ ' ' + chalk.yellow('$ up ')
+ chalk.cyan('proxy')
+ chalk.blue('[domain-name] <port-number>')
+ EOL

139
package-lock.json

@ -95,11 +95,6 @@
"integrity": "sha1-iYUI2iIm84DfkEcoRWhJwVAaSw0=",
"dev": true
},
"async": {
"version": "0.9.2",
"resolved": "https://registry.npmjs.org/async/-/async-0.9.2.tgz",
"integrity": "sha1-rqdNXmHB+JlhO/ZL2mbUx48v0X0="
},
"babel-code-frame": {
"version": "6.26.0",
"resolved": "https://registry.npmjs.org/babel-code-frame/-/babel-code-frame-6.26.0.tgz",
@ -269,11 +264,6 @@
"which": "1.3.0"
}
},
"cycle": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/cycle/-/cycle-1.0.3.tgz",
"integrity": "sha1-IegLK+hYD5i0aPN5QwZisEbDStI="
},
"debug": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
@ -283,11 +273,6 @@
"ms": "2.0.0"
}
},
"deep-equal": {
"version": "0.2.2",
"resolved": "https://registry.npmjs.org/deep-equal/-/deep-equal-0.2.2.tgz",
"integrity": "sha1-hLdFiW80xoTpjyzg5Cq69Du6AX0="
},
"deep-is": {
"version": "0.1.3",
"resolved": "https://registry.npmjs.org/deep-is/-/deep-is-0.1.3.tgz",
@ -437,11 +422,6 @@
"tmp": "0.0.33"
}
},
"eyes": {
"version": "0.1.8",
"resolved": "https://registry.npmjs.org/eyes/-/eyes-0.1.8.tgz",
"integrity": "sha1-Ys8SAjTGg3hdkCNIqADvPgzCC8A="
},
"fast-deep-equal": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-1.0.0.tgz",
@ -564,11 +544,6 @@
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-2.0.0.tgz",
"integrity": "sha1-6CB68cx7MNRGzHC3NLXovhj4jVE="
},
"i": {
"version": "0.3.6",
"resolved": "https://registry.npmjs.org/i/-/i-0.3.6.tgz",
"integrity": "sha1-2WyScyB28HJxG2sQ/X1PZa2O4j0="
},
"iconv-lite": {
"version": "0.4.19",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.19.tgz",
@ -685,11 +660,6 @@
"integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=",
"dev": true
},
"isstream": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz",
"integrity": "sha1-R+Y/evVa+m+S4VAOaQ64uFKcCZo="
},
"js-tokens": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-3.0.2.tgz",
@ -784,12 +754,14 @@
"minimist": {
"version": "0.0.8",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz",
"integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0="
"integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=",
"dev": true
},
"mkdirp": {
"version": "0.5.1",
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz",
"integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=",
"dev": true,
"requires": {
"minimist": "0.0.8"
}
@ -803,7 +775,8 @@
"mute-stream": {
"version": "0.0.7",
"resolved": "https://registry.npmjs.org/mute-stream/-/mute-stream-0.0.7.tgz",
"integrity": "sha1-MHXOk7whuPq0PhvE2n6BFe0ee6s="
"integrity": "sha1-MHXOk7whuPq0PhvE2n6BFe0ee6s=",
"dev": true
},
"natural-compare": {
"version": "1.4.0",
@ -811,11 +784,6 @@
"integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=",
"dev": true
},
"ncp": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/ncp/-/ncp-1.0.1.tgz",
"integrity": "sha1-0VNn5cuHQyuhF9K/gP30Wuz7QkY="
},
"object-assign": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
@ -896,11 +864,6 @@
"pinkie": "2.0.4"
}
},
"pkginfo": {
"version": "0.4.1",
"resolved": "https://registry.npmjs.org/pkginfo/-/pkginfo-0.4.1.tgz",
"integrity": "sha1-tUGO8EOd5UJfxJlQQtztFPsqhP8="
},
"pluralize": {
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/pluralize/-/pluralize-7.0.0.tgz",
@ -941,33 +904,12 @@
"integrity": "sha1-ihvjZr+Pwj2yvSPxDG/pILQ4nR8=",
"dev": true
},
"prompt": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/prompt/-/prompt-1.0.0.tgz",
"integrity": "sha1-jlcSPDlquYiJf7Mn/Trtw+c15P4=",
"requires": {
"colors": "1.1.2",
"pkginfo": "0.4.1",
"read": "1.0.7",
"revalidator": "0.1.8",
"utile": "0.3.0",
"winston": "2.1.1"
}
},
"pseudomap": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz",
"integrity": "sha1-8FKijacOYYkX7wqKw0wa5aaChrM=",
"dev": true
},
"read": {
"version": "1.0.7",
"resolved": "https://registry.npmjs.org/read/-/read-1.0.7.tgz",
"integrity": "sha1-s9oZvQUkMal2cdRKQmNK33ELQMQ=",
"requires": {
"mute-stream": "0.0.7"
}
},
"readable-stream": {
"version": "2.3.3",
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.3.tgz",
@ -1030,15 +972,11 @@
"signal-exit": "3.0.2"
}
},
"revalidator": {
"version": "0.1.8",
"resolved": "https://registry.npmjs.org/revalidator/-/revalidator-0.1.8.tgz",
"integrity": "sha1-/s5hv6DBtSoga9axgZgYS91SOjs="
},
"rimraf": {
"version": "2.6.2",
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.6.2.tgz",
"integrity": "sha512-lreewLK/BlghmxtfH36YYVg1i8IAce4TI7oao75I1g245+6BctqTVQiBP3YUJ9C6DQOXJmkYR9X9fCLtCOJc5w==",
"dev": true,
"requires": {
"glob": "7.1.2"
}
@ -1125,10 +1063,14 @@
"integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=",
"dev": true
},
"stack-trace": {
"version": "0.0.10",
"resolved": "https://registry.npmjs.org/stack-trace/-/stack-trace-0.0.10.tgz",
"integrity": "sha1-VHxws0fo0ytOEI6hoqFZ5f3eGcA="
"string_decoder": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.0.3.tgz",
"integrity": "sha512-4AH6Z5fzNNBcH+6XDMfA/BTt87skxqJlO0lAh3Dker5zThcAxG6mKz+iGu308UKoPPQ8Dcqx/4JhujzltRa+hQ==",
"dev": true,
"requires": {
"safe-buffer": "5.1.1"
}
},
"string-width": {
"version": "2.1.1",
@ -1140,15 +1082,6 @@
"strip-ansi": "4.0.0"
}
},
"string_decoder": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.0.3.tgz",
"integrity": "sha512-4AH6Z5fzNNBcH+6XDMfA/BTt87skxqJlO0lAh3Dker5zThcAxG6mKz+iGu308UKoPPQ8Dcqx/4JhujzltRa+hQ==",
"dev": true,
"requires": {
"safe-buffer": "5.1.1"
}
},
"strip-ansi": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-4.0.0.tgz",
@ -1247,19 +1180,6 @@
"integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=",
"dev": true
},
"utile": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/utile/-/utile-0.3.0.tgz",
"integrity": "sha1-E1LDQOuCDk2N26A5pPv6oy7U7zo=",
"requires": {
"async": "0.9.2",
"deep-equal": "0.2.2",
"i": "0.3.6",
"mkdirp": "0.5.1",
"ncp": "1.0.1",
"rimraf": "2.6.2"
}
},
"which": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/which/-/which-1.3.0.tgz",
@ -1269,37 +1189,6 @@
"isexe": "2.0.0"
}
},
"winston": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/winston/-/winston-2.1.1.tgz",
"integrity": "sha1-PJNJ0ZYgf9G9/51LxD73JRDjoS4=",
"requires": {
"async": "1.0.0",
"colors": "1.0.3",
"cycle": "1.0.3",
"eyes": "0.1.8",
"isstream": "0.1.2",
"pkginfo": "0.3.1",
"stack-trace": "0.0.10"
},
"dependencies": {
"async": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/async/-/async-1.0.0.tgz",
"integrity": "sha1-+PwEyjoTeErenhZBr5hXjPvWR6k="
},
"colors": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/colors/-/colors-1.0.3.tgz",
"integrity": "sha1-BDP0TYCWgP3rYO0mDxsMJi6CpAs="
},
"pkginfo": {
"version": "0.3.1",
"resolved": "https://registry.npmjs.org/pkginfo/-/pkginfo-0.3.1.tgz",
"integrity": "sha1-Wyn2qB9wcXFC4J52W76rl7T4HiE="
}
}
},
"wordwrap": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-1.0.0.tgz",

3
package.json

@ -19,7 +19,7 @@
"block"
],
"bin": {
"up": "./index.js"
"up": "./cli.js"
},
"author": "Muthu Kumar (@MKRhere)",
"license": "MIT",
@ -32,7 +32,6 @@
"commander": "^2.11.0",
"fs-extra": "^4.0.2",
"prettyjson": "^1.2.1",
"prompt": "^1.0.0",
"readline-sync": "^1.4.7",
"shelljs": "^0.7.8"
},

Loading…
Cancel
Save