Skip to content

Commit

Permalink
Rename the module and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Arylo committed Nov 28, 2018
1 parent 9e58dd7 commit 0f555f0
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 13 deletions.
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# nodenv
# NODE-ENV Manager

Operating on the NODE_ENV environmental variable

Expand All @@ -10,8 +10,23 @@ Operating on the NODE_ENV environmental variable
[![Author][AUTHOR_URL]][AUTHOR_HREF]
[![license][LICENSE_URL]][LICENSE_HREF]

[NPM_URL]: https://img.shields.io/node/v/nodenv.svg?style=flat-square&maxAge=600
[NPM_HREF]: https://www.npmjs.com/package/nodenv
## Usage

```javascript
const { ENV } = require("node-env-manager");

ENV.PROD // -> "production"
ENV.DEV // -> "development"
ENV.LOCAL // -> "local"
ENV.TEST // -> "test"
ENV.EXP // ->"experience"
ENV.GRAY // -> "gray"
ENV.CI // -> "ci"

```

[NPM_URL]: https://img.shields.io/node/v/node-env-manager.svg?style=flat-square&maxAge=600
[NPM_HREF]: https://www.npmjs.com/package/node-env-manager
[TRAVIS_URL]: https://img.shields.io/travis/Arylo/nodenv/master.svg?style=flat-square&logo=travis&maxAge=600
[TRAVIS_HREF]: https://travis-ci.org/Arylo/nodenv
[COVERALLS_URL]: https://img.shields.io/coveralls/github/Arylo/nodenv/master.svg?style=flat-square&maxAge=600
Expand Down
51 changes: 42 additions & 9 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
/** NODE_ENV 集合 */
export enum ENV {
/** 开发环境 */
PROD = "production",
/** 生产环境 */
DEV = "development",
/** 本地环境 */
LOCAL = "local",
/** 测试环境 */
TEST = "test",
/** 正式环境 */
EXP = "experience",
/** 灰度环境 */
GRAY = "gray",
/** CI 环境 */
CI = "ci"
}

/**
* process.env.NODE_ENV 不存在时,初始化为`env`
* @param env {string} [env="development"]
*/
export const init = (env = "development") => {
if (!process.env.NODE_ENV) {
set(env);
}
export const init = (env: ENV | string = ENV.DEV) => {
return !process.env.NODE_ENV ? set(env) : false;
};

/**
Expand All @@ -17,10 +33,14 @@ export const get = () => {

/**
* 设置process.env.NODE_ENV 为`env`
* @param env {string} [env="development"]
* @param env {string}
*/
export const set = (env = "development") => {
process.env.NODE_ENV = env.trim().toUpperCase();
export const set = (env: ENV | string) => {
if (typeof env === "string") {
env = env.trim().toLowerCase();
}
process.env.NODE_ENV = env;
return true;
};

/**
Expand Down Expand Up @@ -84,7 +104,7 @@ export const isProd = () => {
};

/**
* 是否Gray 环境
* 是否正式环境
* @returns boolean
*/
export const isExp = () => {
Expand All @@ -101,7 +121,7 @@ export const isExp = () => {
};

/**
* 是否Gray 环境
* 是否灰度环境
* @returns boolean
*/
export const isGray = () => {
Expand All @@ -112,3 +132,16 @@ export const isGray = () => {
env = env.trim().toUpperCase();
return ["gray"].map((item) => item.trim().toUpperCase()).indexOf(env) !== -1;
};

/**
* 是否CI 环境
* @returns boolean
*/
export const isCi = () => {
let env = process.env.NODE_ENV;
if (typeof env !== "string") {
return false;
}
env = env.trim().toUpperCase();
return ["ci"].map((item) => item.trim().toUpperCase()).indexOf(env) !== -1;
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "nodenv",
"name": "node-env-manager",
"version": "1.0.0",
"description": "Operating on the NODE_ENV environmental variable",
"main": "dist/index.js",
Expand Down

0 comments on commit 0f555f0

Please sign in to comment.