Comparing version 0.0.1 to 0.0.2
@@ -7,2 +7,3 @@ var fs = require('fs'); | ||
function escape(str) { | ||
// Escape RegExp special characters. Taken from Prototype.js `RegExp.escape`. | ||
return str.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1'); | ||
@@ -9,0 +10,0 @@ } |
@@ -8,2 +8,3 @@ var fs = require('fs'); | ||
function escape(str) { | ||
// Escape RegExp special characters. Taken from Prototype.js `RegExp.escape`. | ||
return str.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1'); | ||
@@ -10,0 +11,0 @@ } |
var fs = require('fs'); | ||
var path = require('path'); | ||
var util = require('util'); | ||
var Filter = require('./lib/filter'); | ||
var Builder = require('./lib/builder'); | ||
@@ -25,3 +26,4 @@ var SyncBuilder = require('./lib/sync-builder'); | ||
fspkg.Filter = Filter; | ||
fspkg.Builder = Builder; | ||
fspkg.SyncBuilder = SyncBuilder; |
@@ -6,3 +6,3 @@ { | ||
"tags" : ["json", "fs", "file", "fileify", "datauri", "mustache", "commonjs"], | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"homepage": "https://github.com/dandean/fspkg", | ||
@@ -9,0 +9,0 @@ "repository": { |
@@ -1,4 +0,92 @@ | ||
`fspkg`: File System Packager | ||
fspkg: File System Packager | ||
============================= | ||
Read the code! | ||
`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 <code>[modulr-node](https://github.com/tobie/modulr-node)</code> to compile my mustache templates for use in a [backbone.js](http://documentcloud.github.com/backbone/) application. This lets me do this within my backbone views (assuming I've packaged up my "views" directory: | ||
```js | ||
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 sync and async API's. The async API is still in progess, so only the sync is currently documented. | ||
### SyncBuilder ### | ||
```text | ||
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. | ||
Build 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 ### | ||
```text | ||
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. | ||
``` | ||
Install | ||
------- | ||
`npm install fspkg` | ||
TODO | ||
---- | ||
Async Builder and root module function. | ||
License | ||
------- | ||
MIT |
@@ -24,19 +24,17 @@ var fs = require('fs'); | ||
describe('SyncBuilder', function() { | ||
var pkgr, result; | ||
pkgr = new fspkg.SyncBuilder({ format: 'object' }); | ||
result = pkgr.build('./test/scaffold'); | ||
var pkgr1 = new fspkg.SyncBuilder({ format: 'object' }); | ||
var result1 = pkgr1.build('./test/scaffold'); | ||
it('should should include "html", "htm", "mustache" and "txt" files by default', function() { | ||
var count = Object.keys(result).length; | ||
var count = Object.keys(result1).length; | ||
assert.equal(5, count, 'Should have found 5 files but found ' + count); | ||
assert.ok('index.htm' in result); | ||
assert.ok('index.html' in result); | ||
assert.ok('index.mustache' in result); | ||
assert.ok('index.txt' in result); | ||
assert.ok('sub/index.html' in result); | ||
assert.ok('index.htm' in result1); | ||
assert.ok('index.html' in result1); | ||
assert.ok('index.mustache' in result1); | ||
assert.ok('index.txt' in result1); | ||
assert.ok('sub/index.html' in result1); | ||
}); | ||
it('should not include contents of ".git" or "node_modules" directories by default', function() { | ||
var keys = Object.keys(result); | ||
var keys = Object.keys(result1); | ||
assert.ok(keys.indexOf('sub/index.html') > -1, 'could not find key for file in sub-directory: "sub/index.html"'); | ||
@@ -47,5 +45,11 @@ assert.ok(keys.indexOf('.git/index.html') === -1); | ||
pkgr = new fspkg.SyncBuilder({ | ||
var pkgr2 = new fspkg.SyncBuilder({ | ||
format: 'object', | ||
filter: function(path) { | ||
// Include .png files... | ||
if (path && path.match(/\.png$/)) return true; | ||
return fspkg.Filter.Default(path); | ||
}, | ||
// Custom .txt processor | ||
@@ -55,2 +59,7 @@ '.txt': function(path) { | ||
}, | ||
// Custom .png processor | ||
'.png': function(path) { | ||
return fspkg.SyncBuilder.Processor.DataURI(path); | ||
}, | ||
@@ -60,8 +69,14 @@ // Custom .html processor | ||
}); | ||
result = pkgr.build('./test/scaffold'); | ||
var result2 = pkgr2.build('./test/scaffold'); | ||
it('should use extension-specific processors when present in `options` argument', function() { | ||
assert.equal('TEXT FILE', result['index.txt'], 'Custom .txt processor did not execute.'); | ||
assert.ok(result['index.html'].indexOf('data:text/html;charset=UTF-8;base64,') === 0, 'Custom .html processor did not return Data URI of file contents.'); | ||
assert.equal('TEXT FILE', result2['index.txt'], 'Custom .txt processor did not execute.'); | ||
assert.ok(result2['index.html'].indexOf('data:text/html;charset=UTF-8;base64,') === 0, 'Custom .html processor did not return Data URI of file contents.'); | ||
}); | ||
it('should include .png files, encoded as Data URIs', function() { | ||
assert.ok('img/cat-videos.png' in result2, 'PNG was not found in object.'); | ||
assert.ok(result2['img/cat-videos.png'].indexOf('data:image/png;base64,') === 0, 'PNG was not encoded as a Data URI.'); | ||
}); | ||
}); | ||
@@ -68,0 +83,0 @@ |
354309
282
92