bun icon indicating copy to clipboard operation
bun copied to clipboard

Add support for npm_package_config_* variables

Open ashsearle opened this issue 3 years ago • 7 comments

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

ashsearle avatar Dec 01 '21 14:12 ashsearle

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:

  • .env
  • .env.local
  • .env.development

Jarred-Sumner avatar Dec 02 '21 23:12 Jarred-Sumner

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

trnxdev avatar Jul 17 '23 13:07 trnxdev

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

igorvolocRC avatar Nov 07 '23 23:11 igorvolocRC

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",
  ...
}

igorvolocRC avatar Nov 08 '23 18:11 igorvolocRC

+1 This would be helpful

whyman avatar Apr 28 '24 19:04 whyman

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?

crazy-djactor avatar May 10 '24 18:05 crazy-djactor

Even though you can't currently use npm_package_config_* 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.

ChurroC avatar May 11 '24 20:05 ChurroC

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

otecd avatar Jun 12 '24 12:06 otecd