Browse Source

BREAKING API Change. Functions accept objects instead of list of params.

pull/14/head
Muthu Kumar 7 years ago
parent
commit
e7341d5674
  1. 4
      .eslintrc.json
  2. 1
      .gitignore
  3. 10
      README.md
  4. 3
      actions/listServers.js
  5. 28
      cli.js
  6. 5
      docs/Changelog.md
  7. 9
      lib.js
  8. 20
      package-lock.json
  9. 2
      package.json
  10. 12
      utils/listFile.js
  11. 7
      utils/validate.js

4
.eslintrc.json

@ -18,7 +18,9 @@
"error", "error",
"always" "always"
], ],
"prefer-const": "error", "prefer-const": ["error", {
"destructuring": "all"
}],
"prefer-destructuring": "error", "prefer-destructuring": "error",
"no-var": "error", "no-var": "error",
"strict": "error", "strict": "error",

1
.gitignore

@ -1,3 +1,4 @@
node_modules node_modules
test test
test.txt test.txt
binaries

10
README.md

@ -10,7 +10,7 @@
![A quick demo](assets/demo.gif) ![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)\ > 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.
@ -42,7 +42,7 @@ Format: `up command <required> [optional]`
## Examples ## 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 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 kill example.com` will kill the server named example.com.
- `up list` will fetch a list of servers created with `up`. - `up list` will fetch a list of servers created with `up`.
@ -52,12 +52,12 @@ Format: `up command <required> [optional]`
```JavaScript ```JavaScript
const up = require('up-serve') 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 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 console.log(result) // Will log success or throw if error
``` ```

3
actions/listServers.js

@ -8,8 +8,7 @@ const { EOL } = require('os');
function listServers() { function listServers() {
const serversList = readServers(); const serversList = readServers();
if(serversList) return(EOL + prettyjson.render(serversList)); if(serversList) return(EOL + prettyjson.render(serversList));
else return(EOL + else throw new Error("No servers were found! Create some using `up`!");
"No servers were found! Create some using `up`!");
} }
module.exports = listServers; module.exports = listServers;

28
cli.js

@ -22,7 +22,7 @@ let cmdValue = '';
// Check for requirements such as OS version and nginx install. // Check for requirements such as OS version and nginx install.
// #Roadmap: Add ability to satisfy any possible requirements. // #Roadmap: Add ability to satisfy any possible requirements.
requirements(); //requirements();
// Comment in development and uncomment this line in production. // Comment in development and uncomment this line in production.
program program
@ -46,16 +46,25 @@ program
.description('Create a server at this folder.') .description('Create a server at this folder.')
.action((domain, outPort) => .action((domain, outPort) =>
tryCatch( tryCatch(
() => up.server(domain, currentPath, outPort), () => up.server({
domain: domain,
path: currentPath,
outPort: outPort
}),
'new-server' 'new-server'
)); ));
program program
.command('static <domain> [outPort]') .command('static <domain> [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) => .action((domain, outPort) =>
tryCatch( tryCatch(
() => up.server(domain, currentPath, outPort), () => up.server({
domain: domain,
path: currentPath,
outPort: outPort
}),
'new-server' 'new-server'
)); ));
@ -64,7 +73,11 @@ program
.description('Create a proxy server, listening at port number.') .description('Create a proxy server, listening at port number.')
.action((domain, inPort, outPort) => .action((domain, inPort, outPort) =>
tryCatch( tryCatch(
() => up.proxy(domain, inPort, outPort), () => up.proxy({
domain: domain,
inPort: inPort,
outPort: outPort
}),
'new-proxy' 'new-proxy'
)); ));
@ -82,7 +95,10 @@ program
.description('Kill a server.') .description('Kill a server.')
.action((domain, outPort) => .action((domain, outPort) =>
tryCatch ( tryCatch (
() => up.kill(domain, outPort), () => up.kill({
domain: domain,
outPort: outPort
}),
'kill-server' 'kill-server'
)); ));

5
docs/Changelog.md

@ -1,5 +1,9 @@
# Changelog / Version history # 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` v. 0.2.5
- `up static` is DEPRECATED. Use `up serve` instead. - `up static` is DEPRECATED. Use `up serve` instead.
@ -13,6 +17,7 @@
## `up` v. 0.2.0 ## `up` v. 0.2.0
- Added CHANGELOG from here.
- Under the hood BREAKING changes. Working directories change. - Under the hood BREAKING changes. Working directories change.
- `/var/www/` to `/etc/up-serve/static/` - `/var/www/` to `/etc/up-serve/static/`
- `/etc/nginx/sites-available/` to `/etc/nginx/conf.d` - `/etc/nginx/sites-available/` to `/etc/nginx/conf.d`

