From e7341d5674497b7bf266562fd7f7ad6780ae6ce1 Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Sun, 7 Jan 2018 13:08:19 +0530 Subject: [PATCH] BREAKING API Change. Functions accept objects instead of list of params. --- .eslintrc.json | 4 +++- .gitignore | 3 ++- README.md | 10 +++++----- actions/listServers.js | 3 +-- cli.js | 30 +++++++++++++++++++++++------- docs/Changelog.md | 5 +++++ lib.js | 9 ++++++--- package-lock.json | 20 ++++++++++---------- package.json | 2 +- utils/listFile.js | 12 ++++++------ utils/validate.js | 9 +++------ 11 files changed, 65 insertions(+), 42 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 0af9758..16bdabc 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -18,7 +18,9 @@ "error", "always" ], - "prefer-const": "error", + "prefer-const": ["error", { + "destructuring": "all" + }], "prefer-destructuring": "error", "no-var": "error", "strict": "error", diff --git a/.gitignore b/.gitignore index b407659..66c56f7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules test -test.txt \ No newline at end of file +test.txt +binaries \ No newline at end of file diff --git a/README.md b/README.md index 4b0a5bd..f26883b 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ ![A quick demo](assets/demo.gif) -> Current version: `up v.0.2.5 (Alpha)` +> Current version: `up v.0.3.0 (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. @@ -42,7 +42,7 @@ Format: `up command [optional]` ## Examples -- `up static example.com` will serve a static website from current folder. +- `up serve 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` will kill the server named example.com. - `up list` will fetch a list of servers created with `up`. @@ -52,12 +52,12 @@ Format: `up command [optional]` ```JavaScript const up = require('up-serve') -console.log(up.version()) // up v. 0.2.5 +console.log(up.version()) // up v. 0.3.0 -let result = up.server("example.com", "path/to/project", "80") +let result = up.server({ domain: "example.com", path: "path/to/project", outPort: "80" }) console.log(result) // Will log success or throw if error -let result = up.kill("example.com", "80") +let result = up.kill({ domain: "example.com", outPort: "80" }) console.log(result) // Will log success or throw if error ``` diff --git a/actions/listServers.js b/actions/listServers.js index ef24c4a..a5e217c 100644 --- a/actions/listServers.js +++ b/actions/listServers.js @@ -8,8 +8,7 @@ const { EOL } = require('os'); function listServers() { const serversList = readServers(); if(serversList) return(EOL + prettyjson.render(serversList)); - else return(EOL + - "No servers were found! Create some using `up`!"); + else throw new Error("No servers were found! Create some using `up`!"); } module.exports = listServers; diff --git a/cli.js b/cli.js index cf45a03..d5a826d 100644 --- a/cli.js +++ b/cli.js @@ -22,7 +22,7 @@ let cmdValue = ''; // Check for requirements such as OS version and nginx install. // #Roadmap: Add ability to satisfy any possible requirements. -requirements(); +//requirements(); // Comment in development and uncomment this line in production. program @@ -46,16 +46,25 @@ program .description('Create a server at this folder.') .action((domain, outPort) => tryCatch( - () => up.server(domain, currentPath, outPort), + () => up.server({ + domain: domain, + path: currentPath, + outPort: outPort + }), 'new-server' )); - + program .command('static [outPort]') - .description('DEPRECATED! Create a static server at this folder.') + .description(`DEPRECATED! Use 'up serve' instead! + Create a static server at this folder.`) .action((domain, outPort) => tryCatch( - () => up.server(domain, currentPath, outPort), + () => up.server({ + domain: domain, + path: currentPath, + outPort: outPort + }), 'new-server' )); @@ -64,7 +73,11 @@ program .description('Create a proxy server, listening at port number.') .action((domain, inPort, outPort) => tryCatch( - () => up.proxy(domain, inPort, outPort), + () => up.proxy({ + domain: domain, + inPort: inPort, + outPort: outPort + }), 'new-proxy' )); @@ -82,7 +95,10 @@ program .description('Kill a server.') .action((domain, outPort) => tryCatch ( - () => up.kill(domain, outPort), + () => up.kill({ + domain: domain, + outPort: outPort + }), 'kill-server' )); diff --git a/docs/Changelog.md b/docs/Changelog.md index d38c01f..044588b 100644 --- a/docs/Changelog.md +++ b/docs/Changelog.md @@ -1,5 +1,9 @@ # Changelog / Version history +## `up` v. 0.3.0 + +- BREAKING API changes. Functions will now accept an object instead of a list of arguments. This allows for named parameters that don't have to follow a precise order. Check [README](../README.md) for details. + ## `up` v. 0.2.5 - `up static` is DEPRECATED. Use `up serve` instead. @@ -13,6 +17,7 @@ ## `up` v. 0.2.0 +- Added CHANGELOG from here. - Under the hood BREAKING changes. Working directories change. - `/var/www/` to `/etc/up-serve/static/` - `/etc/nginx/sites-available/` to `/etc/nginx/conf.d` diff --git a/lib.js b/lib.js index 1310ed6..23bc4a8 100644 --- a/lib.js +++ b/lib.js @@ -20,7 +20,8 @@ function version () { return pacversion; } -function server (domain, path, outPort = "80") { +function server (options) { + let { domain, path, outPort = "80" } = options; // If outport is not given, 80 is set as default. outPort = String(outPort); validate(domain, outPort); @@ -35,7 +36,8 @@ function server (domain, path, outPort = "80") { ].join(EOL)); } -function proxy (domain, inPort, outPort = "80") { +function proxy (options) { + let { domain, inPort, outPort = "80" } = options; // Inbound port is necessary, but outbound is set to 80 by default. outPort = String(outPort); inPort = String(inPort); @@ -54,7 +56,8 @@ function list () { return listServers(); } -function kill (domain, outPort = "80") { +function kill (options) { + let { domain, outPort = "80" } = options; outPort = String(outPort); // This is a string because regex needs to validate it. killServer(domain, outPort); diff --git a/package-lock.json b/package-lock.json index f45ddbb..efd712a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "up-serve", - "version": "0.2.1", + "version": "0.2.5", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -1063,15 +1063,6 @@ "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=", "dev": true }, - "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", "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz", @@ -1082,6 +1073,15 @@ "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", diff --git a/package.json b/package.json index 4403865..b07e1ab 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "up-serve", - "version": "0.2.5", + "version": "0.3.0", "description": "A cli tool to quickly create and manage nginx server blocks.", "main": "lib.js", "scripts": { diff --git a/utils/listFile.js b/utils/listFile.js index 9e34889..99faa7b 100644 --- a/utils/listFile.js +++ b/utils/listFile.js @@ -9,23 +9,23 @@ function appendToList(domain, outPort, inPort) { inPort = inPort || undefined; let jsonFile = { "domains": [] }; - const domBlock = { + const domainBlock = { "domain": domain, "outPort": outPort }; if (!inPort) { - domBlock.type = "static/server"; + domainBlock.type = "static/server"; } else { - domBlock.type = "proxy server"; - domBlock.inPort = inPort; + domainBlock.type = "proxy server"; + domainBlock.inPort = inPort; } if (fs.existsSync(listFilePath())) { const jsonBuffer = JSON.parse(fs.readFileSync(listFilePath())); jsonFile.domains = removeFromArray(jsonBuffer.domains, domain, outPort); } - jsonFile.domains.push(domBlock); + jsonFile.domains.push(domainBlock); jsonFile = JSON.stringify(jsonFile, null, '\t'); fs.writeFileSync(listFilePath(), jsonFile); } @@ -59,7 +59,7 @@ function readServers () { } } else { - return "No servers were created using `up` yet."; + throw new Error("No servers were created using `up` yet."); } return serversList; } diff --git a/utils/validate.js b/utils/validate.js index eb73570..373cc9f 100644 --- a/utils/validate.js +++ b/utils/validate.js @@ -3,14 +3,11 @@ const parseToInt = require('./parseToInt'); const isIP = require('./isIP'); -// Using Validator +// Validate fully qualified domain names const isDomain = require('./isFQDN'); -function validate(domain, inPort, outPort) { - // - inPort = inPort || undefined; - outPort = outPort || 80; - +function validate(domain, inPort = undefined, outPort = 80) { + // Error messages const domainInvalidMsg = [ "Please use a domain name instead of an IP address.",