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

projects-config

v0.0.5

Published

Loading configs for projects based on environment variable

Downloads

4

Readme

node-projects-config NPM version

Loading configs for projects based on environment variable

Install with npm

npm install projects-config

Usage

F. e. we have two projects: main, admin and two types of environments: dev, production

configs/                       * local configs (not in the repository)
 ├──admin/
 │   └──production.json        * {secretKey: 'YYYY'}
 │
projects/
 ├──admin/
 │   ├──config/                * public config
 │   │   └──default.json       * { resources: {adminApi: '//admin.mysite.com/api'} }
 │   │
 │   └──otherFiles
 │
 └──main/
      ├──config/               * public config
      │   ├──default.json      * { resources: {geoApi: '//maps.googleapis.com/maps/api/js'} }
      │   ├──dev.json          * { resources: {api: '//dev.mysite.com/api'} }
      │   └──production.json   * { resources: {api: '//mysite.com/api'} }
      │
      └──otherFiles
var configs = require('projects-config');
process.env.NODE_ENV = 'production';

configs.load('projects/**/config', 'configs');

console.log(configs);

//log:
//{
//    admin: {
//       resources: {
//           api: '//mysite.com/api',
//           geoApi: '//maps.googleapis.com/maps/api/js',
//           adminApi: '//admin.mysite.com/api',
//       },
//       secretKey: 'YYYY'
//   },
//   main: {
//      resources: {
//          api: '//mysite.com/api',
//          geoApi: '//maps.googleapis.com/maps/api/js'
//      }
//  }
//}

Features

Plugin provides json, json5, hjson, toml, yaml, cson, properties file formats. See node-config

API

configs.load([publicPath][, localPath][, params])

Load configs from config's directories

publicPath

Type: String

Default: 'config'

The path pattern to the directory with the public configurations. Plugin throws error if can not find config for current environment

localPath

Type: String

The path pattern to the directory with the local configurations. Plugin doesn't throw error if can not find config for current environment. It will use the default configuration. Local configs merge to public configs. Usually local configs are stored only on the local machine (not in the repository)

params

env

Type: String

Default: process.env.NODE_ENV

Environment

project

Type: String

Default: process.env.PROJECT

Set project name if you need config for one project. Set undefined/false/null or '*' for all projects

configs.load('projects/**/config', {project: 'main'});

console.log(configs);

//log:
//{
//   main: {
//      resources: {
//          api: '//mysite.com/api',
//          geoApi: '//maps.googleapis.com/maps/api/js'
//      }
//  }
//}
defaultFileName

Type: String

Default: 'default'

File name (without extention) of default config

defaults

Type: Object/Function

Default: function(env, projectName) { return {env: env, project: projectName}; };

Sets default structure for each config. You can use this as link on config of current project

function setDefaultConfig(env, projectName) {
    var public = this.public || {};
    public.project = projectName; //add projectName to `public` in result configs
    
    return {
        project: projectName
        public: public
        private: {
            public: public //copy `public` to `private`
        }
    };
}

configs.stream([params])

Create projects config stream

//configs:
//{
//    project1: { public: {resources: 'resource1'} },
//    project2: { public: {resources: 'resource2'} }
//}

configs.stream({section: 'public.resources'})
    .pipe(gulp.dest(compiledPath));
//config.js
//{
//    project1: 'resource1',
//    project2: 'resource2'
//}

params

name

Type: String

Default: config.js

File name

section

Type: String

Part of config which will be used for forming of config file

stringifySpace

Type: Number

Default: 4

Number of whitespaces of JSON.stringify

configs.forEach([section,] callback)

Executes a provided function once per project. If iterated part of configs is an array it will be provided function once per array element

//configs:
//{
//    admin: {
//        webserver: {port: 7001}
//    },
//    main: {
//        webserver: [
//            {port: 7002},
//            {port: 7003}
//        ]
//    }
//}

configs.forEach('webserver', function(config, projectName) {
    console.log(projectName, config);
})

//log:
//admin {port: 7001}
//main {port: 7002}
//main {port: 7003}

params

section

Type: String

Part of config which will be used for forming of config file

callback

Type: Function

Function to execute for each element, taking two arguments:

config project config or section of project config

projectName project name

returns

Type: Stream

Total stream composed of streams which were returned in callbacks

configs.reduce(callback)

Applies a function against an accumulator and each project config (from left-to-right) to reduce it to a single stream. It is wrapper of _.reduce

configs.reduceRight(callback)

Applies a function against an accumulator and each project config (from left-to-right) to reduce it to a single stream. It is wrapper of _.reduceRight

License

© Oleg Istomin 2015. Released under the MIT license