npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

coa-env

v1.2.0

Published

env component for coa

Downloads

13

Readme

coa-env

GitHub license npm version npm downloads PRs Welcome

COA 框架环境配置包装器,用于统一化通用的环境和配置信息

数据结构

// 以下属性均为只读属性,在实例创建时刻就已经固定,实例生成后无法修改

class CoaEnv {
  // runEnv 运行环境,一般定义为开发类环境('d0' 'd1' 'd2') 测试类环境('t0' 't1' 't2') 生产类环境('v0' 'v1' 'v2')等
  // 由环境变量 process.env.RUN_ENV 控制,如果没有定义,则默认为 'd0'
  public readonly runEnv: string

  // runEnvType 运行环境类型,单字母形式,便于判断某一类环境,根据 runEnv 自动判断,如 'd' 't' 'v'
  public readonly runEnvType: 'd' | 't' | 'v' | string

  // runEnvName 运行环境的名称,便于对外展示环境的名称,根据 runEnvType 自动判断,如 'alpha' 'beta' 'online'
  public readonly runEnvName: 'alpha' | 'beta' | 'online' | 'unknown' | string

  // cwd 当前运行Node.js进程的工作目录,由 process.cwd() 控制
  public readonly cwd: string

  // name 当前包名,由 package.json 中的 name 控制
  public readonly name: string

  // isProd 是否是生产环境,由 process.env.NODE_ENV === 'production' 控制,只要不是 'production' 均为非生产环境
  public readonly isProd: boolean

  // isOnline 是否是线上环境,由 runEnvType === 'v' 控制,只要不是 'v' 均为非线上环境
  public readonly isOnline: boolean

  // hostname 当前主机名称,由 process.env.HOSTNAME 控制,默认为 'local'
  public readonly hostname: string

  // 当前运行的版本号,解耦出来,由外部程序控制,创建实例时必传此参数
  public readonly version: string
}

以上为 TypeScript 代码

用法

安装

yarn add coa-env

示例

import { CoaEnv } from 'coa-env'

// 创建一个新的环境实例
const appEnv = new CoaEnv('1.0.0')

// 判断是否是线上环境
if (appEnv.isOnline) {
  // do something
}

// 判断是否是测试类环境
if (appEnv.runEnvType === 't') {
  // do something
}

// 获取版本
const version = appEnv.version

// 获取环境名称
const runEnvName = appEnv.runEnvName

// 获取当前工作目录
const cwd = appEnv.cwd

配置自动选择

定义不同环境的配置表,会自动根据环境类型返回对应环境的配置信息

  • 精确返回对应环境的配置
// 用户 d1 t1 v1 等精确的环境,精确返回对应环境的配置
const hostConfig1 = appEnv.getConfig({
  d1: { host: '127.0.0.1' },
  t1: { host: '192.168.0.1' },
  v1: { host: '172.16.0.1' },
})
// 当环境是d1,返回 { host: '127.0.0.1' }
// 当环境是t1,返回 { host: '192.168.0.1' }
// 当环境是v1,返回 { host: '172.16.0.1' }
// 当环境是v2,报错 CoaError: Env.ConfigNotFound 配置信息不存在
  • 返回一类环境的配置
// 用 d t v 可以代表一类环境,返回该类环境
const hostConfig2 = appEnv.getConfig({
  d: { host: '127.0.0.1' },
  t: { host: '192.168.0.1' },
  v: { host: '172.16.0.1' },
})
// 当环境是d1 d2 d3,返回 { host: '127.0.0.1' }
// 当环境是t1 t2 t3,返回 { host: '192.168.0.1' }
// 当环境是v1 v2 v3,返回 { host: '172.16.0.1' }
// 当环境是a1,报错 CoaError: Env.ConfigNotFound 配置信息不存在
  • 返回默认环境配置
// 用 $ 可以代表默认配置,当配置表不存在对应环境时,返回默认配置
const hostConfig3 = appEnv.getConfig({
  $: { host: '127.0.0.1' },
  v: { host: '172.16.0.1' },
})
// 当环境是t1 t2 t3,返回 { host: '192.168.0.1' }
// 其他环境,一律返回 { host: '127.0.0.1' }