19 changed files with 375 additions and 409 deletions
			
			
		| @ -0,0 +1,129 @@ | |||||
|  | #!/usr/bin/env node
 | ||||
|  | 
 | ||||
|  | 'use strict'; | ||||
|  | 
 | ||||
|  | const { EOL } = require('os'); | ||||
|  | const path = require('path'); | ||||
|  | 
 | ||||
|  | // Requiring npm modules
 | ||||
|  | const program = require('commander'); | ||||
|  | const chalk = require('chalk'); | ||||
|  | 
 | ||||
|  | // Require API
 | ||||
|  | const up = require('./lib'); | ||||
|  | const killAllConfirm = require('./actions/killAllConfirm'); | ||||
|  | 
 | ||||
|  | // Requiring utils
 | ||||
|  | const requirements = require('./utils/requirements'); | ||||
|  | 
 | ||||
|  | const currentPath = path.normalize(process.cwd()); | ||||
|  | let cmdValue = ''; | ||||
|  | 
 | ||||
|  | // Check for requirements such as OS version and nginx install.
 | ||||
|  | // #Roadmap: Add ability to satisfy any possible requirements.
 | ||||
|  | 
 | ||||
|  | requirements(); | ||||
|  | // Comment in development and uncomment this line in production.
 | ||||
|  | 
 | ||||
|  | program | ||||
|  | 	.version(up.version()) | ||||
|  | 	.arguments('<cmd>') | ||||
|  | 	.action((cmd) => cmdValue = cmd); | ||||
|  | 
 | ||||
|  | const tryCatch = ((test, name) => { | ||||
|  | 	try { | ||||
|  | 		const msg = test(); | ||||
|  | 		if(msg) console.log(msg); | ||||
|  | 	} catch (err) { | ||||
|  | 		err.message = EOL + `[${name}]: ` + err.message; | ||||
|  | 		console.log (err.message); | ||||
|  | 		process.exit(1); | ||||
|  | 	} | ||||
|  | }); | ||||
|  | 
 | ||||
|  | program | ||||
|  | 	.command('serve <domain> [outPort]') | ||||
|  | 	.description('Create a server at this folder.') | ||||
|  | 	.action((domain, outPort) => | ||||
|  | 		tryCatch( | ||||
|  | 			() => up.server(domain, currentPath, outPort), | ||||
|  | 			'new-server' | ||||
|  | 		)); | ||||
|  | 		 | ||||
|  | program | ||||
|  | 	.command('static <domain> [outPort]') | ||||
|  | 	.description('DEPRECATED! Create a static server at this folder.') | ||||
|  | 	.action((domain, outPort) => | ||||
|  | 		tryCatch( | ||||
|  | 			() => up.server(domain, currentPath, outPort), | ||||
|  | 			'new-server' | ||||
|  | 		)); | ||||
|  | 
 | ||||
|  | program | ||||
|  | 	.command('proxy <domain> <inPort> [outPort]') | ||||
|  | 	.description('Create a proxy server, listening at port number.') | ||||
|  | 	.action((domain, inPort, outPort) => | ||||
|  | 		tryCatch( | ||||
|  | 			() => up.proxy(domain, inPort, outPort), | ||||
|  | 			'new-proxy' | ||||
|  | 		)); | ||||
|  | 
 | ||||
|  | program | ||||
|  | 	.command('list') | ||||
|  | 	.description('List all available servers.') | ||||
|  | 	.action(() => | ||||
|  | 		tryCatch( | ||||
|  | 			() => up.list(), | ||||
|  | 			'list' | ||||
|  | 		)); | ||||
|  | 
 | ||||
|  | program | ||||
|  | 	.command('kill <domain> [ourPort]') | ||||
|  | 	.description('Kill a server.') | ||||
|  | 	.action((domain, outPort) => | ||||
|  | 		tryCatch ( | ||||
|  | 			() => up.kill(domain, outPort), | ||||
|  | 			'kill-server' | ||||
|  | 		)); | ||||
|  | 
 | ||||
|  | program | ||||
|  | 	.command('kill-all') | ||||
|  | 	.description('Warning! Will completely kill all servers and reset nginx') | ||||
|  | 	.action(() => killAllConfirm()); | ||||
|  | 
 | ||||
|  | 
 | ||||
|  | program | ||||
|  | 	.command('*') // This should pick invalid commands, but it doesn't, yet.
 | ||||
|  | 	.action(() => { | ||||
|  | 		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', () => { | ||||
|  | 	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); | ||||
|  | } | ||||
| @ -1,136 +0,0 @@ | |||||
| #!/usr/bin/env node
 |  | ||||
| 
 |  | ||||
| 'use strict'; |  | ||||
| 
 |  | ||||
| const { EOL } = require('os'); |  | ||||
| 
 |  | ||||
