npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vcon

v1.6.0

Published

Customizable configuration controls

Downloads

4

Readme

Vcon

Customizable configuration controls.

Basic usage

import vcon from 'vcon';

// Add configuration source from command-line arguments
if (vcon.getArgs().config) {
  vcon.addConfig(vcon.getArgs().config);
}

// Omitting the file extension, it will attempt to load all supported extensions.
vcon.addConfig('./configs/default');
vcon.addConfig('./configs/myConfig.yaml');

// use a config schema similar as json-shema to verify and transform
vcon.setSchema({
  type: 'object',
  properties: {
    app: {
      properties: {
        port: {
          type: 'number',
          default: 3000,
        },
        prefixs: {
          type: 'array',
          required: true,
          items: {
            type: 'string',
          },
        },
      },
    },
  },
});

vcon.load();

console.log('get app.port', vcon.get('app.port'));
console.log('get app.prefixs', vcon.get('app.prefixs'));
console.log('has app.foo', vcon.has('app.foo'));

Configuration Group

Vcon allows add a configuration source to one or more groups sources, and you can decide which groups will be used as available configuration sources when call vcon.load().

If the configuration file does not exist for the specified group, it will try to load the unnamed group.

Anyway, Only the first matching valid configuration file will be used as the configuration source

vcon.addConfig('path/to/project', 'default');
// it's same as above
vcon.addConfig('path/to/project', { group: 'default' });

vcon.addConfig('other/path/project', 'default');
vcon.addConfig('path/to/name', ['development', 'production']);

// unnamed group
vcon.addConfig('path/to/unnamed-group');

// load which group dependends on process.env.NODE_ENV
vcon.load(process.env.NODE_ENV);

// can also pass an array,The default group will be load when no configuration is found for process.env.NODE_ENV group
vcon.load([process.env.NODE_ENV, 'default']);

// try load all
vcon.load();

By default, the name of the group can be loaded as the suffix of the file name.

vcon.addConfig('path/to/project', 'default') has the same effect as the following code:

vcon.addConfig('path/to/project.default');
vcon.addConfig('path/to/project');

How to disable group suffix?

there has two way:

// disable single group
vcon.addConfig(path, { groupSuffix: false });

// disable all
vcon.load({ group: ['group'], groupSuffix: false });

Schema

Vcon implemented some basically features of json-schema for verification.

The supported features table below:

| Field | Type | Description | | -------------------- | ------------------------------------- | ------------------------------------------------ | | type | PropertyType \| Array<PropertyType> | Defines the data type | | properties | Record<string, JSONSchema> | Specifies the object's properties | | required | Array<string> \| boolean | Lists the required properties | | items | JSONSchema | Defines the schema of items | | additionalProperties | boolean | Specifies if additional properties are allowed | | default | SchemaValue | Specifies the default value | | title | string | Provides a title for the schema | | description | string | Describes the purpose of the schema | | maximum | number | Defines the maximum value allowed | | minimum | number | Defines the minimum value allowed | | maxLength | Integer | Specifies the maximum string length | | minLength | Integer | Specifies the minimum string length | | enum | Array<SchemaValue> | Lists the allowable values | | maxItems | Integer | Defines the maximum number of items | | minItems | Integer | Defines the minimum number of items | | uniqueItems | boolean | Indicates if array items must be unique | | pattern | string | Specifies a regular expression pattern | | oneOf | Array | Must be valid against exactly one of subschema | | anyOf | Array | Must match at least one of the specified schemas | | allOf | Array | Must match all of the specified schemas | | not | Array | Must not match the specified schema |

Related type definitions

// string of value type
type PropertyType = 'object' | 'array' | 'string' | 'boolean' | 'number' | 'null' | 'integer' | 'any';

// value type
type SchemaValue = string | number | boolean | SchemaArray | SchemaObject | null;

type SchemaObject = {
  [x: string]: SchemaValue;
};

type SchemaArray = Array<SchemaValue>;

Additional Schema Definition

tranform

When the value is of an unexpected type, tranform will be used to convert the value.

if tranform set is true,will use transformers that may convert the corresponding type will be used.

vcon.setSchema({
  type: 'object',
  properties: {
    port: {
      type: 'integer',
      transform: true,
    },
  },
});

Supported configuration file extensions

Vcon supports the following file extensions by default,You can implement a Parser to support more file extensions,about Parser see next section

  • .json
  • .yaml
  • .yml
  • .json5

Parser

Vcon use Parser to convert the content of the configuration file content.

Here is an example for parse a '.toml' configuration file

import vcon, { VconLoadResult, VconParseMeta, VconParseResult, VconParser } from 'vcon';

class TomlParser implements VconParser {
  parse(loaded, VconLoadResult | undefined, result: VconParseResult, meta: VconParseMeta): void | VconParseResult {
    // Results of other Parser transformations
    result;

    if (meta.ext === '.toml') {
      try {
        return {
          config: toml.parse(loaded.content),
        };
      } catch (e) {
        console.error('Parsing error on line ' + e.line + ', column ' + e.column + ': ' + e.message);
      }
    }
  }
}

vcon.addParser(new TomlParser());
vcon.addExtension('.toml');
// now you can use '.toml' file
vcon.addConfig('path/to/name.toml');