@effected/env

中文文档
0.12.0发布于 2026/11/10MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 8216

Effect-native environment detection: agent, CI, terminal colour level, hyperlinks, columns and audience — read once through Config, swapped in tests with layerTest

原始使用文档
文档目录6 节
  1. Why @effected/env
  2. Install
  3. Quick start
  4. What it exports
  5. Documentation
  6. License

@effected/env

npm License: MIT Node.js %3E%3D24.11.0 TypeScript 7.0

Effect-native environment detection for any front end of a tool: who is running it (an AI agent, a CI job, or neither), what the terminal can do (colour level, OSC 8 hyperlinks and width, per stream), and who the output is for (a human, an agent or CI). Every answer is read once through Config, never from process, and every service has a layerTest, so a test sets the environment it wants without touching a global.

Pre-1.0.0. This package is part of the @effected/* kit, built on stable Effect v4 (effect ~4.1.0) and still in 0.x development. Stable Effect makes a kit 1.0.0 possible, not automatic. To keep your effect and @effect/* versions on the line the kit is built and tested against, install @effected/pnpm-plugin-effected.

Stability: unstable. This package's API surface is not yet considered complete and may change across 0.x releases. Pin an exact version. Full policy: release strategy.

Why @effected/env

Every tool that prints ends up asking these questions, usually by reading process.env and process.stdout.isTTY wherever the answer is needed. The answers then drift (the help text and the report disagree about colour), they cannot be tested without mutating globals, and the rules are subtle: FORCE_COLOR beats NO_COLOR, an empty NO_COLOR means unset, TERM=dumb is a terminal that cannot move the cursor, a Windows console sets no TERM at all yet draws truecolor, and an agent running inside CI should get agent output.

This package makes each decision once, as a service, from rules taken from established sources: colour depth from Node's own getColorDepth (with OS=Windows_NT standing in for its platform check, which gives a Windows terminal truecolor as Node does from Windows 10 build 14931), hyperlink support from std-osc8's terminal table, and agent detection from std-env's table.

Install

npm install @effected/env effect
pnpm add @effected/env effect

Requires Node.js >=24.11.0. effect v4 is the only peer. There are no runtime dependencies, no platform import and no @effected/* edge, so it runs unchanged on Node, Bun and Deno.

All @effected/* packages are ESM-only: the exports maps publish only import conditions, so require() fails with Node's ERR_PACKAGE_PATH_NOT_EXPORTED. Import from an ES module.

Quick start

Decide the audience once, as a service, and let anything that prints ask it:

import { Audience, CurrentRuntimeEnv } from "@effected/env";
import { Effect, Layer } from "effect";

// A valid MYTOOL_AUDIENCE beats detection; then an agent, then CI, then a human.
const AudienceLive = Audience.layer({ envVar: "MYTOOL_AUDIENCE" }).pipe(Layer.provide(CurrentRuntimeEnv.layer));

const program = Effect.gen(function* () {
  const audience = yield* Audience;
  if (audience.kind === "agent") {
    // plain text, no escapes
  }
});

Effect.runPromise(program.pipe(Effect.provide(AudienceLive)));

What the terminal can do comes from TerminalEnv, which reads core's Stdio and Terminal from your platform layer:

import { TerminalEnv } from "@effected/env";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { Effect } from "effect";

const describe = Effect.gen(function* () {
  const terminal = yield* TerminalEnv;
  return `${terminal.stdout.color} colour, ${terminal.width()} columns`;
});

Effect.runPromise(describe.pipe(Effect.provide(TerminalEnv.layer()), Effect.provide(NodeServices.layer)));

In a test, fix the answers instead:

import { Audience, TerminalEnv } from "@effected/env";
import { Layer, Option } from "effect";

const Environment = Layer.mergeAll(
  Audience.layerTest("agent"),
  TerminalEnv.layerTest({ stdout: { isTerminal: true, color: "256", columns: Option.some(120) } }),
);

What it exports

  • RuntimeEnv: a schema-class snapshot of the agent, the CI and the terminal program, persistable as plain JSON. RuntimeEnv.fromRecord(env) builds one as a pure function.
  • CurrentRuntimeEnv: that snapshot as a service. layer reads the ambient ConfigProvider once; layerFrom(source) reads a record or provider of your own, fresh on every use, for a long-lived host; layerTest(overrides) fixes it.
  • TerminalEnv: per-stream isTerminal, colour level (none, basic, 256, truecolor), hyperlinks and columns, plus width(fallback). layer() needs Stdio and Terminal; layerStdio() needs only Stdio; colorLevel("stdout") decides colour alone.
  • Audience: human, agent or ci, and the source that decided it. layer({ envVar }) decides from CurrentRuntimeEnv and an override variable you name.
  • EnvOverride: reads a variable that picks a mode within an audience, accepting only the literals you list per audience. readResult reports without logging; read logs one warning for a rejected value.

Documentation

The guides and the full API reference are at effected.spencerbeg.gs/env. @effected/cli-kit builds its whole presentation layer on these services.

License

MIT

版本与发布记录

@effected/env 共发布 14 个版本,首次发布于 2026/10/02;过去 12 个月发布了 14 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.12.0latest2026/11/102 天前
0.11.02026/11/067 天前
0.10.02026/11/0310 天前
0.9.02026/11/0112 天前
0.8.02026/10/3014 天前
0.7.02026/10/2915 天前

关于 @effected/env 的常见问题

@effected/env 是什么?

Effect-native environment detection: agent, CI, terminal colour level, hyperlinks, columns and audience — read once through Config, swapped in tests with layerTest

如何安装 @effected/env?

在项目目录运行 npm install @effected/env,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:effect(~4.1.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@effected/env 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@effected/env 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@effected/env 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24.11.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@effected/env 可以商用吗?

@effected/env 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@effected/env 的下载量有多少?

根据 npm 官方下载统计,@effected/env 在 2026/10/13 – 2026/11/11 这 30 天内被下载 8,216 次,日均约 274 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@effected/env 还在维护吗?

最新版本 0.12.0 发布于 2026/11/10(2 天前);至今共发布 14 个版本,其中过去 12 个月发布了 14 个;首次发布于 2026/10/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。