Skip to content

Commit

Permalink
Pass args to dynamic package scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
csweichel committed Sep 9, 2022
1 parent 2ed3279 commit e4d93ae
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ config:
## Dynaimc package scripts
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:

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

<table>
<tr>
Expand All @@ -214,7 +217,7 @@ for(let i = 0; i < 5; i++) {
type: "generic",
config: {
commands: [
["echo", "hello from "+i]
["echo", args.msg + ": hello from "+i]
]
}
});
Expand Down Expand Up @@ -244,17 +247,17 @@ pacakages:
type: generic
config:
commands:
- ["echo", "hello from 1"]
- ["echo", "${msg}: hello from 1"]
- name: hello-2
type: generic
config:
commands:
- ["echo", "hello from 2"]
- ["echo", "${msg}: hello from 2"]
- name: hello-3
type: generic
config:
commands:
- ["echo", "hello from 3"]
- ["echo", "${msg}: hello from 3"]
...
```

Expand Down
2 changes: 1 addition & 1 deletion fixtures/pkgs/generic/BUILD.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ for(let i = 0; i < 10; i++) {
type: "generic",
config: {
commands: [
["echo", "hello from "+i]
["echo", args.msg + " hello from "+i]
]
}
});
Expand Down
8 changes: 6 additions & 2 deletions pkg/leeway/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ func loadComponent(ctx context.Context, workspace *Workspace, path string, args

builderFN := strings.TrimSuffix(path, ".yaml") + ".js"
if _, err := os.Stat(builderFN); err == nil {
addFC, err := runPackageBuilder(builderFN)
addFC, err := runPackageBuilder(builderFN, args)
if err != nil {
return Component{}, err
}
Expand Down Expand Up @@ -896,7 +896,7 @@ func mergeEnv(pkg *Package, src []string) error {
return nil
}

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

vm := goja.New()
err = vm.Set("args", args)
if err != nil {
return nil, err
}
_, err = vm.RunString(string(prog))
if err != nil {
return nil, err
Expand Down

0 comments on commit e4d93ae

Please sign in to comment.