Browse Source

appendToList Test

pull/3/head
Shanmugam 7 years ago
parent
commit
9ba538798b
  1. 116
      index.js
  2. 6
      utils/listFile.js

116
index.js

@ -7,74 +7,80 @@ var chalk = require('chalk');
// Requiring utils // Requiring utils
var validate = require('./utils/validate'); var validate = require('./utils/validate');
var requirements = require('./utils/requirements'); var requirements = require('./utils/requirements');
var appendToList = require('./utils/listFile');
// Requiring Actions // Requiring Actions
var createProxyServer = require('./actions/createProxyServer'); var createProxyServer = require('./actions/createProxyServer');
var createStaticServer = require('./actions/createStaticServer'); var createStaticServer = require('./actions/createStaticServer');
var killServer = require('./actions/killServer'); var killServer = require('./actions/killServer');
appendToList("example.com", "80");
appendToList("example2.com", "80", "4000");
// 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. // 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 // program
.version('0.1.5'); // .version('0.1.5');
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) { //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. // 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);
if (outPort != "80" || "443") domain = domain + ":" + outPort; // if (outPort != "80" || "443") domain = 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!");
}); // });
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) { //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. // 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);
if (outPort != "80" || "443") domain = domain + ":" + outPort; // if (outPort != "80" || "443") domain = domain + ":" + 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!");
}); // });
program // program
.command('list') // .command('list')
.description('List all available servers.') // .description('List all available servers.')
.action(function () { // .action(function () {
// Stuff happens here // // Stuff happens here
}); // });
program // program
.command('kill <domain> [ourPort]') // .command('kill <domain> [ourPort]')
.description('Kill a server.') // .description('Kill a server.')
.action(function (domain, outPort) { // .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); // killServer(domain, outPort);
console.log("\nDone! Your server has been killed!\n"); // console.log("\nDone! Your server has been killed!\n");
}); // });
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.
program.on('--help', function () { // program.on('--help', function () {
console.log(''); // console.log('');
console.log(' Usage:'); // console.log(' Usage:');
console.log(''); // 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(' Set up a static server at domain-name');
console.log(''); // 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(' Set up a proxy server listening at port-number');
console.log(''); // console.log('');
}); // });
// 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);

6
utils/listFile.js

@ -1,12 +1,12 @@
// These functions just return paths. // These functions just return paths.
var fs = require('fs-extra'); var fs = require('fs-extra');
var shell = require('shelljs'); var shell = require('shelljs');
var EOL = require('os').EOL; // \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.
var listFilePath = 'etc/up-serve/servers.up'; var listFilePath = 'etc/up-serve/servers.up';
function list(domain, inPort, outPort) { function appendToList(domain, outPort, inPort) {
fs.ensureFileSync(listFilePath) // Creates directory if doesn't exist fs.ensureFileSync(listFilePath) // Creates directory if doesn't exist
if (inPort) { if (inPort) {
@ -19,4 +19,4 @@ function list(domain, inPort, outPort) {
fs.appendFileSync(listFilePath, addr); fs.appendFileSync(listFilePath, addr);
} }
module.exports = listFile; module.exports = appendToList;
Loading…
Cancel
Save