weare-npm-assets
v0.1.6
Published
Expose NPM modules assets folder.
Downloads
3
Readme
Repository https://npm.dev.studioweare.com
WE_ARE NPM Assets
Expose NPM modules assets folder.
Installation
$ npm install weare-npm-assets
Usage
Say we're in a app
named module that have lodash
as a dependency.
var express = require('express');
var app = express();
var npmAssets = require('weare-npm-assets')();
var lodashScriptPath = npmAssets.getFilePath(require, 'lodash');
// -> /app/modules/lodash/index.js
var myAppScriptPath = npmAssets.getFilePath(require, null, 'javascripts/app.js', 'src');
// -> /app/src/javascripts/app.js
npmAssets.expressServe(app);
// Will create 2 virtual paths:
// "/app/modules/lodash" real path -> "/{root-of-the-app}/node_modules/lodash"
// "/app/src" real path -> "/{root-of-the-app}/src"
app.use('/', function (req, res) {
res.send(
'<script src="'+lodashScriptPath+'" />' +
'<script src="'+myAppScriptPath+'" />'
);
});
Debug
To see the virtual paths generated by "getFilePath" and the static paths (virtual/hard) that are created, you can setup the environment variable DEBUG
to npm-assets
when starting your app:
$ DEBUG=npm-assets node bin/www
Function references
Most useful:
- getFilePath(cRequire, dependency, file, relPath)
- getFileHardPath(cRequire, dependency, file)
- expressServe(app)
Expert mode:
- addStaticPath (cRequire, dependency, relVirtualPath, relHardPath, mainRelPath)
- getMainFileRelPath(cRequire, dependency)
- getFolderPath(cRequire, dependency, mainRelPath)
getFilePath(cRequire, dependency, file, relPath)
/**
* Get the virtual path of a file contain in a module or one of his dependency.
*
* By passing the reference of the "require" function, the contextual dependency
* will be processed by "require" to retrieve the right module folder.
*
* This will create a public static path that expose the entire module or one
* of his subfolder.
*
* Ex: Module X require module A and B. Module A can be found at the same level
* that module X but module B have a specific version so it's found in X/node_modules/B.
*
* @param Function cRequire
* The contextual "require" function.
* @param String dependency
* (optionnal) The name of the main module dependency. When used, the final
* path of the file looks like this:
* /{main-module}/modules/{dependency}/{file}
* @param String file
* (optionnal) The module relative file path. By default, this is the main
* module's file.
* @param String relPath
* (optionnal) The relative folder path in which to get the file. Useful for
* not exposing the entire module folder, see this example:
* getFilePath(require, null, 'src/javascripts/app.js')
* Will expose the folder of the entire module (/).
* getFilePath(require, null, 'javascripts/app.js', 'src')
* Will only expose the "src" folder of the module (/src).
* For non-weird results, don't use if "file" is undefined.
*
* @return String
* The virtual path of the file.
*/
getFileHardPath(cRequire, dependency, file)
/**
* Get the hardpath of a file contain in a module or one of his dependency.
*
* @param Function cRequire
* The contextual "require" function.
* @param String dependency
* (optionnal) The name of the main module dependency. If it's undefined, the
* file will be search in the main module.
* @param String file
* (optionnal) The module relative file path. By default, this is the main
* module's file.
*
* @return String
* The hardpath of the file.
*/
expressServe(app)
/**
* Serve all the static path with Express.
*
* @param Function express
* Express reference.
* @param Object app
* The Express application.
*/
addStaticPath (cRequire, dependency, relVirtualPath, relHardPath, mainRelPath)
/**
* Add a static path for a module or one of it's dependency and return the
* resulting virtual path.
*
* @param Function cRequire
* The main module contextual "require" function.
* @param String dependency
* (optionnal) The name of the main module dependency.
* @param String relVirtualPath
* (optionnal) A path that will be append to the main module/dependency
* virtual path folder. Useful to follow the same hierarchy of the module's
* folders when exposing only a specific folder with the "relHardPath" argument
* ("src" folder for example).
* @param String relHardPath
* (optionnal) A path that will be append to the main module/dependency
* hard path folder. Useful to not expose the entire module folder
* (only "src" folder for example).
* @param String mainRelPath
* (optionnal) The relative path of the main file of the module. Useful for
* NPM module that don't have main file. In that case, point to any file
* inside the module folder. Default: Will try to find the main file in
* "package.json" file, then default to "index.js".
*/
getMainFileRelPath(cRequire, dependency)
/**
* Get the relative path of the main file of the main module/dependency.
*
* @param Function cRequire
* The main module contextual "require" function.
* @param String dependency
* (optionnal) The name of the main module dependency.
*
* @return String
* Return the relative path of the main file of the module.
*/
getFolderPath(cRequire, dependency, mainRelPath)
/**
* Get the absolute folder path of the main module/dependency.
*
* @param Function cRequire
* The main module contextual "require" function.
* @param String dependency
* (optionnal) The name of the main module dependency.
* @param String mainRelPath
* (optionnal) The relative path of the main file of the module. Useful for
* NPM module that don't have main file. In that case, point to any file
* inside the module folder. Default: Will try to find the main file in
* "package.json" file, then default to "index.js".
*
* @return String
* Return the absolute folder path of the module.
*/