Skip to content

Commit e4d93ae

Browse files
committed
Pass args to dynamic package scripts
1 parent 2ed3279 commit e4d93ae

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ config:
195195
## Dynaimc package scripts
196196
Packages can be dynamically produced within a component using a dynamic package script named `BUILD.js`. This ECMAScript 5.1 file is executed using [Goja](https://github.com/dop251/goja) and produces a `packages` array which contains the package struct much like they'd exist within the `BUILD.yaml`. For example:
197197

198+
Leeway interacts with the script using global variables, specifically:
199+
- `args` [input] a JavaScript object containing the build arguments which have explicitely been passed to leeway.
200+
- `packages` [output] where the script produces an array of package structures akin to those found in a `BUILD.yaml` file.
198201

199202
<table>
200203
<tr>
@@ -214,7 +217,7 @@ for(let i = 0; i < 5; i++) {
214217
type: "generic",
215218
config: {
216219
commands: [
217-
["echo", "hello from "+i]
220+
["echo", args.msg + ": hello from "+i]
218221
]
219222
}
220223
});
@@ -244,17 +247,17 @@ pacakages:
244247
type: generic
245248
config:
246249
commands:
247-
- ["echo", "hello from 1"]
250+
- ["echo", "${msg}: hello from 1"]
248251
- name: hello-2
249252
type: generic
250253
config:
251254
commands:
252-
- ["echo", "hello from 2"]
255+
- ["echo", "${msg}: hello from 2"]
253256
- name: hello-3
254257
type: generic
255258
config:
256259
commands:
257-
- ["echo", "hello from 3"]
260+
- ["echo", "${msg}: hello from 3"]
258261
...
259262
```
260263

fixtures/pkgs/generic/BUILD.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ for(let i = 0; i < 10; i++) {
99
type: "generic",
1010
config: {
1111
commands: [
12-
["echo", "hello from "+i]
12+
["echo", args.msg + " hello from "+i]
1313
]
1414
}
1515
});

pkg/leeway/workspace.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ func loadComponent(ctx context.Context, workspace *Workspace, path string, args
659659

660660
builderFN := strings.TrimSuffix(path, ".yaml") + ".js"
661661
if _, err := os.Stat(builderFN); err == nil {
662-
addFC, err := runPackageBuilder(builderFN)
662+
addFC, err := runPackageBuilder(builderFN, args)
663663
if err != nil {
664664
return Component{}, err
665665
}
@@ -896,7 +896,7 @@ func mergeEnv(pkg *Package, src []string) error {
896896
return nil
897897
}
898898

899-
func runPackageBuilder(fn string) (fc []map[string]interface{}, err error) {
899+
func runPackageBuilder(fn string, args Arguments) (fc []map[string]interface{}, err error) {
900900
defer func() {
901901
if err != nil {
902902
err = fmt.Errorf("failed to run package builder script at %s: %w", fn, err)
@@ -909,6 +909,10 @@ func runPackageBuilder(fn string) (fc []map[string]interface{}, err error) {
909909
}
910910

911911
vm := goja.New()
912+
err = vm.Set("args", args)
913+
if err != nil {
914+
return nil, err
915+
}
912916
_, err = vm.RunString(string(prog))
913917
if err != nil {
914918
return nil, err

0 commit comments

Comments
 (0)