fspkg: File System Packager
fspkg
takes a directory or file as input and transforms it into a CommonJS module, JSON string or JavaScript object. fspkg
is great for:
- bundling mustache templates for inclusion in a client-side application.
- encoding images and other assets as Data URIs.
For example, I use fspkg
with modulr-node
to compile my mustache templates for use in a backbone.js application. This lets me do this within my backbone views (assuming I've packaged up my "views" directory:
var MyView = Backbone.View.extend({
...
render: function() {
var template = require('views')['layouts/index.mustache'];
var html = Mustache.to_html(template, this.model);
this.el.html(html);
},
...
});
API
fspkg
exposes both async and sync API's.
Main Function
The module export is a shortcut to instantiating a new Builder
and immediately
its build
method, packaging the file-system at path
.
fspkg(path, [options,] cb)
- path (String): Path to the file to package.
- options (Object): Options to configure the builder.
- cb (Function(e, content)): Callback function which is given the file content.
Example
require('fspkg')('./views', function(e, pkg) {
console.log(pkg);
});
The above example would print a CommonJS module similar to this:
module.exports = {
"index.mustache": "<div>\n Welcome {{username}}!\n</div> ...",
"about/index.mustache": "<h1>This {{expletive}} is awesome</h1> ...",
"about/contact.mustache": "<h1>Contact Us</h1>\n Phone: {{phone}}..."
}
Builder (async)
new Builder([options])
- options (Object): Options to configure the builder.
Creates a new Builder instance. Available Options:
* filter (Function(String path)): A function which filters file paths found in the
directory to be packaged. Should return `true` to include the file, `false` to
exclude it. Defaults to `fspkg.Filter.Default` when `filter` option is not
provided.
* format (String): The format to return from the `build` method:
"module", "json" or "object". Defaults to "module";
Builder#build(path, cb)
- path (String): The root path of the package.
- cb (Function(e, result)): Callback function which is given the result.
Builds the directory or file `path`. `result` is a package in the configured format.
Builder.Processor.*(path, cb) -> String
- path (String): Path to the file to package.
- cb (Function(e, content)): Callback function which is given the file content.
All processors have the same signature: they take a file path and callback function.
The file content is encoded file as a String and passed to `cb` as the 2nd argument.
Builder.Processor.Default(path, cb)
Builder.Processor.Base64(path, cb)
Builder.Processor.DataURI(path, cb)
SyncBuilder
new SyncBuilder([options])
- options (Object): Options to configure the builder.
Creates a new SyncBuilder instance. Available Options:
* filter (Function(String path)): A function which filters file paths found in the
directory to be packaged. Should return `true` to include the file, `false` to
exclude it. Defaults to `fspkg.Filter.Default` when `filter` option is not
provided.
* format (String): The format to return from the `build` method:
"module", "json" or "object". Defaults to "module";
SyncBuilder#build(path) -> ?
- path (String): The root path of the package.
Builds the directory or file `path`, returning a package in the configured format.
SyncBuilder.Processor.*(path) -> String
- path (String): Path to the file to package.
All processors have the same signature: they take a file path and return the
encoded file as a String.
SyncBuilder.Processor.Default(path) -> UTF-8 encoded string.
SyncBuilder.Processor.Base64(path) -> Base64 encoded string.
SyncBuilder.Processor.DataURI(path) -> Base64 encoded Data URI.
Filter
Filter.Default(path) -> Boolean
- path (String): The path to the file.
Returns `true` if the file should be included in the package, otherwise: `false`.
Only .mustache, .html, .htm and .txt files pass this filter, and all files within
".git" and "node_modules" direcotories are excluded.
Example
var fs = require('fs');
var fspkg = require('fspkg');
var builder = new fspkg.SyncBuilder({
filter: function(path) {
if (path && path.match(/\.png$/)) return true;
return fspkg.Filter.Default(path);
},
'.png': function(path) {
return fspkg.SyncBuilder.Processor.DataURI(path);
}
});
var result = builder.build('./stuff');
fs.writeFileSync('./assets.js', result);
console.log(require('./assets.js'));
Install
npm install fspkg
License
MIT