Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for npm_package_config_* variables #73

Closed
ashsearle opened this issue Dec 1, 2021 · 8 comments · Fixed by #14786
Closed

Add support for npm_package_config_* variables #73

ashsearle opened this issue Dec 1, 2021 · 8 comments · Fixed by #14786
Labels
bun install Something that relates to the npm-compatible client enhancement New feature or request good first issue Something that would be good for new contributors

Comments

@ashsearle
Copy link

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 Jarred-Sumner added bun run enhancement New feature or request labels Dec 2, 2021
@Jarred-Sumner
Copy link
Collaborator

Jarred-Sumner commented Dec 2, 2021

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

@github-actions github-actions bot removed the bun run label Jul 15, 2022
@Electroid Electroid added the npm label Nov 1, 2022
@github-actions github-actions bot removed the npm label Nov 2, 2022
@paperclover paperclover added the good first issue Something that would be good for new contributors label Jun 3, 2023
@trnxdev
Copy link
Contributor

trnxdev commented Jul 17, 2023

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

@igorvolocRC
Copy link

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

@igorvolocRC
Copy link

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
Copy link

whyman commented Apr 28, 2024

+1 This would be helpful

@crazy-djactor
Copy link

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
Copy link

ChurroC commented May 11, 2024

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.

@otecd
Copy link
Contributor

otecd commented Jun 12, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bun install Something that relates to the npm-compatible client enhancement New feature or request good first issue Something that would be good for new contributors
Projects
None yet