Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added a readme, yay.

  • Loading branch information...
commit b725da4ec683a04bf4252681d692e18d05369745 1 parent dbc430f
@Qard authored
Showing with 119 additions and 41 deletions.
  1. +91 −1 README.md
  2. +27 −39 lib/index.js
  3. +1 −1  package.json
View
92 README.md
@@ -1 +1,91 @@
-Documentation coming...tomorrow? It's sleeping time now. >.>
+# CRSH your javascript into tiny blocks.
+With crsh, you can combine a whole bunch of javascript (or coffeescript!) files into one tiny block. Or several if you like. Coffeescript is compiled automagically after each change, but not when in production mode. Inversely; the resulting code is uglified, but only when IN production. Line numbers are handy in development.
+
+## Requirements
+* Node.js 0.4+
+
+## Install
+
+ npm install jsonfig
+
+## Usage
+### Middleware Mode
+
+ // The middleware will setup the file watcher
+ // and rebuild the block whenever a file changes
+ app.use(Crsh.middleware(__dirname + '/public/javascripts', {
+ libs: [
+ './jquery.js'
+ , './underscore.js'
+ , './backbone.js'
+ ]
+ }));
+
+ // Now that the middleware is attached, req.crsh becomes available
+ app.use(function (req, res) {
+ // Here's our block object
+ var block = req.crsh.libs;
+
+ // We can get the file of the block like this;
+ var name = block.name;
+
+ // Should look something like "crsh-{md5-of-joined-filenames}.js"
+ console.log(name);
+
+ // We can also get it with a cachebusting string like this;
+ var url = block.getUrl();
+
+ // Same as before but with "?{modification-date}" after it
+ console.log(url);
+
+ // A "local" property is also created for use in views
+ // This is equivalent to block.getUrl()
+ var local = res.local('crsh_libs')
+ });
+
+### Manual Mode
+
+ var crsh_libs = new Crsh(__dirname + '/public/javascripts', [
+ './jquery.js'
+ , './underscore.js'
+ , './backbone.js'
+ ])
+
+ var url = crsh_libs.getUrl()
+
+or
+
+ var crsh_libs = new Crsh(__dirname + '/public/javascripts')
+
+ crsh_libs
+ .add('./jquery.js')
+ .add('./underscore.js')
+ .add('./backbone.js')
+ .compile()
+
+ var url = crsh_libs.getUrl()
+
+## API
+
+#### new Crsh([base_path], [file_paths])
+Constructs a new block, compiles and starts the watcher, if watcher is allowed in the current mode. Accepts a base_path and a list of file paths to add to the block, either can be left out. If base_path is empty Crsh.path will be used, which defaults to process.cwd(). If file_paths is empty, crsh.compile() will not be executed right away; you must first add() file paths, then compile().
+
+#### crsh.add|remove(path)
+Add or remove a file by its path from the block and watcher. Note that this will not recompile the block. In this particular case crsh.compile() should be called manually.
+
+#### crsh.compile(callback)
+Forces the current block to compile. This is called automatically when a new block is constructed with a file_paths list or the watcher is enable and a change has occured. When chaining, this is called manually instead.
+
+#### Crsh.middleware([base_path], block_defs)
+For each item in block_defs, constructs a new Crsh instance and exposes it in req.crsh[name]. Also creates a "crsh_{name}" properties in view locals.
+
+---
+
+### Copyright (c) 2011 Stephen Belanger
+#### Licensed under MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
66 lib/index.js
@@ -29,18 +29,27 @@ function Crsh (path, list) {
this.watcher = new Watch(path)
}
- // Populate list
- this.list = []
- list.forEach(function (file) {
- self.add(file)
- })
+ // Populate list, if supplied.
+ if (typeof list !== 'undefined') {
+ this.list = []
+ list.forEach(function (file) {
+ self.add(file)
+ })
+
+ this.compile()
+ }
- this.start()
+ // Run the compiler when a change occurs
+ if (this.watcher) {
+ this.watcher.on('change', function () {
+ self.compile()
+ })
+ }
}
-// Default to a tmp output folder
-Crsh.path = process.cwd() + '/tmp'
+// Default to current working directory
+Crsh.path = process.cwd()
/**
@@ -58,7 +67,7 @@ Crsh.prototype.add = function (f) {
}
this.list.push(f)
- this.watcher.add(f)
+ this.watcher && this.watcher.add(f)
return this
}
@@ -70,48 +79,23 @@ Crsh.prototype.add = function (f) {
* Relative or absolute path to file
*/
Crsh.prototype.remove = function (f) {
+ f = helper.realPath(f, this.path)
var p = this.list.indexOf(f)
if (p > -1) { this.list.splice(p, 1) }
- this.watcher.remove(f)
+ this.watcher && this.watcher.remove(f)
return this
}
/**
- * Start the file watcher
- */
-Crsh.prototype.start = function () {
- var self = this
-
- // Run the first 'change' event right away
- function onChange () {
- self.compile(function (err) {
- if (err) return console.error(err)
- self.lastChange = Date.now()
- })
- }
- onChange()
-
- // The watcher is disabled in production
- if (this.watcher) {
- // Add list of files to watcher
- for (var i = 0; i > this.list.length; i++) {
- this.watcher.add(this.list[i])
- }
-
- // Run the compiler when a change occurs
- this.watcher.on('change', onChange)
- }
-}
-
-
-/**
* Merge and minify all the files
*/
Crsh.prototype.compile = function (cb) {
var self = this
, list = this.list
+ cb || (cb = function () {})
+
// Load all the files into a hash
Files.load(list, function (err, files) {
if (err) cb(err)
@@ -138,7 +122,11 @@ Crsh.prototype.compile = function (cb) {
// Write merged and uglified data to file
var p = path.join(self.path, self.name)
- Files.write(p, data, cb)
+ Files.write(p, data, function (err) {
+ if (err) return cb(err)
+ self.lastChange = Date.now()
+ cb()
+ })
})
}
View
2  package.json
@@ -2,7 +2,7 @@
"author": "Stephen Belanger <admin@stephenbelanger.com> (http://stephenbelanger.com)",
"name": "crsh",
"description": "crsh your javascript into tiny blocks.",
- "version": "0.0.3",
+ "version": "0.0.6",
"repository": {
"url": "git://github.com/Qard/crsh.git"
},
Please sign in to comment.
Something went wrong with that request. Please try again.