Navigation Menu

Skip to content

Commit

Permalink
⭐ Typescript re-write using robb-j/ts-node-base
Browse files Browse the repository at this point in the history
  • Loading branch information
robb-j committed Apr 5, 2019
1 parent e13680d commit 4b4a66a
Show file tree
Hide file tree
Showing 15 changed files with 6,565 additions and 1,081 deletions.
5 changes: 4 additions & 1 deletion .editorconfig
@@ -1,4 +1,7 @@
# Editor config - For sharing IDE prefs ~ https://editorconfig.org/
#
# Editor config, for sharing IDE preferences ~ https://editorconfig.org/
#

root = true

[*]
Expand Down
23 changes: 0 additions & 23 deletions .eslintrc.js

This file was deleted.

6 changes: 5 additions & 1 deletion .gitignore
@@ -1,5 +1,9 @@
#
# Ignore files from git source control
#

node_modules
coverage
.DS_Store
.nyc_output
*.env
dist
7 changes: 7 additions & 0 deletions .prettierignore
@@ -0,0 +1,7 @@
#
# Files for prettier to ignore
#

dist
coverage
node_modules
73 changes: 69 additions & 4 deletions README.md
@@ -1,16 +1,56 @@
# Valid Environment

Check your node environment is set up just right and fail if variables aren't set.
A super slim, zero dependency package for checking environment variables aren't undefined.

> NOTE: It checks variables for `=== undefined` so if a variable is set to
> another [falsy value](https://developer.mozilla.org/en-US/docs/Glossary/Falsy)
> it will pass the check.
> This means you can safely set variables to those falsy values if you want to.
## Package motivation

I created this package because lots of my npm-based projects needed to check certain environment variables were set and I didn't want to keep copying the code from repo to repo.

Its designed to be as light as possible so there are no dependancies and minimal code, I'm just interested in repurposing this same code quickly.

I also like to make my code human-readable so the function names are longer but hopefully have more meaning to the reader.

## Usage

There are three ways of using this package, it also provides
[type definitions](/dist/index.d.ts) so you can easily use it in TypeScript.

### 1 - Exit the current process

This is the primary use case, you can put this snippet during the startup
of your project and quickly assert all the required environment variables are set.

**in JavaScript**

```js
const validateEnv = require('valid-env')
const { validateEnv } = require('valid-env')

validateEnv([ 'DATABASE_URL', 'SERVICE_KEY', 'NUM_CARROTS' ])
validateEnv(['MYSQL_URI', 'SERVICE_KEY', 'NUM_CARROTS'])

// exit 1: Missing environment variables: SERVICE_KEY, NUM_CARROTS
```

Check your node environment is set up just right and fail if variables aren't set.
**in TypeScript**

```ts
import validateEnv from 'valid-env'

validateEnv(['MYSQL_URI', 'SERVICE_KEY', 'NUM_CARROTS'])

// exit 1: Missing environment variables: SERVICE_KEY, NUM_CARROTS
```

Or if you want, place this in your **package.json**:
### 2 - As a package.json script

An alternate case is to use a pre-x script in your `package.json`
to check environment variables there.
This works when you have this package as a dependency.

```json
{
Expand All @@ -19,3 +59,28 @@ Or if you want, place this in your **package.json**:
}
}
```

### 3 - Throwing errors

You can also use the internal `#checkVariables` method to only throw an error,
if you don't want to exit the process.

**in JavaScript**

```js
const { checkVariables } = require('valid-env')

checkVariables(['AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY', 'MYSQL_URI'])
```

**in TypeScript**

```ts
import { checkVariables } from 'valid-env'

checkVariables(['AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY', 'MYSQL_URI'])
```

---

> This project was setup with [robb-j/ts-node-base](https://github.com/robb-j/ts-node-base)

0 comments on commit 4b4a66a

Please sign in to comment.