| // Requiring npm modules
 |  | ||||
| const program = require('commander'); |  | ||||
| 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'); |  | ||||
| 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`
 |  | ||||
| 
 |  | ||||
| program |  | ||||
| 	.version('0.2.1'); |  | ||||
| 	 |  | ||||
| let cmdValue; |  | ||||
| 
 |  | ||||
| program |  | ||||
| 	.version('0.1.0') |  | ||||
| 	.arguments('<cmd>') |  | ||||
| 	.action(function (cmd) { |  | ||||
| 		cmdValue = cmd; |  | ||||
| 	}); |  | ||||
| 
 |  | ||||
| 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.
 |  | ||||
| 		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)); |  | ||||
| 	}); |  | ||||
| 
 |  | ||||
| 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.
 |  | ||||
| 		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)); |  | ||||
| 	}); |  | ||||
| 
 |  | ||||
| program |  | ||||
| 	.command('list') |  | ||||
| 	.description('List all available servers.') |  | ||||
| 	.action(function () { |  | ||||
| 		listServers(); |  | ||||
| 	}); |  | ||||
| 
 |  | ||||
| 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.
 |  | ||||
| 		killServer(domain, outPort); |  | ||||
| 		console.log(EOL + "Done! Your server has been killed!"+ EOL); |  | ||||
| 	}); |  | ||||
| 
 |  | ||||
| program |  | ||||
| 	.command('kill-all') |  | ||||
| 	.description('Warning! Will completely kill all servers and reset nginx') |  | ||||
| 	.action(function() { |  | ||||
| 		killAllConfirm(); |  | ||||
| 	}); |  | ||||
| 
 |  | ||||
| 
 |  | ||||
| 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(''); |  | ||||
| 	console.log('  Usage:'); |  | ||||
| 	console.log(''); |  | ||||
| 	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('      Set up a proxy server listening at port-number'); |  | ||||
| 	console.log(''); |  | ||||
| }); |  | ||||
| 
 |  | ||||
| // 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); |  | ||||
| } |  | ||||
| @ -0,0 +1,70 @@ | |||||
|  | 'use strict'; | ||||
|  | 
 | ||||
|  | const { EOL } = require('os'); | ||||
|  | 
 | ||||
|  | // Requiring npm modules
 | ||||
|  | const chalk = require('chalk'); | ||||
|  | 
 | ||||
|  | // Requiring Actions
 | ||||
|  | const createNewServer = require('./actions/createNewServer'); | ||||
|  | const createProxyServer = require('./actions/createProxyServer'); | ||||
|  | const killServer = require('./actions/killServer'); | ||||
|  | const listServers = require('./actions/listServers'); | ||||
|  | 
 | ||||
|  | // Requiring utils
 | ||||
|  | const validate = require('./utils/validate'); | ||||
|  | 
 | ||||
|  | const pacversion = 'up-serve v. ' + require('./package.json').version; | ||||
|  | 
 | ||||
|  | function version () { | ||||
|  | 	return pacversion; | ||||
|  | } | ||||
|  | 
 | ||||
|  | function server (domain, path, outPort = "80") { | ||||
|  | 	// If outport is not given, 80 is set as default.
 | ||||
|  | 	outPort = String(outPort); | ||||
|  | 	validate(domain, outPort); | ||||
|  | 	// Validates domain and outport, and if invalid, throws and returns.
 | ||||
|  | 	createNewServer(domain, path, outPort); | ||||
|  | 	if (outPort != "80" || "443") domain = domain + ":" + outPort; | ||||
|  | 	return (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 = "80") { | ||||
|  | 	// Inbound port is necessary, but outbound is set to 80 by default.
 | ||||
|  | 	outPort = String(outPort); | ||||
|  | 	inPort = String(inPort); | ||||
|  | 	// This is a string because regex needs to validate it.
 | ||||
|  | 	validate(domain, inPort, outPort); | ||||
|  | 	createProxyServer(domain, inPort, outPort); | ||||
|  | 	if (outPort != "80" || "443") domain = domain + ":" + outPort; | ||||
|  | 	return (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 () { | ||||
|  | 	return listServers(); | ||||
|  | } | ||||
|  | 
 | ||||
|  | function kill (domain, outPort = "80") { | ||||
|  | 	outPort = String(outPort); | ||||
|  | 	// This is a string because regex needs to validate it.
 | ||||
|  | 	killServer(domain, outPort); | ||||
|  | 	return (EOL + "Done! Your server has been killed!"); | ||||
|  | } | ||||
|  | 
 | ||||
|  | module.exports = { | ||||
|  | 	version, | ||||
|  | 	server, | ||||
|  | 	proxy, | ||||
|  | 	list, | ||||
|  | 	kill | ||||
|  | }; | ||||
					Loading…
					
					
				
		Reference in new issue