Anand Chowdhary
๐Ÿ“ฆ

Cosmic

July 31, 2020
TypeScript
2 stars
0 forks

๐ŸŒŒ Find and load configuration for your app, based on cosmiconfig

AnandChowdhary/cosmicREADME

๐ŸŒŒ Cosmic

Find and load configuration for your app, based on cosmiconfig, with fallback to environment variables.

Node CI Travis CI Coverage Dependencies License Vulnerabilities Based on Node.ts npm type definitions npm package npm downloads Contributors semantic-release

npm

โญ Features

Cosmic, like cosmiconfig, will look for a configuration file:

  • a package.json property
  • a JSON or YAML, extensionless โ€œrc fileโ€
  • an โ€œrc fileโ€ with the extensions .json, .yaml, .yml, or .js.
  • a .config.js CommonJS module

For example, if the app name is โ€œcosmicโ€, these files will be searched:

  • a cosmic property in package.json
  • a .cosmicrc file in JSON or YAML format
  • a .cosmicrc.json file
  • a .cosmicrc.yaml, .cosmicrc.yml, or .cosmicrc.js file
  • a cosmic.config.js file exporting a JS object

Apart from these, it also looks for:

  • a cosmic.yaml, cosmic.yml, or cosmic.js file without โ€œrcโ€
  • Environment variables

๐Ÿ’ก Usage

Install the package from npm:

npm install @anandchowdhary/cosmic

Import and use:

import { cosmic } from "@anandchowdhary/cosmic";

const config = await cosmic("project"); // {}

Use the config function to fetch a value:

import { cosmic, config } from "@anandchowdhary/cosmic";

await cosmic("project");
const environment = config("nodeEnv");

Clear the cache and fetch configuration available:

import { clearCosmicCache } from "@anandchowdhary/cosmic";

clearCosmicCache();

Sync functions are also available:

import { cosmicSync } from "@anandchowdhary/cosmic";

const config = cosmicSync("project"); // {}

๐Ÿ‘ฉโ€๐Ÿ’ป Development

Build TypeScript:

npm run build

Run unit tests and view coverage:

npm run test-without-reporting

๐Ÿ“„ License

MIT ยฉ Anand Chowdhary