diff --git a/.eslintrc.json b/.eslintrc.json index d9aca22..0af9758 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -23,6 +23,7 @@ "no-var": "error", "strict": "error", "eol-last": "error", - "max-len": "error" + "max-len": "error", + "no-unused-expressions": "error" } } \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 92f021c..a877113 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,5 @@ -{ - "editor.insertSpaces": false, - "editor.tabSize": 4, - "files.eol": "\n" +{ + "editor.insertSpaces": false, + "editor.tabSize": 4, + "files.eol": "\n" } \ No newline at end of file diff --git a/README.md b/README.md index 17df013..6339d30 100644 --- a/README.md +++ b/README.md @@ -8,10 +8,10 @@ # up -> Current version: `up v.0.2.1 (Alpha)` +> Current version: `up v.0.2.5 (Alpha)` > Notes: `up` is now in Alpha! 🎉 [(Changelog)](/docs/Changelog.md)\ -> ⚠️ ❌ `up` is pretty useable so far. If you're testing `up` on a development server, do give us feedback. +> ⚠️ `up` is pretty useable so far. If you're testing `up` on a development server, do give us feedback. **`up`** is a command line application that creates nginx server blocks quickly with a single command. @@ -32,7 +32,8 @@ Install `up` from npm: Format: `up command [optional]` -- `up static [outbound port]` - Create new static server at current folder. +- `up serve [outbound port]` - Create new static server at current folder. + - `up static` is deprecated from `v. 0.2.5` (see [changelog](/docs/CHANGELOG.md)) - `up proxy [outbound port]` - Create new proxy server listening at said port. - `up list` - List currently available servers. - `up kill ` - Kill the server for this domain. @@ -41,10 +42,29 @@ Format: `up command [optional]` - `up static example.com` will serve a static website from current folder. - `up proxy example.com 8081` will create a reverse proxy listening at port 8081. -- `up kill example.com` +- `up kill example.com` will kill the server named example.com. +- `up list` will fetch a list of servers created with `up`. + +## API + +```JavaScript +const up = require('up-serve') + +console.log(up.version()) // up v. 0.2.5 + +let result = up.server("example.com", "path/to/project", "80") +console.log(result) // Will log success or throw if error + +let result = up.kill("example.com", "80") +console.log(result) // Will log success or throw if error +``` + +More detailed API documentation coming soon. --- +

Meta

+

Roadmap

Contribution Guidelines

diff --git a/actions/createStaticServer.js b/actions/createNewServer.js similarity index 68% rename from actions/createStaticServer.js rename to actions/createNewServer.js index 555cca6..602990b 100644 --- a/actions/createStaticServer.js +++ b/actions/createNewServer.js @@ -2,21 +2,21 @@ const fs = require('fs-extra'); const shell = require('shelljs'); -const path = require('path'); const npath = require('../utils/nginxPath'); const conf = require('../utils/nginxConf'); const nginxReload = require('../utils/nginxReload'); const { appendToList } = require('../utils/listFile'); -const currentPath = path.normalize(process.cwd()); const { EOL } = require('os'); // \n if used on Linux, \r\n if used on Windows. -function createStaticServer(domain, outPort) { +function createStaticServer(domain, path, outPort) { outPort = outPort || 80; - shell.mkdir('-p', npath.confD()); + + shell.mkdir('-p', npath.enabledSites()); + // Creates directories if doesn't exist - fs.outputFileSync((conf(npath.confD(), domain, outPort)), + fs.outputFileSync((conf(npath.enabledSites(), domain, outPort)), // Gets nginx's paths from nginxPath.js "server {" + EOL + " listen " + outPort + ";" + EOL + @@ -30,18 +30,12 @@ function createStaticServer(domain, outPort) { " }" + EOL + "}" ); - shell.mkdir('-p', npath.enabledSites()); - // Creates directories if doesn't exist - shell.rm('-rf', conf(npath.enabledSites(), domain, outPort)); - // Removes domain from sites-enabled if exists - shell.ln('-sf', conf(npath.confD(), domain, outPort), - conf(npath.enabledSites(), domain, outPort)); - // Symlink the conf file from confD 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)); + shell.ln('-sf', path, npath.webRootDomain(domain, outPort)); // Symlink current directory to nginx's web root appendToList(domain, outPort); diff --git a/actions/createProxyServer.js b/actions/createProxyServer.js index 0b0a967..6ad3cd6 100644 --- a/actions/createProxyServer.js +++ b/actions/createProxyServer.js @@ -12,9 +12,11 @@ const { EOL } = require('os'); // \n if used on Linux, \r\n if used on Windows. function createProxyServer(domain, inPort, outPort) { outPort = outPort || 80; - shell.mkdir('-p', npath.confD()); + + shell.mkdir('-p', npath.enabledSites()); + // Creates directories if doesn't exist - fs.outputFileSync((conf(npath.confD(), domain, outPort)), + fs.outputFileSync((conf(npath.enabledSites(), domain, outPort)), "server {" + EOL + " listen " + outPort + ";" + EOL + " listen [::]:" + outPort + ";" + EOL + @@ -34,9 +36,6 @@ function createProxyServer(domain, inPort, outPort) { shell.mkdir('-p', npath.enabledSites()); // Creates directories if doesn't exist - shell.ln('-sf', conf(npath.confD(), domain, outPort), - conf(npath.enabledSites(), domain, outPort)); - // Symlink the conf file from sites-available to sites-enabled appendToList(domain, outPort, inPort); nginxReload(); diff --git a/actions/killALL.js b/actions/killALL.js index 012806b..dfe44ba 100644 --- a/actions/killALL.js +++ b/actions/killALL.js @@ -13,26 +13,23 @@ function killALL () { shell.rm('-Rf', npath.serversBakUp); shell.mv(npath.serversUp(), npath.serversBakUp()); shell.rm('-Rf', npath() + "sites-available"); - shell.rm('-Rf', npath.confD()); shell.rm('-Rf', npath.enabledSites()); shell.rm('-Rf', npath.webRoot()); - shell.mkdir('-p', npath.confD()); shell.mkdir('-p', npath.enabledSites()); shell.mkdir('-p', npath.webRoot()); shell.cp((path.join(__dirname, '/../build/defaultNginx.conf')), conf(npath.enabledSites())); // Create the default.conf file - console.log("All servers were killed and reverted to default."); - console.log(EOL + [ - "A backup of your old servers.up is " + - "saved in /etc/up-serve/servers.bak.up.", - "Check this if you need to." - ].join(EOL) + EOL); nginxReload(); + console.log(EOL + "All servers were killed and reverted to default." + + EOL + "A backup of your old servers.up is " + + "saved in /etc/up-serve/servers.bak.up." + + "Check this if you need to." + EOL + ); } function noKill () { - console.log(EOL + "kill-all was interrupted by user."); + return(EOL + "kill-all was interrupted by user."); } module.exports.kill = killALL; diff --git a/actions/listServers.js b/actions/listServers.js index b9fbc9d..ef24c4a 100644 --- a/actions/listServers.js +++ b/actions/listServers.js @@ -7,10 +7,9 @@ const { EOL } = require('os'); function listServers() { const serversList = readServers(); - if(serversList) console.log(EOL + prettyjson.render(serversList) + EOL); - else console.log(EOL + - "No servers were found! Create some using `up`!" + - EOL); + if(serversList) return(EOL + prettyjson.render(serversList)); + else return(EOL + + "No servers were found! Create some using `up`!"); } module.exports = listServers; diff --git a/assets/tlds.txt b/assets/tlds.txt index 2df92a0..cab12b8 100644 --- a/assets/tlds.txt +++ b/assets/tlds.txt @@ -1,4 +1,4 @@ -# Version 2017111900, Last Updated Sun Nov 19 07:07:01 2017 UTC +# Version 2017112900, Last Updated Wed Nov 29 07:07:01 2017 UTC AAA AARP ABARTH diff --git a/cli.js b/cli.js new file mode 100644 index 0000000..cf45a03 --- /dev/null +++ b/cli.js @@ -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('') + .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 [outPort]') + .description('Create a server at this folder.') + .action((domain, outPort) => + tryCatch( + () => up.server(domain, currentPath, outPort), + 'new-server' + )); + +program + .command('static [outPort]') + .description('DEPRECATED! Create a static server at this folder.') + .action((domain, outPort) => + tryCatch( + () => up.server(domain, currentPath, outPort), + 'new-server' + )); + +program + .command('proxy [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 [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] ') + + 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); +} diff --git a/docs/Changelog.md b/docs/Changelog.md index b6529cf..d38c01f 100644 --- a/docs/Changelog.md +++ b/docs/Changelog.md @@ -1,15 +1,22 @@ -# Changelog / Version history - -## `up` v. 0.2.1 - -- Bug fix and patch for `up kill-all` breaking unexpectedly due to undefined default config file. - -## `up` v. 0.2.0 - -- Under the hood BREAKING changes. Working directories change. - - `/var/www/` to `/etc/up-serve/static/` - - `/etc/nginx/sites-available/` to `/etc/nginx/conf.d` -- `up static|proxy ` adds the server to `/etc/up-serve/servers.up` list. -- `up kill ` removes server from `servers.up` list. -- `up list` lists available servers from /etc/up-serve/servers.up! -- `up kill-all` destroys all servers and places a `default.conf` in `/etc/nginx/sites-enabled`. +# Changelog / Version history + +## `up` v. 0.2.5 + +- `up static` is DEPRECATED. Use `up serve` instead. +- Major refactor of code, and splitting of `index.js` to `lib.js` and `cli.js`. +- Basic API now available. +- Usage of `conf.d` is DEPRECATED. `up-serve` now only uses `sites-enabled`. Since everything is abstracted at a higher level, the need for maintaining two directories (Read sites-available here) is non-existent. + +## `up` v. 0.2.1 + +- Bug fix and patch for `up kill-all` breaking unexpectedly due to undefined default config file. + +## `up` v. 0.2.0 + +- Under the hood BREAKING changes. Working directories change. + - `/var/www/` to `/etc/up-serve/static/` + - `/etc/nginx/sites-available/` to `/etc/nginx/conf.d` +- `up static|proxy ` adds the server to `/etc/up-serve/servers.up` list. +- `up kill ` removes server from `servers.up` list. +- `up list` lists available servers from /etc/up-serve/servers.up! +- `up kill-all` destroys all servers and places a `default.conf` in `/etc/nginx/sites-enabled`. diff --git a/index.js b/index.js deleted file mode 100755 index bc388d4..0000000 --- a/index.js +++ /dev/null @@ -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('') - .action(function (cmd) { - cmdValue = cmd; - }); - -program - .command('static [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 [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 [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); -} diff --git a/lib.js b/lib.js new file mode 100644 index 0000000..1310ed6 --- /dev/null +++ b/lib.js @@ -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 +}; diff --git a/package-lock.json b/package-lock.json index a73b731..f45ddbb 100644 --- a/package-lock.json +++ b/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", diff --git a/package.json b/package.json index 981ac76..4403865 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "up-serve", - "version": "0.2.1", + "version": "0.2.5", "description": "A cli tool to quickly create and manage nginx server blocks.", - "main": "index.js", + "main": "lib.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "build": "node ./build/fetchTLDS" @@ -19,7 +19,10 @@ "block" ], "bin": { - "up": "./index.js" + "up": "./cli.js" + }, + "pkg": { + "assets": "assets/*" }, "author": "Muthu Kumar (@MKRhere)", "license": "MIT", @@ -28,11 +31,10 @@ }, "homepage": "https://github.com/codefeathers/up-serve#readme", "dependencies": { - "chalk": "^2.3.0", - "commander": "^2.11.0", + "chalk": "2.3.0", + "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" }, diff --git a/utils/listFile.js b/utils/listFile.js index b16c973..9e34889 100644 --- a/utils/listFile.js +++ b/utils/listFile.js @@ -1,7 +1,5 @@ 'use strict'; -const { EOL } = require('os'); - const fs = require('fs-extra'); const removeFromArray = require('./removeFromArray'); @@ -17,9 +15,9 @@ function appendToList(domain, outPort, inPort) { }; if (!inPort) { - domBlock.type = "static"; + domBlock.type = "static/server"; } else { - domBlock.type = "proxy"; + domBlock.type = "proxy server"; domBlock.inPort = inPort; } @@ -35,23 +33,33 @@ function appendToList(domain, outPort, inPort) { function removeFromList (domain, outPort) { if (fs.existsSync(listFilePath())) { let jsonFile = { "domains": [] }; - const jsonBuffer = JSON.parse(fs.readFileSync(listFilePath())); - jsonFile.domains = removeFromArray(jsonBuffer.domains, domain, outPort); - - jsonFile = JSON.stringify(jsonBuffer, null, '\t'); - fs.writeFileSync(listFilePath(), jsonFile); + const jsonContent = fs.readFileSync(listFilePath(), "utf-8"); + const jsonBuffer = JSON.parse(jsonContent); + for (let i = 0; i < (jsonBuffer.domains).length; i ++) { + if(jsonBuffer.domains[i].domain == domain){ + jsonFile.domains = + removeFromArray(jsonBuffer.domains, domain, outPort); + + jsonFile = JSON.stringify(jsonFile, null, '\t'); + fs.writeFileSync(listFilePath(), jsonFile); + return; + } + } + throw new Error("This domain does not exist in servers.up"); } - else console.log(EOL + "No servers were created using `up` yet." + EOL); + else throw new Error("No servers were created using `up` yet."); } function readServers () { let serversList; if (fs.existsSync(listFilePath())) { serversList = JSON.parse(fs.readFileSync(listFilePath())); - if(!serversList.domains[0]) return undefined; + if(!serversList.domains[0]) { + throw new Error("No domains exist in servers.up"); + } } else { - return "Servers were not created"; + return "No servers were created using `up` yet."; } return serversList; } diff --git a/utils/nginxReload.js b/utils/nginxReload.js index 55db66b..6b6f2f1 100644 --- a/utils/nginxReload.js +++ b/utils/nginxReload.js @@ -3,14 +3,17 @@ const { execSync } = require('child_process'); 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); - process.exit(1); - } - }); + try { + execSync('service nginx reload', { + stdio: 'ignore' + }); + execSync('service nginx start', { + stdio: 'ignore' + }); + } catch (err) { + throw new Error('nginx failed to load'); + } + return; } module.exports = nginxReload; diff --git a/utils/requirements.js b/utils/requirements.js index 26b2cdb..2c17684 100644 --- a/utils/requirements.js +++ b/utils/requirements.js @@ -25,7 +25,8 @@ function requirements() { // Check if sudo if (process.getuid() != 0) { - console.log("`up` requires root privileges to work. Please use `sudo up `"); + shell.echo("`up` requires root privileges to work." + + "Please use `sudo up `"); shell.exit(1); } diff --git a/utils/validate.js b/utils/validate.js index e1a9d2b..eb73570 100644 --- a/utils/validate.js +++ b/utils/validate.js @@ -1,7 +1,5 @@ 'use strict'; -const { EOL } = require('os'); - const parseToInt = require('./parseToInt'); const isIP = require('./isIP'); @@ -15,12 +13,12 @@ function validate(domain, inPort, outPort) { // Error messages const domainInvalidMsg = [ - EOL + "Please use a domain name instead of an IP address.", - EOL + "Domain is not valid. Please use a valid domain name." + "Please use a domain name instead of an IP address.", + "Domain is not valid. Please use a valid domain name." ]; const portInvalidMsg = [ - EOL + "Port should be a number.", - EOL + "Port should be a number from 1 and 65535." + "Port should be a number.", + "Port should be a number from 1 and 65535." ]; // ARGV returns a string as input. @@ -30,35 +28,25 @@ function validate(domain, inPort, outPort) { const validInPort = parseToInt(inPort); const validOutPort = parseToInt(outPort); - // The value of isInvalid will be returned back. - // If none of the `if`s are true, the default - // value `true` is returned `domain`, `inPort` and `outPort` are considered - // validated. - let isValid = true; - // Throw if IP is given instead of domain name. if (isIP(domain)) { - console.log(domainInvalidMsg[0]); - return isValid = false; + throw new Error(domainInvalidMsg[0]); } // Throw if input is not a Fully Qualified Domain Name (FQDN) if (!isDomain(domain)) { - console.log(domainInvalidMsg[1]); - return isValid = false; + throw new Error(domainInvalidMsg[1]); } // Enter if `inPort` is not defined. // This happens for `up static` where no inbound ports are required. if (typeof inPort == undefined) { if (!validOutPort) { - console.log(portInvalidMsg[0]); // `outPort` is not an integer. - return isValid = false; + throw new Error(portInvalidMsg[0]);// `outPort` is not an integer. } if (!(validOutPort > 0 && validOutPort <= 65535)) { - console.log(portInvalidMsg[1]); + throw new Error(portInvalidMsg[1]); // `outPort` is not within port range. - return isValid = false; } } @@ -66,23 +54,18 @@ function validate(domain, inPort, outPort) { // inbound port is required. if (typeof inPort !== undefined) { if (!validInPort || !validOutPort) { - console.log(portInvalidMsg[0]); + throw new Error(portInvalidMsg[0]); // Either `inPort` or `outPort` is not an integer. - return isValid = false; } if (typeof outPort !== undefined) { if (!( (validInPort > 0 && validInPort <= 65535) && (validOutPort > 0 && validOutPort <= 65535) )) { - console.log(portInvalidMsg[1]); + throw new Error(portInvalidMsg[1]); // Either `inPort` or `outPort` are not within port range. - return isValid = false; } } - return isValid; - // If any of the `if`s were true, `isInvalid = false`. - // If not, `isInvalid = true`. } }