forked from tilt-dev/tilt-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add yarn extension for yarn workspace projects (tilt-dev#590)
- Loading branch information
1 parent
e8cb91c
commit b39987b
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |