Skip to content

Commit 6d46ed9

Browse files
committed
Breaking: supports default values for argument placeholders (fixes #54)
1 parent 6dbc1fe commit 6d46ed9

File tree

2 files changed

+72
-5
lines changed

2 files changed

+72
-5
lines changed

src/lib/index.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const runTasksInSequencial = require("./run-tasks-in-sequencial")
2121
// Helpers
2222
//------------------------------------------------------------------------------
2323

24-
const ARGS_PATTERN = /[{]([*@]|\d+)[}]/g
24+
const ARGS_PATTERN = /\{(!)?([*@]|\d+)([^}]+)?}/g
2525

2626
/**
2727
* Converts a given value to an array.
@@ -44,19 +44,42 @@ function toArray(x) {
4444
* @returns {string[]} replaced
4545
*/
4646
function applyArguments(patterns, args) {
47-
return patterns.map(pattern => pattern.replace(ARGS_PATTERN, (unused, index) => {
48-
if (index === "@") {
47+
const defaults = Object.create(null)
48+
49+
return patterns.map(pattern => pattern.replace(ARGS_PATTERN, (whole, indirectionMark, id, options) => {
50+
if (indirectionMark != null) {
51+
throw Error(`Invalid Placeholder: ${whole}`)
52+
}
53+
if (id === "@") {
4954
return shellQuote.quote(args)
5055
}
51-
if (index === "*") {
56+
if (id === "*") {
5257
return shellQuote.quote([args.join(" ")])
5358
}
5459

55-
const position = parseInt(index, 10)
60+
const position = parseInt(id, 10)
5661
if (position >= 1 && position <= args.length) {
5762
return shellQuote.quote([args[position - 1]])
5863
}
5964

65+
// Address default values
66+
if (options != null) {
67+
const prefix = options.slice(0, 2)
68+
69+
if (prefix === ":=") {
70+
defaults[id] = shellQuote.quote([options.slice(2)])
71+
return defaults[id]
72+
}
73+
if (prefix === ":-") {
74+
return shellQuote.quote([options.slice(2)])
75+
}
76+
77+
throw Error(`Invalid Placeholder: ${whole}`)
78+
}
79+
if (defaults[id] != null) {
80+
return defaults[id]
81+
}
82+
6083
return ""
6184
}))
6285
}

test/argument-placeholders.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,48 @@ describe("[argument-placeholders]", () => {
174174
.then(() => assert(result() === "[\"1st\",\"2nd\",\"1st\",\"2nd\",\"1st 2nd\"]"))
175175
)
176176
})
177+
178+
describe("'{1:-foo}' should be replaced by 'foo' if arguments are nothing:", () => {
179+
it("Node API", () =>
180+
nodeApi("test-task:dump {1:-foo} {1}")
181+
.then(() => assert(result() === "[\"foo\"]"))
182+
)
183+
184+
it("npm-run-all command", () =>
185+
runAll(["test-task:dump {1:-foo} {1}"])
186+
.then(() => assert(result() === "[\"foo\"]"))
187+
)
188+
189+
it("run-s command", () =>
190+
runSeq(["test-task:dump {1:-foo} {1}"])
191+
.then(() => assert(result() === "[\"foo\"]"))
192+
)
193+
194+
it("run-p command", () =>
195+
runPar(["test-task:dump {1:-foo} {1}"])
196+
.then(() => assert(result() === "[\"foo\"]"))
197+
)
198+
})
199+
200+
describe("'{1:=foo}' should be replaced by 'foo' and should affect following '{1}' if arguments are nothing:", () => {
201+
it("Node API", () =>
202+
nodeApi("test-task:dump {1:=foo} {1}")
203+
.then(() => assert(result() === "[\"foo\",\"foo\"]"))
204+
)
205+
206+
it("npm-run-all command", () =>
207+
runAll(["test-task:dump {1:=foo} {1}"])
208+
.then(() => assert(result() === "[\"foo\",\"foo\"]"))
209+
)
210+
211+
it("run-s command", () =>
212+
runSeq(["test-task:dump {1:=foo} {1}"])
213+
.then(() => assert(result() === "[\"foo\",\"foo\"]"))
214+
)
215+
216+
it("run-p command", () =>
217+
runPar(["test-task:dump {1:=foo} {1}"])
218+
.then(() => assert(result() === "[\"foo\",\"foo\"]"))
219+
)
220+
})
177221
})

0 commit comments

Comments
 (0)