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/applicationvar approot = __dirname;
HINT: It is recommended to expose approot
in global, which will be helpful to share the path across the whole javascript files.
globalapproot = __dirname;
Basic Usage
// return /var/application // return /var/application/models // return /var/application/models/user.js
Consolidate
consolidate
can inject sub-directories automatically, which is still a approot
function.
approot; // Scan then inject all subdirectories automaticallyapproot // return /var/application/models, exists after consolidate is calledapproot // return /var/application/models/user.js
HINT: since consolidate
method return the approot
itself, so you can use in this way:
var approot = __dirname;
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 = rootPath; // return '/' approot = rootPath;approot // return '/models' approot = rootPath;approot // return '/models'approot // return '/routes' approot = rootPath;approot // return '/models'approot // return '/routes'approotroutes // return '/routes/admin'approot // return '/routes/assets'approotassets // return '/routes/assets/js'approotassets // return '/routes/assets/css'approot // return '/routes/config'approotconfig // return '/routes/config/credential'approotconfigcredential// return '/routes/config/credential/secret'
List Children
approot // list all files and folders in current folderapproot // 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 beforevar User = approotmodels; // 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. Useconsolidate
with arguments instead. listChildren
always returns a empty array
lazily-require
Used withWhen used in conjunction of lazily-require to initialize the application environment.
// initEnv.jsvar lazy = ; // suppose __dirname is /var/applicationglobalappRoot = __dirname; // expose the approot to global and consolidate first-layer directories globalconfiguration = ; globalServices = lazy appRoot;globalRoutes = lazy appRoot;globalRecords = lazy appRoot;globalModels = lazy appRoot;globalEntities = lazy appRootentities; // a differnt javascript filevar User = ModelsUser; // var User = require('/var/application/models/User');
License
MIT