Support reading package name from WSK_PACKAGE env var #10
diff --git a/README.md b/README.md index d3b8943..7b79ffa 100644 --- a/README.md +++ b/README.md
@@ -124,7 +124,7 @@ Add the configuration below to your [launch.json](https://code.visualstudio.com/docs/editor/debugging#_launch-configurations). Replace `MYACTION` with the name of your action and `ACTION.js` with the source file containing the action. When you run this, it will start wskdebug and should automatically connect the debugger. -``` +```json "configurations": [ { "type": "node", @@ -145,6 +145,41 @@ For troubleshooting, you can run the debugger in verbose mode by adding `"-v"` to the `args` array. +#### Control wskprops credentials + +To use custom credentials from a custom `.wskprops` and/or use a developer-specific openwhisk package name, but avoid committing that into your source control system, you can use an `.env` file. + +1. Put this `.env` file in your VS Code project root: + + ``` + WSK_CONFIG_FILE=/Users/user/.wskprops-custom + WSK_PACKAGE=my-package + ``` + + Set either of the variables as needed. + +2. Make sure to not commit this file into source control, by e.g. adding `.env` to a `.gitignore` file. + +2. Add an `envFile` setting to the `.vscode/launch.json`, which you can share with co-workers and commit to source control: + + ```json + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "wskdebug", + "runtimeExecutable": "wskdebug", + "args": [ "MYACTION", "ACTION.js", "-l" ], + "localRoot": "${workspaceFolder}", + "remoteRoot": "/code", + "outputCapture": "std", + "envFile": "${workspaceFolder}/.env" + } + ] + ``` + + + <a name="nodejs-multiple-actions"></a> ### Node.js: Multiple actions
diff --git a/examples/nodejs/.env b/examples/nodejs/.env new file mode 100644 index 0000000..d2c1e30 --- /dev/null +++ b/examples/nodejs/.env
@@ -0,0 +1,2 @@ +WSK_PACKAGE=wskdebug-examples +#WSK_CONFIG_FILE=<absolute-path-to-custom-wskprops>
diff --git a/examples/nodejs/.vscode/launch.json b/examples/nodejs/.vscode/launch.json index aeb9f4e..47501f7 100644 --- a/examples/nodejs/.vscode/launch.json +++ b/examples/nodejs/.vscode/launch.json
@@ -49,7 +49,19 @@ "localRoot": "${workspaceFolder}", "remoteRoot": "/code", "outputCapture": "std" + }, + { + "type": "node", + "request": "launch", + "name": "wskdebug with WSK_PACKAGE", + "runtimeExecutable": "wskdebug", + "args": [ + "webaction" + ], + "localRoot": "${workspaceFolder}", + "remoteRoot": "/code", + "outputCapture": "std", + "envFile": "${workspaceFolder}/.env" } - ] } \ No newline at end of file
diff --git a/index.js b/index.js index 96a83cb..0406efe 100644 --- a/index.js +++ b/index.js
@@ -258,6 +258,10 @@ argv.invokeParams = argv.P; argv.invokeAction = argv.a; argv.onChange = argv.r; + + if (process.env.WSK_PACKAGE && argv.action && !argv.action.includes("/")) { + argv.action = `${process.env.WSK_PACKAGE}/${argv.action}`; + } } function printErrorAndExit(err, argv) {
diff --git a/test/wskdebug.test.js b/test/wskdebug.test.js index ca8e941..959168e 100644 --- a/test/wskdebug.test.js +++ b/test/wskdebug.test.js
@@ -22,13 +22,32 @@ // tests basic cli -const wskdebug = require('../index'); +let wskdebug = require('../index'); const test = require('./test'); const assert = require('assert'); const stripAnsi = require('strip-ansi'); const {execSync} = require('child_process'); +const mockRequire = require('mock-require'); + +function mockDebugger() { + const receivedArgv = {}; + + class MockDebugger { + constructor(argv) { + Object.assign(receivedArgv, argv); + } + + start() {} + run() {} + } + + mockRequire("../src/debugger", MockDebugger); + wskdebug = mockRequire.reRequire("../index"); + return receivedArgv; +} + describe('wskdebug cli', function() { it("should print version (via wskdebug.js)", async function() { @@ -66,4 +85,25 @@ assert.equal(stripAnsi(stdio.stdout.trim()), require(`${process.cwd()}/package.json`).version); }); + it("should take action argument", async function() { + const argv = mockDebugger(); + + await wskdebug(`action`); + assert.strictEqual(argv.action, "action"); + + await wskdebug(`package/action`); + assert.strictEqual(argv.action, "package/action"); + }); + + it("should use WSK_PACKAGE env var as package name", async function() { + const argv = mockDebugger(); + + process.env.WSK_PACKAGE = "envPackage"; + await wskdebug(`action`); + assert.strictEqual(argv.action, "envPackage/action"); + + // cli package takes precedence + await wskdebug(`package/action`); + assert.strictEqual(argv.action, "package/action"); + }); });