Skip to content

[Idea]: env() helper for temporary environment variables #1432

@alejaranda

Description

@alejaranda

What's your idea?

Add a small helper env() to simplify running commands with temporary environment variables.

Currently this requires repeating ...process.env:

await $({
  env: { ...process.env, FOO: "hello" }
})`echo $FOO`

Proposed usage:

await env({ FOO: "hello" })`echo $FOO`

The helper would internally wrap $ options.

Why is that needed? How it may be useful?

✨ Improves ergonomics for a common scripting pattern
🐚 Closer to shell syntax (FOO=bar command)
📖 Reduces boilerplate in scripts

This seems consistent with existing helpers like cd(), sleep(), echo(), etc.

Maybe you have a demo or example?

Current:

await $({
  env: { ...process.env, NODE_ENV: "production" }
})`node build.js`

Proposed:

await env({ NODE_ENV: "production" })`node build.js`

Possible minimal implementation:

export function env(vars: Record<string, string>) {
  return $({
    env: {
      ...process.env,
      ...vars,
    },
  })
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions