Socket
Socket
Sign inDemoInstall

@zingle/config

Package Overview
Dependencies
0
Maintainers
2
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @zingle/config

read configuration from environment and CLI args


Version published
Weekly downloads
0
Maintainers
2
Created
Weekly downloads
 

Readme

Source

The Zingle node-config project provides a way to configure Node.js apps using the environment and command-line arguments.

Basic Usage

Create Schema

The Schema class is the entry point into the @zingle/config package. Start by creating a new instance.

import {Schema} from "@zingle/config";
const schema = new Schema();

Define Fields

Use the schema to configure fields which can be set. Fields can have one or more flags which control their behavior.

schema.define("output_file");
schema.define("force", Schema.Flag);
schema.define("input_file", Schema.Required);

Read Configuration

Once fields have been defined on the schema, use the .read method to read the configuration from the environment and command-line.

const config = schema.read(process.env, process.argv);

Handle Leftover Arguments

Remaining arguments can be found in the .argv property of the configuration.

if (config.argv.length > 2) {   // first two are "node", and the script name
    throw new Error(`unexpected argument ${config.argv[2]}`);
}

Example

import fs from "fs";
import {Schema} from "@zingle/config";

const schema = new Schema();

schema.define("output_file");
schema.define("force", Schema.Flag);
schema.define("input_file", Schema.Required);

const config = schema.read(process.env, process.argv);
const input = fs.readFileSync(config.input_file);
const output = processInput(input, config.force);

if (config.output_file) {
    fs.writeFileSync(config.output_file, output);
} else {
    console.log(output);
}

FAQs

Last updated on 11 Jan 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc