Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ryota-ka committed Dec 19, 2021
0 parents commit 9765abb
Show file tree
Hide file tree
Showing 69 changed files with 5,532 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.{md}]
indent_size = 2

[*.{js,ts}]
indent_size = 4

[package.json]
indent_size = 2

[Makefile]
indent_style = tab
17 changes: 17 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
env: {
es6: true,
},
extends: ['@herp-inc'],
parser: '@typescript-eslint/parser',
overrides: [
{
extends: ['@herp-inc/eslint-config-jest'],
files: ['*.test.ts'],
},
],
parserOptions: {
sourceType: 'module',
project: './tsconfig.json',
},
};
33 changes: 33 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Build
on:
push:
branches:
- '**'
tags-ignore:
- '**'
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: 'lts/*'
- name: Install dependencies
run: |
yarn install --frozen-lockfile
- name: Run Prettier
run: |
yarn prettier --check --ignore-unknown .
- name: Typecheck
run: |
yarn tsc --noEmit
- name: Run ESLint
run: |
yarn eslint ./src
- name: Run tests
run: |
yarn jest ./src
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules
/result
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules
/result
14 changes: 14 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"printWidth": 120,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "all",
"overrides": [
{
"files": ["*.js", "*.ts"],
"options": {
"tabWidth": 4
}
}
]
}
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
all: sdist

format: install
yarn prettier --write .

lint: install
yarn eslint ./src

install:
yarn install

sdist:
nix-build

test: install
yarn jest ./src --watch

typecheck: install
yarn tsc --noEmit --watch
76 changes: 76 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# `@herp-inc/environmen-ts` [![npm](https://img.shields.io/npm/v/@herp-inc/environmen-ts)](https://www.npmjs.com/package/@herp-inc/environmen-ts)

A composable environment variable decoder library

## Installation

Note that the following packages are peer dependencies of this library, which need to be installed separately.

| Package | Version |
| ---------------------------------------------- | --------- |
| [`fp-ts`](https://www.npmjs.com/package/fp-ts) | `^2.11.0` |

```sh
$ yarn add @herp-inc/environmen-ts
```

## Example

```typescript
import {
defaultTo,
Environment,
EnvironmentError,
hex,
keyOf,
optional,
port,
required,
} from '@herp-inc/environmen-ts';
import * as E from 'fp-ts/Either';
import { pipe } from 'fp-ts/function';
import * as RE from 'fp-ts/ReaderEither';

type LogLevel = 'debug' | 'info' | 'warn' | 'error';
type Config = {
logLevel: LogLevel;
port: number;
secretKeyBase: Buffer;
};

const portD = required('PORT', port());

const logLevelD = pipe(
optional(
'LOG_LEVEL',
keyOf({
debug: null,
info: null,
warn: null,
error: null,
}),
),
defaultTo<LogLevel>(() => 'info'),
);

const secretKeyBaseD = required('SECRET_KEY_BASE', hex({ length: 32 }), {
// You can optionally mark a variable to be sensitive so that it will not be logged.
sensitive: true,
});

const configD = pipe(
RE.Do,
RE.bind('port', () => portD),
RE.bind('logLevel', () => logLevelD),
RE.bind('secretKeyBase', () => secretKeyBaseD),
);

const env = new Environment(process.env);

const config: E.Either<EnvironmentError, Config> = configD(env);

// Check out what's found and what's missing.
for (const log of env.logs()) {
logger.info(log);
}
```
32 changes: 32 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{ pkgs ? import <nixpkgs> {} }:

let
packageJSON = pkgs.lib.importJSON ./package.json;
in

pkgs.stdenv.mkDerivation rec {
name = "herp-inc-environmen-ts";
version = packageJSON.version;

src = pkgs.nix-gitignore.gitignoreSource [] ./.;

buildInputs = [
pkgs.nodejs-slim
pkgs.yarn
];

buildPhase=''
HOME=$TMP yarn install --frozen-lockfile
yarn rollup --config ./rollup.config.js
cp ./package.json ./README.md ./dist
cd ./dist
yarn pack
cd ..
mv ./dist/*.tgz ./
'';

dontInstall = true;

doDist = true;
tarballs = "herp-inc-environmen-ts-v${version}.tgz";
}
8 changes: 8 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
globals: {
'ts-jest': {
isolatedModules: true,
},
},
preset: 'ts-jest',
};
38 changes: 38 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@herp-inc/environmen-ts",
"description": "A composable environment variable decoder library",
"version": "0.1.0",
"keywords": [
"fp-ts",
"environment variables"
],
"main": "cjs/index.js",
"module": "mjs/index.js",
"types": "cjs/index.d.ts",
"sideEffects": false,
"repository": "https://github.com/herp-inc/environmen-ts",
"license": "MIT",
"devDependencies": {
"@herp-inc/eslint-config": "0.11.0",
"@herp-inc/eslint-config-jest": "0.3.0",
"@rollup/plugin-typescript": "8.3.0",
"@types/jest": "27.0.3",
"@typescript-eslint/eslint-plugin": "5.7.0",
"@typescript-eslint/parser": "5.7.0",
"eslint": "8.5.0",
"eslint-config-prettier": "8.3.0",
"eslint-import-resolver-typescript": "2.5.0",
"eslint-plugin-import": "2.25.3",
"eslint-plugin-jest": "25.3.0",
"fast-check": "2.20.0",
"fp-ts": "2.11.5",
"jest": "27.4.5",
"prettier": "2.5.1",
"rollup": "2.61.1",
"ts-jest": "27.1.2",
"typescript": "4.5.4"
},
"peerDependencies": {
"fp-ts": "^2.11.0"
}
}
3 changes: 3 additions & 0 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["github>herp-inc/renovate.json"]
}
30 changes: 30 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import typescript from '@rollup/plugin-typescript';

export default [
{
input: 'src/index.ts',
output: {
dir: 'dist/cjs',
format: 'cjs',
},
plugins: [
typescript({
exclude: ['**/*.test.ts'],
outDir: 'dist/cjs',
}),
],
},
{
input: 'src/index.ts',
output: {
dir: 'dist/esm',
format: 'esm',
},
plugins: [
typescript({
exclude: ['**/*.test.ts'],
outDir: 'dist/esm',
}),
],
},
];
8 changes: 8 additions & 0 deletions src/Decoder/Decoder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type * as RE from 'fp-ts/ReaderEither';

import type { Environment } from '../Environment';
import type { EnvironmentError } from '../EnvironmentError';

type Decoder<A> = RE.ReaderEither<Environment, EnvironmentError, A>;

export type { Decoder };
Loading

0 comments on commit 9765abb

Please sign in to comment.