A local test script for node.js functions (#26)
* A local test script for node.js functions
As [suggested by Carlos](https://twitter.com/csantanapr/status/861362098877739009) and [initially provided by Raymond](https://www.raymondcamden.com/2017/01/09/quick-tip-for-testing-openwhisk-actions-locally).
* We don’t need zlib
diff --git a/node-local/README.md b/node-local/README.md
new file mode 100644
index 0000000..2de3234
--- /dev/null
+++ b/node-local/README.md
@@ -0,0 +1,24 @@
+# Testing node.js functions locally
+
+Usage:
+
+```bash
+node test.js ./path-to-function.js parameter=value parameter2=value2
+```
+
+Will invoke the `main` function of `path-to-function.js` with following `params`:
+```javascript
+{
+ "parameter":"value",
+ "parameter2":"value"
+}
+```
+
+Alternatively, input can be passed on stdin, this allows the creation of more complex input
+objects that would be inconvenient to edit on the command line or passing non-string values.
+
+```bash
+echo '{"boolean": true}' | node test.js ./path-to-function.js
+cat input.json | node test.js ./path-to-function.js
+```
+
diff --git a/node-local/test.js b/node-local/test.js
new file mode 100644
index 0000000..16041f1
--- /dev/null
+++ b/node-local/test.js
@@ -0,0 +1,59 @@
+process.stdin.setEncoding('utf8');
+
+if (process.argv[2]=="--help"||process.argv[2]=="-h") {
+ help();
+} else if (!process.stdin.isTTY) {
+ var input = "";
+
+ process.stdin.on('readable', () => {
+ var chunk = process.stdin.read();
+ if (chunk !== null) {
+ input += chunk;
+ }
+ });
+
+ process.stdin.on('end', () => {
+ const params = JSON.parse(input);
+
+ run(params);
+ });
+
+} else {
+ let params = {};
+ for(var i=3;i<process.argv.length;i++) {
+ let [name,value] = process.argv[i].split('=');
+ params[name] = value;
+ }
+ run(params);
+}
+
+function help() {
+ console.log("");
+ console.log("Usage:");
+ console.log(" node test.js ./main.js foo=bar");
+ console.log(" echo '{\"foo\":\"bar\"}' | node test.js ./main.js");
+}
+
+function run(params) {
+ const actionToRun = process.argv[2];
+
+ if (!actionToRun) {
+ console.error("./test.js: Missing argument <action-to-run>");
+ help();
+ process.exit(1);
+ }
+
+ const imports = require(actionToRun);
+ //support a non-exported main function as a fallback
+ const action = imports.main ? imports.main : main;
+
+ let result = action(params);
+
+ if (result.then) {
+ Promise.resolve(result)
+ .then(result => console.log(result.toString("utf-8")))
+ .catch(error => console.error(error));
+ } else {
+ console.log(result);
+ }
+}
\ No newline at end of file