Skip to content

Commit

Permalink
Add yarn extension for yarn workspace projects (tilt-dev#590)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyle-seely authored Jul 11, 2024
1 parent e8cb91c commit b39987b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ All extensions have been vetted and approved by the Tilt team.
- [`vault_client`](/vault_client): Reach secrets from a Vault instance.
- [`wait_for_it`](/wait_for_it): Wait until command output is equal to given output.
- [`base64`](/base64): Base64 encode or decode a string.
- [`yarn`](/yarn): Create Tilt resources from package.json scripts in a yarn workspace.

## Contribute an Extension

Expand Down
29 changes: 29 additions & 0 deletions yarn/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Yarn

Create Tilt resources from package.json scripts in a yarn workspace.

## Usage

### Create a Tilt resource for every package.json script in the specified workspace.

```python
load('ext://yarn', 'yarn')

yarn('path/to/package.json')
```

### Additional Usage

See [Configuration](#configuration-tilt_configjson) for parameter descriptions. Function parameters are merged with tilt_config.json values.

```python
load('ext://yarn', 'yarn')

yarn('path/to/package.json', auto_init=[], enabled_workspaces=[], enabled_scripts=[])
```

## Configuration: `tilt_config.json`

- `yarn_auto_init` ( List [ str ] ) – List of scripts to automatically run. The full resource name is expected like `<script>-<workspace>`.
- `yarn_enabled_workspaces` ( List [ str ] ) - List of workspaces to enable.
- `yarn_enabled_scripts` ( List [ str ] ) - List of scripts to enable for yarn. Common examples include `start`, `test`, `build`.
44 changes: 44 additions & 0 deletions yarn/Tiltfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
def yarn(path, auto_init=[], enabled_workspaces=[], enabled_scripts=[]):
config.define_string_list('yarn_auto_init', usage='List of scripts to automatically run. The full tilt name is expected like <script>-<workspace>.')
config.define_string_list('yarn_enabled_workspaces', usage='List of workspaces to enable for yarn.')
config.define_string_list('yarn_enabled_scripts', usage='List of scripts to enable for yarn. Common examples include `start`, `test`, `build`.')
cfg = config.parse()
def is_auto_init(script):
yarn_auto_init = auto_init + cfg.get('yarn_auto_init', [])
return script in yarn_auto_init
def workspace_enabled(workspace):
workspaces = enabled_workspaces + cfg.get('yarn_enabled_workspaces', [])
return workspace in workspaces if workspaces else True
def script_enabled(script):
scripts = enabled_scripts + cfg.get('yarn_enabled_scripts', [])
return script in scripts if scripts else True

root_package_dir = os.path.dirname(path)
workspaces = local(['yarn', 'workspaces', 'list', '--json'], quiet=True, dir=root_package_dir)
for workspace_json in str(workspaces).splitlines():
workspace = decode_json(workspace_json)
sanitized_workspace_name = workspace['name'].replace('@', '').replace('/', '.')
if (workspace_enabled(sanitized_workspace_name)):
scripts = read_json(os.path.join(workspace['location'], 'package.json')).get('scripts', {})
for script in sorted(scripts.keys()):
if (script_enabled(script)):
script_name = '-'.join([script, sanitized_workspace_name])
local_resource(
name=script_name,
cmd=['yarn', 'workspace', workspace['name'], 'run', script],
dir=root_package_dir,
labels=sanitized_workspace_name,
trigger_mode=TRIGGER_MODE_MANUAL,
auto_init=is_auto_init(script_name),
allow_parallel=True,
)

local_resource(
name='yarn-install',
cmd=['yarn', 'install'],
dir=root_package_dir,
labels='yarn',
trigger_mode=TRIGGER_MODE_MANUAL,
auto_init=is_auto_init('yarn-install'),
allow_parallel=True,
)

0 comments on commit b39987b

Please sign in to comment.