-
-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add support for parameter expansion #342
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.expandEnvs = void 0; | ||
/** | ||
* expandEnvs Replaces $var in args and command with environment variables | ||
* the environment variable doesn't exist, it leaves it as is. | ||
*/ | ||
function expandEnvs(str, envs) { | ||
return str.replace(/(?<!\\)\$[a-zA-Z0-9_]+/g, varName => { | ||
const varValue = envs[varName.slice(1)]; | ||
return varValue === undefined ? varName : varValue; | ||
return str.replace(/(?<!\\)\$\{?([a-zA-Z0-9_]+)\}?/g, (_, varName) => { | ||
const varValue = envs[varName]; | ||
return varValue === undefined ? `$${varName}` : varValue; | ||
}); | ||
} | ||
exports.expandEnvs = expandEnvs; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,18 @@ | ||
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.GetEnvVars = void 0; | ||
const get_env_vars_1 = require("./get-env-vars"); | ||
__export(require("./env-cmd")); | ||
// Export the core env-cmd API | ||
__exportStar(require("./types"), exports); | ||
__exportStar(require("./env-cmd"), exports); | ||
exports.GetEnvVars = get_env_vars_1.getEnvVars; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.spawn = void 0; | ||
const spawn = require("cross-spawn"); | ||
exports.spawn = spawn; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,7 +89,8 @@ export class TermSignals { | |
*/ | ||
public _terminateProcess (code?: number, signal?: NodeJS.Signals): void { | ||
if (signal !== undefined) { | ||
return process.kill(process.pid, signal) | ||
process.kill(process.pid, signal) | ||
return | ||
Comment on lines
+92
to
+93
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. linting was failing for me on this one, because |
||
} | ||
if (code !== undefined) { | ||
return process.exit(code) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,12 +7,15 @@ describe('expandEnvs', (): void => { | |
notvar: 'this is not used', | ||
dollar: 'money', | ||
PING: 'PONG', | ||
IP1: '127.0.0.1' | ||
IP1: '127.0.0.1', | ||
WHO: 'World' | ||
} | ||
const args = ['notvar', '$dollar', '\\$notvar', '-4', '$PING', '$IP1', '\\$IP1', '$NONEXIST'] | ||
const argsExpanded = ['notvar', 'money', '\\$notvar', '-4', 'PONG', '127.0.0.1', '\\$IP1', '$NONEXIST'] | ||
|
||
it('should replace environment variables in args', (): void => { | ||
// eslint-disable-next-line no-template-curly-in-string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I needed to disable the rule for this line, otherwise I could not commit my test string. |
||
const args = ['notvar', '$dollar', '\\$notvar', '-4', '$PING', '$IP1', '\\$IP1', '$NONEXIST', '"Hello ${WHO}"'] | ||
const argsExpanded = ['notvar', 'money', '\\$notvar', '-4', 'PONG', '127.0.0.1', '\\$IP1', '$NONEXIST', '"Hello World"'] | ||
|
||
it.only('should replace environment variables in args', (): void => { | ||
const res = args.map(arg => expandEnvs(arg, envs)) | ||
assert.sameOrderedMembers(res, argsExpanded) | ||
}) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation does not check, if the pattern is contained in a string. That would make the regex way more complex.
Currently something like
env-cmd -x ${WHO}
would also be expanded. I don’t think that is a big deal, but if so we could use something like((?<="[^"]*)\${([a-zA-Z0-9_]+)}(?=[^"]*")|(?<!\\)\$([a-zA-Z0-9_]+))
.