Network Performance Monitor

    approot

    0.2.1 • Public • Published

    approot NPM version Build Status Dependency Status

    An utility to build path based on a base path. Useful in node.js application, such as web app server. Recommend to instantiate an approot in global namespace, to provide an specific reference path across all files in the project

    Install

    Install using npm.

    $ npm install approot
    

    Usage

    Suppose the code is located in /var/application

    // build approot based on __dirname, and __dirname is /var/application
    var approot = require('approot')(__dirname);
     

    HINT: It is recommended to expose approot in global, which will be helpful to share the path across the whole javascript files.

    global.approot = require('approot')(__dirname);

    Basic Usage

    approot()                         // return /var/application
    approot('models')                 // return /var/application/models
    approot('models', 'user.js')      // return /var/application/models/user.js
     

    Consolidate

    consolidate can inject sub-directories automatically, which is still a approot function.

    approot.consolidate();            // Scan then inject all subdirectories automatically
    approot.models()                  // return /var/application/models, exists after consolidate is called
    approot.models('user.js')         // return /var/application/models/user.js
     

    HINT: since consolidate method return the approot itself, so you can use in this way:

    var approot = require('approot')(__dirname).consolidate();

    Consolidate with arguments

    consolidate with arguments will extend generate approot instance as argument regardeless the file system. Due to this nature, it can be used in browser environment.

    var rootPath = '/'
    var approot;
     
    approot = require('approot')(rootPath);
    approot()                         // return '/'
     
    approot = require('approot')(rootPath).consolidate('models');
    approot.models()                  // return '/models'
     
    approot = require('approot')(rootPath).consolidate(['models', 'routes']);
    approot.models()                  // return '/models'
    approot.routes()                  // return '/routes'
     
    approot = require('approot')(rootPath).consolidate({models: true, routes: 'admin', assets: ['js', 'css'], config: { credential: 'secret' }});
    approot.models()                  // return '/models'
    approot.routes()                  // return '/routes'
    approot.routes.admin()            // return '/routes/admin'
    approot.assets()                  // return '/routes/assets'
    approot.assets.js()               // return '/routes/assets/js'
    approot.assets.css()              // return '/routes/assets/css'
    approot.config()                  // return '/routes/config'
    approot.config.credential()       // return '/routes/config/credential'
    approot.config.credential.secret()// return '/routes/config/credential/secret'

    List Children

    approot.listChildren()                  // list all files and folders in current folder
    approot.listChildren('sub', 'grandsub') // list all files and folders in './sub/grandsub'
     

    Relative Require

    require method is a syntactic sugar that make require a little bit easier

    // suppose approot is create somewhere before
    var User = approot.models.require('user'); // Equivalent to "require(approot.models('user'))"

    Browser Support

    approot can be used in bowser with the help of Browserify or Webpack. But there are some limitations due to lack of fs API support.

    When used in browser, approot cannot scan file system automatically, so the API behavior changes in following circumstances:

    • Use consolidate without arguments does not scan the file system automatically, it just return itself. Use consolidate with arguments instead.
    • listChildren always returns a empty array

    Used with lazily-require

    When used in conjunction of lazily-require to initialize the application environment.

    // initEnv.js
    var lazy = require('lazily-require');
     
    // suppose __dirname is /var/application
    global.appRoot = require('approot')(__dirname).consolidate(); // expose the approot to global and consolidate first-layer directories
     
    global.configuration = require(appRoot.config('configuration'));
     
    global.Services = lazy appRoot.services();
    global.Routes = lazy appRoot.routes();
    global.Records = lazy appRoot.records();
    global.Models = lazy appRoot.models();
    global.Entities = lazy appRoot.entities();
     
    // a differnt javascript file
    var User = Models.User; // var User = require('/var/application/models/User');

    License

    MIT

    NPM downloads

    Install

    npm i approot

    DownloadsWeekly Downloads

    54

    Version

    0.2.1

    License

    MIT

    Last publish

    Collaborators

    • timnew