Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
imaustink committed Jul 22, 2023
0 parents commit 5d15426
Show file tree
Hide file tree
Showing 11 changed files with 4,532 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: Node.js CI

on:
push:
branches: ['main']
pull_request:
branches: ['main']

jobs:
build:
name: 'Automated Tests'
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [14.x, 16.x, 18.x, 20.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run lint
- run: npm test
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
coverage
dist
.DS_Store
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Austin Kurpuis

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Env Magic

![Automated Tests](https://github.com/imaustink/envmagic/actions/workflows/build.yml/badge.svg)
<span class="badge-npmversion"><a href="https://npmjs.org/package/envmagic" title="View this project on NPM"><img src="https://img.shields.io/npm/v/envmagic.svg" alt="NPM version" /></a></span>

Manage your environment variables like magic. Just import `env` from `envmagic` and use it like you would [`process.env`](https://nodejs.org/docs/latest-v8.x/api/process.html#process_process_env). By default, accessing `undefined` or empty environment variables throws an error.

```javascript
import { env } from "envmagic";

// Throws an error if NODE_ENV is undefined or ""
const { NODE_ENV } = env;
```

# Motivation

I think we've all seen and written code that looks a lot like this.

```javascript
import assert from "node:assert";

const { FOO } = process.env;

assert(FOO, "FOO is required!");
```

This is error prone, ugly, and can easily become outdated. It's time to automate your environment variable access.

# Customization

Env Magic aims to be highly customizable. You may import the `configuration` object and change setting in your application code, or you can set most setting via environment variables.

| Property | Env | Default | Description |
| ----------- | ---------------------- | -------------- | ------------------------------------------------------------ |
| `onMissing` | `MAGIC_ENV_ON_MISSING` | `error` | [Action](#actions) taken for missing (`undefined`) variables |
| `onEmpty` | `MAGIC_ENV_ON_EMPTY` | `error` | [Action](#actions) taken for missing (`""`) variables |
| `optional` | `MAGIC_ENV_OPTIONAL` | `[]` | Variables for which no [action](#actions) should be taken |
| `logger` | n/a | `console.warn` | Custom logger function |

```javascript
import { configuration } from "envmagic";

configuration.set("onMissing", "warn");
configuration.set("onEmpty", "warn");
configuration.set("logger", (message) => {
// implement custom logger here
});
configuration.set("optional", ["OPTIONAL_VARIABLE"]);
```

# Actions

Actions are the things that Env Magic can do in response to missing or empty environment variables.

- `error`: Throws an error indicating a given variable is missing or empty
- `warn`: Logs a warning indicating a given variable is missing or empty
- `null`: Returns `null` when the given environment variable is missing or empty
- `undefined`: Returns `undefined` when the given environment variable is missing or
6 changes: 6 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
collectCoverage: true,
coverageProvider: "v8",
preset: "ts-jest",
setupFiles: ["./test/setup.js"],
};
Loading

0 comments on commit 5d15426

Please sign in to comment.