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

approot

v0.2.1

Published

A helper class to build file path. Useful in application, to provide path reference to whole app

Downloads

1,128

Readme

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