yarn-workspacesyarn-v3

yarn run: can I get the caller path?


I'm using Yarn 3's workspaces.

If I have a script in a package.json

{
  "name": "myscripts",
  "scripts": {
    "migrate": "./migrate up -m src/migrations"

and in another package foo

{
  "name": "foo",
  "dependencies": {
    "myscripts": "workspace:*"

if I run migrate from the foo package

yarn run migrate

is there a way to get the folder of the foo package in my migrate script?

I want to use the path in my migrate script ./migrate up -m $CALLERS_PATH/migrations


Solution

  • The yarn docs here: https://yarnpkg.com/advanced/lifecycle-scripts indicate that scripts are invoked with an INIT_CWD env var set to the original cwd the script was invoked from.

    You should be able to (in a shell script for example), reference the path with

    ./migrate up -m $INIT_CWD/migrations