Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Arylo committed Dec 5, 2018
1 parent f17e282 commit 9aa35eb
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 59 deletions.
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,36 @@ Operating on the NODE_ENV environmental variable

## Usage

### Install

```shell
npm install node-env-manager
```

### CONST

```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"
ENV.PROD // -> "production",
ENV.PRODUCTION // -> "production",
ENV.DEV // -> "dev",
ENV.DEVELOP // -> "develop",
ENV.DEVELOPMENT // -> "development",
ENV.LOCAL // -> "local",
ENV.TEST // -> "test",
ENV.EXP // -> "exp",
ENV.EXPERIENCE // -> "experience",
ENV.GRAY // -> "gray",
ENV.CI // -> "ci"
```

### Mothods

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

env.init(); // set `process.env.NODE_ENV` value if this is null
env.init(); // set `process.env.NODE_ENV` value if the value is null
env.set(); // set `process.env.NODE_ENV` value
env.get(); // get `process.env.NODE_ENV` value

Expand Down
75 changes: 39 additions & 36 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@
export enum ENV {
/** 开发环境 */
PROD = "production",
/** 开发环境 */
PRODUCTION = "production",
/** 生产环境 */
DEV = "dev",
/** 生产环境 */
DEVELOP = "develop",
/** 生产环境 */
DEV = "development",
DEVELOPMENT = "development",
/** 本地环境 */
LOCAL = "local",
/** 测试环境 */
TEST = "test",
/** 正式环境 */
EXP = "experience",
EXP = "exp",
/** 正式环境 */
EXPERIENCE = "experience",
/** 灰度环境 */
GRAY = "gray",
/** CI 环境 */
Expand All @@ -36,7 +44,7 @@ export const get = () => {
* @param env {string}
*/
export const set = (env: ENV | string) => {
if (typeof env !== "string") {
if (!env || typeof env !== "string") {
return false;
}
env = env.trim().toLowerCase();
Expand All @@ -50,11 +58,12 @@ export const set = (env: ENV | string) => {
*/
export const isLocal = () => {
let env = process.env.NODE_ENV;
if (typeof env !== "string") {
if (!env || typeof env !== "string") {
return false;
}
env = env.trim().toUpperCase();
return ["local"].map((item) => item.trim().toUpperCase()).indexOf(env) !== -1;
env = env.trim().toLowerCase();
const envs: string[] = [ ENV.LOCAL ];
return envs.indexOf(env) !== -1;
};

/**
Expand All @@ -63,11 +72,12 @@ export const isLocal = () => {
*/
export const isTest = () => {
let env = process.env.NODE_ENV;
if (typeof env !== "string") {
if (!env || typeof env !== "string") {
return false;
}
env = env.trim().toUpperCase();
return ["test"].map((item) => item.trim().toUpperCase()).indexOf(env) !== -1;
env = env.trim().toLowerCase();
const envs: string[] = [ ENV.TEST ];
return envs.indexOf(env) !== -1;
};

/**
Expand All @@ -76,15 +86,12 @@ export const isTest = () => {
*/
export const isDev = () => {
let env = process.env.NODE_ENV;
if (typeof env !== "string") {
if (!env || typeof env !== "string") {
return false;
}
env = env.trim().toUpperCase();
return (
["dev", "develop", "development"]
.map((item) => item.trim().toUpperCase())
.indexOf(env) !== -1
);
env = env.trim().toLowerCase();
const envs: string[] = [ ENV.DEV, ENV.DEVELOP, ENV.DEVELOPMENT ];
return envs.indexOf(env) !== -1;
};

/**
Expand All @@ -93,15 +100,12 @@ export const isDev = () => {
*/
export const isProd = () => {
let env = process.env.NODE_ENV;
if (typeof env !== "string") {
if (!env || typeof env !== "string") {
return false;
}
env = env.trim().toUpperCase();
return (
["prod", "production"]
.map((item) => item.trim().toUpperCase())
.indexOf(env) !== -1
);
env = env.trim().toLowerCase();
const envs: string[] = [ ENV.PROD, ENV.PRODUCTION ];
return envs.indexOf(env) !== -1;
};

/**
Expand All @@ -110,15 +114,12 @@ export const isProd = () => {
*/
export const isExp = () => {
let env = process.env.NODE_ENV;
if (typeof env !== "string") {
if (!env || typeof env !== "string") {
return false;
}
env = env.trim().toUpperCase();
return (
["exp", "experience"]
.map((item) => item.trim().toUpperCase())
.indexOf(env) !== -1
);
env = env.trim().toLowerCase();
const envs: string[] = [ ENV.EXP, ENV.EXPERIENCE ];
return envs.indexOf(env) !== -1;
};

/**
Expand All @@ -127,11 +128,12 @@ export const isExp = () => {
*/
export const isGray = () => {
let env = process.env.NODE_ENV;
if (typeof env !== "string") {
if (!env || typeof env !== "string") {
return false;
}
env = env.trim().toUpperCase();
return ["gray"].map((item) => item.trim().toUpperCase()).indexOf(env) !== -1;
env = env.trim().toLowerCase();
const envs: string[] = [ ENV.GRAY ];
return envs.indexOf(env) !== -1;
};

/**
Expand All @@ -140,9 +142,10 @@ export const isGray = () => {
*/
export const isCI = () => {
let env = process.env.NODE_ENV;
if (typeof env !== "string") {
if (!env || typeof env !== "string") {
return false;
}
env = env.trim().toUpperCase();
return ["ci"].map((item) => item.trim().toUpperCase()).indexOf(env) !== -1;
env = env.trim().toLowerCase();
const envs: string[] = [ ENV.CI ];
return envs.indexOf(env) !== -1;
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-env-manager",
"version": "1.0.0",
"version": "1.0.1",
"description": "Operating on the NODE_ENV environmental variable",
"main": "dist/index.js",
"types": "lib/index.ts",
Expand All @@ -23,6 +23,8 @@
"environment",
"env",
"nodeenv",
"node-env",
"node_env",
"NODE_ENV",
"test",
"local",
Expand Down
29 changes: 15 additions & 14 deletions test/is.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,64 +5,65 @@ test.beforeEach(() => {
delete process.env.NODE_ENV;
});

test((t) => {
test("isProd", (t) => {
process.env.NODE_ENV = env.ENV.PROD;
t.true(env.isProd());
});

test((t) => {
test("isProd", (t) => {
t.false(env.isProd());
});

test((t) => {
test("isDev", (t) => {
process.env.NODE_ENV = env.ENV.DEV;
t.true(env.isDev());
});
test((t) => {

test("isDev", (t) => {
t.false(env.isDev());
});

test((t) => {
test("isLocal", (t) => {
process.env.NODE_ENV = env.ENV.LOCAL;
t.true(env.isLocal());
});

test((t) => {
test("isLocal", (t) => {
t.false(env.isLocal());
});

test((t) => {
test("isTest", (t) => {
process.env.NODE_ENV = env.ENV.TEST;
t.true(env.isTest());
});

test((t) => {
test("isTest", (t) => {
t.false(env.isTest());
});

test((t) => {
test("isExp", (t) => {
process.env.NODE_ENV = env.ENV.EXP;
t.true(env.isExp());
});

test((t) => {
test("isExp", (t) => {
t.false(env.isExp());
});

test((t) => {
test("isGray", (t) => {
process.env.NODE_ENV = env.ENV.GRAY;
t.true(env.isGray());
});

test((t) => {
test("isGray", (t) => {
t.false(env.isGray());
});

test((t) => {
test("isCI", (t) => {
process.env.NODE_ENV = env.ENV.CI;
t.true(env.isCI());
});

test((t) => {
test("isCI", (t) => {
t.false(env.isCI());
});
11 changes: 11 additions & 0 deletions test/set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,14 @@ test((t) => {
t.true(env.set(env.ENV.DEV));
t.true(env.isDev());
});

test((t) => {
t.false(env.set(1 as any));
});

test((t) => {
env.init();
t.true(env.isDev());
t.false(env.set(undefined as any));
t.true(env.isDev());
});

0 comments on commit 9aa35eb

Please sign in to comment.