oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.3k stars 2.69k forks source link

Add support for npm_package_config_* variables #73

Open ashsearle opened 2 years ago

ashsearle commented 2 years ago

To minimise repetition in package.json scripts, it'd be useful to support the $npm_package_config_ prefix enabling:

{
  "name": "whatever",
  "config": {
    "buildCommand": "echo env TS_NODE_PROJECT=tsconfig.common.json some-bundler"
  },
  "scripts": {
    "build:dev": "$npm_package_config_buildCommand --mode development",
    "build:prod": "$npm_package_config_buildCommand --mode production"
  }
}

For npm run build:dev, yarn run build:dev and pnpm run build:dev the result is:

env TS_NODE_PROJECT=tsconfig.common.json some-bundler --mode development

For bun, the output is currently 😢

bun run build:dev
$ $npm_package_config_buildCommand --mode development
/usr/local/bin/bash: line 1: --mode: command not found
Script error "build:dev" exited with 127 status
Jarred-Sumner commented 2 years ago

I had no idea this exists but I will add support for it. I saw references to the environment variables in npm & pnpm's code, but hadn't seen any usages of "config" before. I see it in the docs: https://docs.npmjs.com/cli/v8/configuring-npm/package-json#config

For now, you can use a .env file if you'd like. bun run will read those.

For example:

TS_NODE_PROJECT="tsconfig.common.json some-bundler"

This can be in any of:

trnxdev commented 1 year ago

I can work on it! seems like a good issue for me

igorvolocRC commented 10 months ago

Hi Bun team, Do you have this feature on the roadmap? Is there any ETA?

igorvolocRC commented 10 months ago

Hi @Jarred-Sumner, I'm sorry to ping you directly, but can you provide some updates for this feature? I was looking to create some scripts to create migrations for TypeORM https://wanago.io/2022/07/25/api-nestjs-database-migrations-typeorm/

"scripts": {
  "typeorm": "ts-node ./node_modules/typeorm/cli",
  "typeorm:run-migrations": "npm run typeorm migration:run -- -d ./typeOrm.config.ts",
  "typeorm:generate-migration": "npm run typeorm -- -d ./typeOrm.config.ts migration:generate ./migrations/$npm_config_name",
  "typeorm:create-migration": "npm run typeorm -- migration:create ./migrations/$npm_config_name",
  "typeorm:revert-migration": "npm run typeorm -- -d ./typeOrm.config.ts migration:revert",
  ...
}
whyman commented 4 months ago

+1 This would be helpful

crazy-djactor commented 4 months ago

Hi @Jarred-Sumner, I'm sorry to ping you directly, but can you provide some updates for this feature? I was looking to create some scripts to create migrations for TypeORM https://wanago.io/2022/07/25/api-nestjs-database-migrations-typeorm/

"scripts": {
  "typeorm": "ts-node ./node_modules/typeorm/cli",
  "typeorm:run-migrations": "npm run typeorm migration:run -- -d ./typeOrm.config.ts",
  "typeorm:generate-migration": "npm run typeorm -- -d ./typeOrm.config.ts migration:generate ./migrations/$npm_config_name",
  "typeorm:create-migration": "npm run typeorm -- migration:create ./migrations/$npm_config_name",
  "typeorm:revert-migration": "npm run typeorm -- -d ./typeOrm.config.ts migration:revert",
  ...
}

Is there any solution for this?

ChurroC commented 4 months ago

Even though you can't currently use npm_packageconfig* there are still ways to pass arguments to scripts like below. If you have a script like so

"scripts": {
    "scriptName": "echo $argumentName"
}

You can then do argumentName=value bun run scriptName. Make sure you the argumentName has a $ dollar side prepended like above.

otecd commented 3 months ago

Is there any solution for this?

Yes. @ChurroC suggested the right solution, and I want to add a little info about real case of @igorvolocRC

Rewrite the variable in your script this way:

{
  "...": "...",
  "typeorm:generate-migration": "npm run typeorm -- -d ./typeOrm.config.ts migration:generate ./migrations/$(echo $NAME)",
  "...": "...",
}

Then pass it

$ NAME=SomeName npm run typeorm:generate-migration