bun
bun copied to clipboard
Add support for npm_package_config_* variables
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
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
I can work on it! seems like a good issue for me
Hi Bun team, Do you have this feature on the roadmap? Is there any ETA?
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",
...
}
+1 This would be helpful
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?
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.
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