Skip to content
This repository was archived by the owner on Dec 17, 2024. It is now read-only.

Commit 7664e10

Browse files
committed
support for env var referencing from command line
Fixes #16
1 parent bb1a633 commit 7664e10

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

app/content/js/repl.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,31 @@ self.pexec = (command, execOptions) => self.exec(command, Object.assign({ echo:
453453

454454
const patterns = {
455455
commentLine: /\s*#.*$/,
456+
dash: /-([^\s]*)/,
456457
whitespace: /\s/
457458
}
459+
460+
/**
461+
* Escape the given value so that it is compatible with command line execution
462+
*
463+
*/
464+
const escape = str => str.replace(patterns.dash, "'-$1'")
465+
466+
/**
467+
* Resolve the given string as a possible reference to an environment
468+
* variable; e.g. $FOO should resolve to 3 if process.env.FOO has
469+
* value 3.
470+
*
471+
*/
472+
const resolveEnvVar = variable => {
473+
const envValue = process.env[variable.substring(1)]
474+
return envValue ? escape(envValue) : variable
475+
}
476+
477+
/**
478+
* Split the given string into an argv
479+
*
480+
*/
458481
const split = (str, removeOuterQuotes=true) => {
459482
const A = [],
460483
stack = []
@@ -466,7 +489,7 @@ const split = (str, removeOuterQuotes=true) => {
466489

467490
if (stack.length === 0 && char.match(patterns.whitespace)) {
468491
if (cur.length > 0) {
469-
A.push(cur)
492+
A.push(resolveEnvVar(cur))
470493
cur = ''
471494
}
472495
continue
@@ -507,7 +530,7 @@ const split = (str, removeOuterQuotes=true) => {
507530
}
508531

509532
if (cur.length > 0) {
510-
A.push(cur)
533+
A.push(resolveEnvVar(cur))
511534
}
512535

513536
return A

tests/data/echo.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const main = x=>x

tests/tests/passes/02/headless.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ const path = require('path'),
2323
fsh = path.join(__dirname, '../../../../app/bin/fsh')
2424

2525
const cli = {
26-
do: cmd => new Promise((resolve, reject) => {
26+
do: (cmd, env={}) => new Promise((resolve, reject) => {
2727
const command = `${fsh} ${cmd} --no-color`
2828

29-
exec(command, (err, stdout, stderr) => {
29+
exec(command, { env: Object.assign({}, process.env, env) }, (err, stdout, stderr) => {
3030
if (err) {
3131
resolve({ code: err.code, output: stdout.trim().concat(stderr) })
3232
} else {
@@ -125,4 +125,8 @@ describe('Headless mode', function() {
125125
it('should create an app', () => cli.do('app create seq ./data/fsm.json')
126126
.then(cli.expectOK('ok: updated app seq\n', { exact: true }))
127127
.catch(common.oops(this)))
128+
129+
it('should create an action with an env var parameter', () => cli.do('action create envfun ./data/echo.js -p fun $FUN', { FUN: 3 })
130+
.then(cli.expectOK('ok: updated action envfun\n', { exact: true }))
131+
.catch(common.oops(this)))
128132
})

0 commit comments

Comments
 (0)