2

If I am inside a project, I'd like to be able to run a command that outputs the name of that project dynamically. Is there a native yarn command?

Note that macs don't have jq (which would make it easy...just read package.json`

Example

my-project
  - package.json <- read package name
  - src
     - some_mod <- run command here in command line (ex: `yarn getCurrentPackage`)
leonheess
  • 16,068
  • 14
  • 77
  • 112
Toli
  • 5,547
  • 8
  • 36
  • 56
  • Vote on [this proposal](https://meta.stackoverflow.com/questions/354583/disentangle-the-yarn) to ease the tag confusion around [yarn] and [yarnpkg]. – leonheess Feb 11 '20 at 16:29

1 Answers1

3

Here you go!

PACKAGE_NAME=$(cut -d "=" -f 2 <<< $(npm run env | grep "npm_package_name"))
STA
  • 30,729
  • 8
  • 45
  • 59
Mike Rodov
  • 569
  • 1
  • 4
  • 18
  • 2
    Beautiful! Thank you, MIke! Slightly easier to read, but exact same solution is: PACKAGE_NAME=$( npm run env | grep npm_package_name | cut -d= -f2 ) – Dmitry Shevkoplyas Dec 15 '21 at 20:15