@delucis/if-env

中文文档
1.1.2发布于 2019/06/11GPL-3.0-only近 30 天下载 5651

Run an npm script if an environment variable matches a pattern

原始使用文档
文档目录6 节
  1. Installation
  2. Usage
  3. Multiple conditions
  4. Negating conditions
  5. Escaping characters
  6. Case sensitivity

@delucis/if-env

latest npm release version Build Status Coverage Status Greenkeeper badge npms.io package score

Run an npm script if an environment variable matches a pattern

This is a fork of Eric Clemmons’s if-env. It allows you to match environment variables using wildcards (*) rather than using simple string equality. See Sindre Sorhus’s matcher for details about how the matching works.

Installation

npm install --save @delucis/if-env

Usage

This package installs if-env, a script which lets you easily match environment variable values against a pattern using wildcards. It also includes if-env-cs, which is case sensitive.

If you want to to run an NPM script conditionally, depending on the value of an environment variable, you can use if-env in your package.json. In this example, we only want to run our test script when the SOME_VAR variable starts with new:

"scripts": {
  "test": "if-env SOME_VAR=new* && npm run test-suite"
}

Multiple conditions

If you want several conditions to be met you can pass them all to if-env:

"scripts": {
  "yay": "if-env VAR1=woo* VAR2=*hoo && echo yay"
}

If you want to run a script if either one or another condition is met, you can use the || operator:

"scripts": {
  "moo": "if-env ANIMAL=cow || if-env ANIMAL=bull* && echo moo"
}

If you want to do different things depending on the value of a variable, you can also use the || operator:

"scripts": {
  "joy": "if-env MOOD=happy && echo 😄 || echo 😭"
}

Negating conditions

If you want to do something if a variable does not match a pattern, you have two options. Note the single quotes in the first example; they escape ! for your shell.

"scripts": {
  "ifenv": "if-env MOOD='!happy' && echo 😭",
  "shell": "if-env MOOD=happy || echo 😭"
}

Escaping characters

If you need to have spaces in your pattern, make sure you wrap it in quotes:

"scripts": {
  "spacious": "if-env SENTENCE='I want * spaces.' && echo You get spaces!"
}

Case sensitivity

if-env is case insensitive, which means if-env VAR=foo will match both foo and FOO. If you need to test a variable and case is important, use the if-env-cs command:

"scripts": {
  "case": "if-env-cs WORD=lower && echo nice! || echo STOP SHOUTING!"
}

版本与发布记录

@delucis/if-env 共发布 5 个版本,首次发布于 2018/05/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.2latest2019/06/117 年前
1.1.12018/05/298 年前
1.1.02018/05/198 年前
1.0.02018/05/188 年前
0.1.02018/05/178 年前

关于 @delucis/if-env 的常见问题

@delucis/if-env 是什么?

Run an npm script if an environment variable matches a pattern

如何安装 @delucis/if-env?

在项目目录运行 npm install @delucis/if-env,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 if-env、if-env-cs,可以用 npx if-env 直接运行。

@delucis/if-env 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/delucis__if-env 是否存在。

@delucis/if-env 可以商用吗?

@delucis/if-env 使用 GPL-3.0-only 许可。强 Copyleft:分发包含该代码的软件时,整个作品需以 GPL-3.0 兼容许可开源,并附带专利与反锁定条款。仅内部使用不触发此义务。以上为概括,不构成法律意见,请以许可原文为准。

@delucis/if-env 的下载量有多少?

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

@delucis/if-env 还在维护吗?

最新版本 1.1.2 发布于 2019/06/11(7 年前);至今共发布 5 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/05/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。