Skip to content

Commit

Permalink
chore(3.1.2): .ts -> .mjs + .d.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed Aug 6, 2023
1 parent 6be8de2 commit 9148c31
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ dealing with files, ports, containers, command line arguments...
|Package|NPM|What it does|Source|Target|
|---|---|---|---|---|
|[**@hackbg/cmds**](./cmds/README.md)|[![NPM version](https://img.shields.io/npm/v/@hackbg/cmds?color=9013fe&label=&style=for-the-badge)](https://www.npmjs.com/package/@hackbg/cmds)|**Command runner.** |TS |Backend |
|[**@hackbg/conf**](./conf/README.md)|[![NPM version](https://img.shields.io/npm/v/@hackbg/conf?color=9013fe&label=&style=for-the-badge)](https://www.npmjs.com/package/@hackbg/conf)|**Environment configuration.** |TS |Isomorphic|
|[**@hackbg/conf**](./conf/README.md)|[![NPM version](https://img.shields.io/npm/v/@hackbg/conf?color=9013fe&label=&style=for-the-badge)](https://www.npmjs.com/package/@hackbg/conf)|**Environment configuration.** |ESM |Isomorphic|
|[**@hackbg/dock**](./dock/README.md)|[![NPM version](https://img.shields.io/npm/v/@hackbg/dock?color=9013fe&label=&style=for-the-badge)](https://www.npmjs.com/package/@hackbg/dock)|**Dockerize commands.** |TS |Backend |
|[**@hackbg/file**](./file/README.md)|[![NPM version](https://img.shields.io/npm/v/@hackbg/file?color=9013fe&label=&style=for-the-badge)](https://www.npmjs.com/package/@hackbg/file)|**Filesystem model.** |TS |Backend |
|[**@hackbg/port**](./port/README.md)|[![NPM version](https://img.shields.io/npm/v/@hackbg/port?color=9013fe&label=&style=for-the-badge)](https://www.npmjs.com/package/@hackbg/port)|**Network port utilities.** |TS |Backend |
Expand Down
33 changes: 13 additions & 20 deletions conf/conf.ts → conf/conf.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,52 @@ import { overrideFiltered } from '@hackbg/over'

export class ConfigError extends Error {
static Required = class EnvConfigRequiredError extends ConfigError {
constructor (name: string, type: string) {
constructor (name, type) {
super(`The environment variable ${name} must be a ${type}`)
}
}
}

export class Config {
constructor (readonly environment: Environment = Environment.initial) {}
override (options: object) {
constructor (environment = Environment.initial) {}
override (options) {
overrideFiltered(false, this, options)
return this
}
getFlag <T extends boolean, U> (name: string, fallback?: ()=>T|U): T|U {
getFlag (name, fallback) {
return this.environment.getFlag<T, U>(name, fallback)
}
getString <T extends string, U> (name: string, fallback?: ()=>T|U): T|U {
getString (name, fallback) {
return this.environment.getString<T, U>(name, fallback)
}
getNumber <T extends number, U> (name: string, fallback?: ()=>T|U): T|U {
getNumber (name, fallback) {
return this.environment.getNumber<T, U>(name, fallback)
}
}

export class Environment {
constructor (
/** Current working directory. */
readonly cwd: string = '',
/** Environment variables */
readonly vars: typeof process.env = {},
/** Tag to identify the environment (e.g. timestamp) */
readonly tag: string|undefined = String(+new Date())
) {
constructor (cwd, vars = {}, tag = String(+new Date())) {
Object.defineProperty(this, 'vars', { configurable: true, enumerable: false })
}

get [Symbol.toStringTag]() { return this.tag }

getFlag <T extends boolean, U> (name: string, fallback?: ()=>T|U): T|U {
getFlag (name, fallback) {
if (name in this.vars) {
const value = (this.vars[name]??'').trim()
return !Environment.FALSE.includes(value) as T
return !Environment.FALSE.includes(value)
}
if (fallback) return fallback()
throw new ConfigError.Required(name, 'boolean')
}

getString <T extends string, U> (name: string, fallback?: ()=>T|U): T|U {
if (name in this.vars) return String(this.vars[name] as string) as T
getString (name, fallback) {
if (name in this.vars) return String(this.vars[name])
if (fallback) return fallback()
throw new ConfigError.Required(name, 'string')
}

getNumber <T extends number, U> (name: string, fallback?: ()=>T|U): T|U {
getNumber (name, fallback) {
if (name in this.vars) {
const value = (this.vars[name]??'').trim()
if (value === '') {
Expand All @@ -63,7 +56,7 @@ export class Environment {
}
const number = Number(value)
if (isNaN(number)) throw new ConfigError.Required(name, 'number')
return number as T
return number
} else if (fallback) {
return fallback()
} else {
Expand Down
6 changes: 3 additions & 3 deletions conf/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "@hackbg/conf",
"version": "3.1.1",
"main": "conf.ts",
"version": "3.1.2",
"main": "conf.mjs",
"type": "module",
"homepage": "https://github.com/hackbg/toolbox",
"bugs": "https://github.com/hackbg/toolbox/issues",
"repository": {"type": "git", "url": "https://github.com/hackbg/toolbox", "directory": "conf"},
"license": "MIT",
"description": "Command runner.",
"files": [ "README.md", "*.ts" ],
"files": [ "README.md", "*.ts", "*.mjs" ],
"scripts": {
"ubik": "npm run check && ubik",
"check": "tsc --noEmit",
Expand Down

0 comments on commit 9148c31

Please sign in to comment.