9
lib.js

@ -20,7 +20,8 @@ function version () {
return pacversion; 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. // If outport is not given, 80 is set as default.
outPort = String(outPort); outPort = String(outPort);
validate(domain, outPort); validate(domain, outPort);
@ -35,7 +36,8 @@ function server (domain, path, outPort = "80") {
].join(EOL)); ].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. // Inbound port is necessary, but outbound is set to 80 by default.
outPort = String(outPort); outPort = String(outPort);
inPort = String(inPort); inPort = String(inPort);
@ -54,7 +56,8 @@ function list () {
return listServers(); return listServers();
} }
function kill (domain, outPort = "80") { function kill (options) {
let { domain, outPort = "80" } = options;
outPort = String(outPort); outPort = String(outPort);
// This is a string because regex needs to validate it. // This is a string because regex needs to validate it.
killServer(domain, outPort); killServer(domain, outPort);

20
package-lock.json

@ -1,6 +1,6 @@
{ {
"name": "up-serve", "name": "up-serve",
"version": "0.2.1", "version": "0.2.5",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
@ -1063,15 +1063,6 @@
"integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=", "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=",
"dev": true "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": { "string-width": {
"version": "2.1.1", "version": "2.1.1",
"resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz", "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz",
@ -1082,6 +1073,15 @@
"strip-ansi": "4.0.0" "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": { "strip-ansi": {
"version": "4.0.0", "version": "4.0.0",
"resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-4.0.0.tgz", "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-4.0.0.tgz",

2
package.json

@ -1,6 +1,6 @@
{ {
"name": "up-serve", "name": "up-serve",
"version": "0.2.5", "version": "0.3.0",
"description": "A cli tool to quickly create and manage nginx server blocks.", "description": "A cli tool to quickly create and manage nginx server blocks.",
"main": "lib.js", "main": "lib.js",
"scripts": { "scripts": {

12
utils/listFile.js

@ -9,23 +9,23 @@ function appendToList(domain, outPort, inPort) {
inPort = inPort || undefined; inPort = inPort || undefined;
let jsonFile = { "domains": [] }; let jsonFile = { "domains": [] };
const domBlock = { const domainBlock = {
"domain": domain, "domain": domain,
"outPort": outPort "outPort": outPort
}; };
if (!inPort) { if (!inPort) {
domBlock.type = "static/server"; domainBlock.type = "static/server";
} else { } else {
domBlock.type = "proxy server"; domainBlock.type = "proxy server";
domBlock.inPort = inPort; domainBlock.inPort = inPort;
} }
if (fs.existsSync(listFilePath())) { if (fs.existsSync(listFilePath())) {
const jsonBuffer = JSON.parse(fs.readFileSync(listFilePath())); const jsonBuffer = JSON.parse(fs.readFileSync(listFilePath()));
jsonFile.domains = removeFromArray(jsonBuffer.domains, domain, outPort); jsonFile.domains = removeFromArray(jsonBuffer.domains, domain, outPort);
} }
jsonFile.domains.push(domBlock); jsonFile.domains.push(domainBlock);
jsonFile = JSON.stringify(jsonFile, null, '\t'); jsonFile = JSON.stringify(jsonFile, null, '\t');
fs.writeFileSync(listFilePath(), jsonFile); fs.writeFileSync(listFilePath(), jsonFile);
} }
@ -59,7 +59,7 @@ function readServers () {
} }
} }
else { else {
return "No servers were created using `up` yet."; throw new Error("No servers were created using `up` yet.");
} }
return serversList; return serversList;
} }

7
utils/validate.js

@ -3,13 +3,10 @@
const parseToInt = require('./parseToInt'); const parseToInt = require('./parseToInt');
const isIP = require('./isIP'); const isIP = require('./isIP');
// Using Validator // Validate fully qualified domain names
const isDomain = require('./isFQDN'); const isDomain = require('./isFQDN');
function validate(domain, inPort, outPort) { function validate(domain, inPort = undefined, outPort = 80) {
//
inPort = inPort || undefined;
outPort = outPort || 80;
// Error messages // Error messages
const domainInvalidMsg = [ const domainInvalidMsg = [

Loading…
Cancel
Save