microsoft / rushstack

Monorepo for tools developed by the Rush Stack community
https://rushstack.io/
Other
5.81k stars 592 forks source link

[rush] Idea: "rush symlink" built-in command. #4304

Open elliot-nelson opened 12 months ago

elliot-nelson commented 12 months ago

Summary

An issue we've encountered in our repo is that we are consuming a package that lives outside the repo, and sometimes we need to test out a potential update to that package locally.

We've developed a custom script and mapped it to rush symlink, to make it easy for folks in our monorepo to perform this action. Perhaps this script could become the basis of a built-in feature of Rush, for mapping/unmapping a specific package/version locally to the specified target directory.

Details

Here is the current version of our script in its entirety, as a baseline:

#!/usr/bin/env node

/*
    This 'symlink' command is meant to create a "global" symlink of a 3rd party module
    directly in the Rush pnpm store.

    The benefit of such symlink is that it will affect all the projects of the mono-repo
    which depend on the same version of a 3rd party module.

    To reverse the process, run `rush install --purge` to revert all changes to the store.

    # Usage
symlink [--help] [--path] PATH [--version VERSION] [--dry]

- [--path] PATH: path to an external module location that you want to link
- [--version] VERSION: a specific version that you want to link (otherwise all are linked)
- [--dry]: dry-run flag, no filesystem changes
```

# Details

Example, initial layout:

+ acme/
  + common/temp/node_modules/.pnpm/
    + {library}@{version}/
      + node_modules/
        + {library}/
  + apps/
    + {project}/
      + node_modules/
        + {library}/ -> common/temp/node_modules/.pnpm/{library}@{version}/node_modules/{library}

After linking to {local_library}:

+ acme/
  + common/temp/node_modules/.pnpm/
    + {library}@{version}/
      + node_modules/
        + {library}/ -> {local_library}
  + apps/
    + {project}/
      + node_modules/
        + {library}/ -> common/temp/node_modules/.pnpm/{library}@{version}/node_modules/{library} -> {local_library}

*/

const fs = require('fs'); const path = require('path'); const process = require("process");

process.exitCode = 1;

let args; try { args = parseArgs(process.argv) } catch (error) { console.error(error.message); console.error(); console.error(getHelpMessage()); return; }

try { program(args); process.exitCode = 0; } catch (error) { console.error(error.message); console.info('\nTo unlink or fix issues with your Rush pnpm store: rush install --purge'); }

/**

/**

/**

/**

/**

/**

/**

And here is the command definition:

    {
      "name": "symlink",
      "commandKind": "global",
      "summary": "Create a global symlink to an external library.",
      "safeForSimultaneousRushProcesses": true,
      "shellCommand": "node common/scripts/symlink.js"
    }
...
    {
      "longName": "--path",
      "parameterKind": "string",
      "argumentName": "PATH",
      "description": "Path to an external module location to symlink",
      "required": true,
      "associatedCommands": ["symlink"]
    },
    {
      "longName": "--version",
      "parameterKind": "string",
      "argumentName": "VERSION",
      "description": "Link a specific version (otherwise all are linked)",
      "associatedCommands": ["symlink"]
    },
    {
      "longName": "--dry",
      "parameterKind": "flag",
      "description": "Dry-run only",
      "associatedCommands": ["symlink"]
    }

Standard questions

Please answer these questions to help us investigate your issue more quickly:

Question Answer
@microsoft/rush globally installed version? n/a
rushVersion from rush.json? n/a
useWorkspaces from rush.json? yes
Operating system? mac
Would you consider contributing a PR? yes
Node.js version (node -v)? 18
dmichon-msft commented 11 months ago

You should just be able to use the rush-pnpm link command: https://pnpm.io/cli/link

vjau commented 11 months ago

You should just be able to use the rush-pnpm link command: https://pnpm.io/cli/link

@dmichon-msft i couldn't get rush-pnpm link to work I did pnpm link --global in the package (outside monorepo) folder. I then did rush-pnpm link --global package-name inside the folder consuming the package in the monorepo. I then did rush update per instructions. But every time, the package installed is from npm and not from local package.