Permalink
Browse files

Merge pull request #4 from kevinkuebler/dev-mode-setting

Dev mode setting
  • Loading branch information...
2 parents c5041e4 + d96793e commit 038a80363056cee979fc7e7f85cffbf0c2510662 @CharlotteGore committed Sep 9, 2012
Showing with 470 additions and 471 deletions.
  1. +2 −3 EXAMPLE/app.js
  2. +2 −2 lib/assembly.js
  3. +1 −1 lib/asset.js
  4. +105 −105 lib/cassette.js
  5. +360 −360 lib/manifest.js
View
5 EXAMPLE/app.js
@@ -5,11 +5,10 @@
var express = require('express')
, routes = require('./routes')
-
- // require cassette...
- , cassette = require('cassette-express')(); // we added an entry to package.json here..
+ , cassette
var app = module.exports = express.createServer();
+var cassette = require('cassette-express')({mode: app.settings.env});
// make the cassette middleware available inside templates..
app.locals( {assets : cassette.middleware()} );
View
4 lib/assembly.js
@@ -25,7 +25,7 @@ var Assembly = base.createChild().addInstanceMethods({
this.length++;
this.merged += '\n' + asset.data;
- var src = path.normalize(this.outputPath + '/' + asset.name);
+ var src = this.outputPath + asset.name;
if( /node_modules/.test(src) ){
@@ -36,7 +36,7 @@ var Assembly = base.createChild().addInstanceMethods({
fs.writeFileSync(this.assetsPath + this.buildPath + '/' + hash + ".js", asset.data);
- src = path.normalize(this.outputPath + this.buildPath + '/' + hash + ".js");
+ src = this.outputPath + this.buildPath + '/' + hash + ".js";
}
View
2 lib/asset.js
@@ -12,7 +12,7 @@ var Asset = base.createChild().addInstanceMethods({
init : function(name, lpath, assets, stat, nodeModules){
this.name = path.normalize(lpath + '/' + name);
-
+ this.webName = lpath.replace('\\', '/') + '/' + name;
this.path = path.normalize(lpath + '/');
assetsPath = assets;
View
210 lib/cassette.js
@@ -1,105 +1,105 @@
-var base = require('base-framework'),
- _ = require('underscore'),
- Manifest = require('./manifest.js');
-
-// some defaults for an Express environment.
-var assetsPath = './public/javascripts/';
-var nodeModulesPath = './node_modules';
-var outputPath = '/javascripts';
-var nodeModules = '/node_modules';
-var buildPath = './public/javascripts/cassette';
-
-
-var cassette = base.createChild().addInstanceMethods({
-
- init : function( config ){
-
- if(config){
-
- if(config.assetsPath){
-
- assetsPath = config.assetsPath;
-
- }
-
- if(config.outputPath){
-
- outputPath = config.outputPath;
-
- }
-
- if(config.buildPath){
-
- buildPath = config.buildPath;
-
- }
-
- if(config.mode){
-
- this.mode = config.mode;
-
- }
-
- if(config.nodeModules){
-
- nodeModules = config.nodeModules;
-
- }
-
-
- if(config.scanNodeModules){
-
- this.scanNodeModules = config.scanNodeModules;
-
- } else {
-
- this.scanNodeModules = false;
-
- }
-
- } else {
-
- this.mode = 'debug';
- this.scanNodeModules = false;
-
- }
-
- this.manifest = Manifest(assetsPath, outputPath, nodeModules, this.scanNodeModules);
- this.assemblies = {};
-
- return this;
-
- },
-
- middleware : function(){
-
- var self = this;
-
- return {
-
- useAsset : function( asset ){
-
- var str;
-
- if(self.mode==='debug'){
-
- str = self.manifest.getAssembly( asset, 'forceCheck' ).getDebugTags( outputPath );
-
- } else {
-
- str = self.manifest.getAssembly( asset ).getTag( outputPath );
-
- }
-
- return str;
-
- }
-
- };
-
- }
-
-});
-
-
-exports = module.exports = cassette;
+var base = require('base-framework'),
+ _ = require('underscore'),
+ Manifest = require('./manifest.js');
+
+// some defaults for an Express environment.
+var assetsPath = './public/javascripts/';
+var nodeModulesPath = './node_modules';
+var outputPath = '/javascripts';
+var nodeModules = '/node_modules';
+var buildPath = './public/javascripts/cassette';
+
+
+var cassette = base.createChild().addInstanceMethods({
+
+ init : function( config ){
+
+ if(config){
+
+ if(config.assetsPath){
+
+ assetsPath = config.assetsPath;
+
+ }
+
+ if(config.outputPath){
+
+ outputPath = config.outputPath;
+
+ }
+
+ if(config.buildPath){
+
+ buildPath = config.buildPath;
+
+ }
+
+ if(config.mode){
+
+ this.mode = config.mode === 'development' ? 'debug' : config.mode;
+
+ }
+
+ if(config.nodeModules){
+
+ nodeModules = config.nodeModules;
+
+ }
+
+
+ if(config.scanNodeModules){
+
+ this.scanNodeModules = config.scanNodeModules;
+
+ } else {
+
+ this.scanNodeModules = false;
+
+ }
+
+ } else {
+
+ this.mode = 'debug';
+ this.scanNodeModules = false;
+
+ }
+
+ this.manifest = Manifest(assetsPath, outputPath, nodeModules, this.scanNodeModules);
+ this.assemblies = {};
+
+ return this;
+
+ },
+
+ middleware : function(){
+
+ var self = this;
+
+ return {
+
+ useAsset : function( asset ){
+
+ var str;
+
+ if(self.mode==='debug'){
+
+ str = self.manifest.getAssembly( asset, 'forceCheck' ).getDebugTags( outputPath );
+
+ } else {
+
+ str = self.manifest.getAssembly( asset ).getTag( outputPath );
+
+ }
+
+ return str;
+
+ }
+
+ };
+
+ }
+
+});
+
+
+exports = module.exports = cassette;
View
720 lib/manifest.js
@@ -1,360 +1,360 @@
-var base = require('base-framework'),
- path = require('path'),
- fs = require('fs'),
- _ = require('underscore'),
-
- Asset = require('./asset.js'),
- Assembly = require('./assembly.js'),
- sorter = require('./sorter.js')
- Manifest;
-
-// var m = require('./lib/manifest.js')(process.env.PWD + '/test/examples');
-// m.createAssembly('/NonCircularFileRefs/a.js')
-
-// Here is the Bundle object.
-var Manifest = base.createChild().addInstanceMethods({
-
- init : function( assetsPath, outputPath, nodeModules, scanNodeModules ){
-
- this.assetsPath = path.normalize(assetsPath);
- this.outputPath = outputPath; // we don't normalize this! Windows style would be bad, mkay.
- this.nodeModules = nodeModules;
- this.buildPath = path.normalize("/cassette");
- this.scanNodeModules = scanNodeModules;
-
- this.reset();
-
- return this;
-
- },
-
- // the the files
- scanDirectory : function( currentPath ){
-
- var files = fs.readdirSync( this.assetsPath + currentPath);
-
- if(!this.bundleIndex[currentPath]){
-
- this.bundleIndex[currentPath] = {
- files : [],
- bundles : [],
- mtime : fs.statSync(this.assetsPath + currentPath).mtime.getTime()
- };
-
- };
-
- _.each(files, function(file){
-
- var currentFile = this.assetsPath + currentPath + '/' + file,
- stat = fs.statSync(currentFile),
- src = '',
- dependency;
-
- if(stat.isFile()){
-
- if(/\.js$/.test(file)){
-
- var asset = Asset(file, currentPath, this.assetsPath, stat, this.nodeModules );
-
- this.assetIndex[ asset.name ] = asset;
- this.bundleIndex[ currentPath ].files.push( asset.name );
-
- }
-
-
- }else if(stat.isDirectory()){
-
- if( path.normalize( currentPath + '/' + file ) !== this.buildPath ){
-
- this.bundleIndex[currentPath].bundles.push( path.normalize(currentPath + '/' + file) );
- this.scanDirectory(path.normalize(currentPath + '/' + file));
-
- }
-
- }
-
-
- }, this);
-
- return this;
-
- },
-
- assetsChanged : function(){
-
- // STUB: go through all the assets/directories, detect changes, refresh if necessary.
-
- return false;
-
- },
-
- convertToSortable : function( key ){
-
- var item = {
- key : key,
- dependsOn : []
-
- };
-
- var single = this.assetIndex[key].fileDependencies();
-
- var filesFromBundles = [];
-
- _.each(this.assetIndex[key].bundleDependencies(), function(bundle){
-
- filesFromBundles = filesFromBundles.concat(this.getFilesFromBundle(bundle));
-
- }, this);
-
- item.dependsOn = _.uniq(single.concat(filesFromBundles));
-
- return item;
-
- },
-
- reset : function(){
- this.assemblies = {};
- this.assetIndex = {};
- this.bundleIndex = {};
-
- var node = path.relative(this.assetsPath, this.nodeModules);
-
- if(this.scanNodeModules){
-
- this.scanDirectory( node );
- }
- this.scanDirectory('');
- },
-
- getAssembly : function( key, forceCheck ){
-
- var dirtyFiles = false;
-
- if(key){
-
- key = path.normalize(key);
-
- }else{
-
- throw new Error('No key specified for asset request');
-
- }
-
- if(this.assemblies[key] && typeof forceCheck==='undefined'){
-
- return this.assemblies[key];
-
- }
-
- if(forceCheck){
-
- _.each(this.assetIndex, function(asset){
-
- var hasChanged = asset.hasChanged();
-
- if(hasChanged){
-
- dirtyFiles = true;
-
- };
-
- });
-
-
-
- if(dirtyFiles===true || (this.fileSystemChanged()===true) ){
- // we invalidate all assemblies and get a fresh scan.
- this.reset();
-
- }
-
- }
-
- var list = [];
-
- _.each( // for each sorted element...
- sorter().sort(this.buildBundle( key ), function(item){
- return {
- key : item.key,
- dependsOn : item.dependsOn.slice(0)
- };
- }),
-
- // push the actual asset into the array...
- function( item ){
-
- list.push(this.assetIndex[item.key]);
-
- },this);
-
- this.assemblies[key] = Assembly(list, {
- outputPath : this.outputPath,
- assetsPath : this.assetsPath,
- buildPath : this.buildPath
- });
-
- return this.assemblies[key];
-
- },
-
- buildBundle : function( key ){
-
- var self = this; // keep a reference to this
-
- var single;
- var initialFiles = [];
-
- // an object to quickly check if we have a file.
- var lookup = {};
-
- // if it's a file...
- if(this.assetIndex[key]){
-
- // get the file requested...
- single = key;
-
- // add it to 'got'
- initialFiles.push(single);
- lookup[key] = this.assetIndex[single];
-
- if(_.isEmpty(this.assetIndex[key].fileDependencies()) && _.isEmpty(this.assetIndex[key].bundleDependencies()) ){
- // early exit: Assembly is a single file without dependences. We're done!
- return [this.convertToSortable(key)];
-
- }
- // else if it's a directory we know about..
- }else if(this.bundleIndex[key]){
-
- // get all the files from the directory requested (and the sub directories)
- initialFiles = this.getFilesFromBundle(key);
-
- _.each(initialFiles, function( file ){
-
- lookup[file] = this.assetIndex[file];
-
- }, this);
-
- // or we throw up.
- }else{
-
- throw new Error('No such asset: No files or directories found matching ' + key);
-
- }
-
- var filesThatNeedFiles = [];
- var finalList = [];
-
- // process 1: converting a collection of file keys to sortable objects (with keys an dependencies)
- _.each(initialFiles, function(file){
-
- filesThatNeedFiles = filesThatNeedFiles.concat(this.convertToSortable(file));
-
- }, this);
-
- // process 2 : find which items actually do need files...
- while(filesThatNeedFiles.length > 0){
-
- // pluck one from the filesThatNeedFiles
- var testItem = filesThatNeedFiles.splice(-1)[0];
-
- _.each(testItem.dependsOn, function( dependency, index){
-
- var done;
-
- if(!lookup[dependency]){
-
- // add the file to filesThatNeedFiles...
- if(this.assetIndex[dependency]){
-
- done = this.convertToSortable(dependency);
- lookup[done.key] = 1;
- filesThatNeedFiles.push( done )
-
- }
-
- }
-
- }, this);
-
- finalList.push(testItem);
-
- }
-
- return finalList;
-
- },
-
- getFilesFromBundle : function( key ){
-
- var self = this;
-
- return (function getList(id){
-
- var files = self.bundleIndex[id].files.slice(0); // get the files
-
- _.each(self.bundleIndex[id].bundles, function(bundle){
-
- files = files.concat( getList( bundle ) );
-
- }, this);
-
- return files;
-
- }(key));
-
- },
-
- fileSystemChanged : function(){
-
- var start = (new Date()).getTime();
- var changesDetected = false;
-
- _.each(this.bundleIndex, function(bundle, index){
-
- try{
-
- var current = fs.statSync(path.normalize(this.assetsPath + '/' + index)).mtime.getTime();
-
- if(current!==bundle.mtime ){
-
- changesDetected = true;
- bundle.mtime = current;
-
- }
-
- }catch(e){
-
- // we've got a missing directory.
- changesDetected = true;
-
- }
-
-
- }, this);
-
-
-
- if(changesDetected){
-
- return true;
-
- }else{
-
- console.log( 'Elapsed: ' + ((new Date()).getTime() - start) );
-
- return false;
-
- }
-
- },
-
- // unit testing helper..
- query : function(){
-
- return this.bundleIndex;
-
- }
-});
-
-exports = module.exports = Manifest;
-
+var base = require('base-framework'),
+ path = require('path'),
+ fs = require('fs'),
+ _ = require('underscore'),
+
+ Asset = require('./asset.js'),
+ Assembly = require('./assembly.js'),
+ sorter = require('./sorter.js')
+ Manifest;
+
+// var m = require('./lib/manifest.js')(process.env.PWD + '/test/examples');
+// m.createAssembly('/NonCircularFileRefs/a.js')
+
+// Here is the Bundle object.
+var Manifest = base.createChild().addInstanceMethods({
+
+ init : function( assetsPath, outputPath, nodeModules, scanNodeModules ){
+
+ this.assetsPath = path.normalize(assetsPath);
+ this.outputPath = outputPath; // we don't normalize this! Windows style would be bad, mkay.
+ this.nodeModules = nodeModules;
+ this.buildPath = "/cassette";
+ this.scanNodeModules = scanNodeModules;
+
+ this.reset();
+
+ return this;
+
+ },
+
+ // the the files
+ scanDirectory : function( currentPath ){
+
+ var files = fs.readdirSync( this.assetsPath + currentPath);
+
+ if(!this.bundleIndex[currentPath]){
+
+ this.bundleIndex[currentPath] = {
+ files : [],
+ bundles : [],
+ mtime : fs.statSync(this.assetsPath + currentPath).mtime.getTime()
+ };
+
+ };
+
+ _.each(files, function(file){
+
+ var currentFile = this.assetsPath + currentPath + '/' + file,
+ stat = fs.statSync(currentFile),
+ src = '',
+ dependency;
+
+ if(stat.isFile()){
+
+ if(/\.js$/.test(file)){
+
+ var asset = Asset(file, currentPath, this.assetsPath, stat, this.nodeModules );
+
+ this.assetIndex[ asset.name ] = asset;
+ this.bundleIndex[ currentPath ].files.push( asset.name );
+
+ }
+
+
+ }else if(stat.isDirectory()){
+
+ if( (currentPath + '/' + file) !== this.buildPath ){
+
+ this.bundleIndex[currentPath].bundles.push( path.normalize(currentPath + '/' + file) );
+ this.scanDirectory(path.normalize(currentPath + '/' + file));
+
+ }
+
+ }
+
+
+ }, this);
+
+ return this;
+
+ },
+
+ assetsChanged : function(){
+
+ // STUB: go through all the assets/directories, detect changes, refresh if necessary.
+
+ return false;
+
+ },
+
+ convertToSortable : function( key ){
+
+ var item = {
+ key : key,
+ dependsOn : []
+
+ };
+
+ var single = this.assetIndex[key].fileDependencies();
+
+ var filesFromBundles = [];
+
+ _.each(this.assetIndex[key].bundleDependencies(), function(bundle){
+
+ filesFromBundles = filesFromBundles.concat(this.getFilesFromBundle(bundle));
+
+ }, this);
+
+ item.dependsOn = _.uniq(single.concat(filesFromBundles));
+
+ return item;
+
+ },
+
+ reset : function(){
+ this.assemblies = {};
+ this.assetIndex = {};
+ this.bundleIndex = {};
+
+ var node = path.relative(this.assetsPath, this.nodeModules);
+
+ if(this.scanNodeModules){
+
+ this.scanDirectory( node );
+ }
+ this.scanDirectory('');
+ },
+
+ getAssembly : function( key, forceCheck ){
+
+ var dirtyFiles = false;
+
+ if(key){
+
+ key = path.normalize(key);
+
+ }else{
+
+ throw new Error('No key specified for asset request');
+
+ }
+
+ if(this.assemblies[key] && typeof forceCheck==='undefined'){
+
+ return this.assemblies[key];
+
+ }
+
+ if(forceCheck){
+
+ _.each(this.assetIndex, function(asset){
+
+ var hasChanged = asset.hasChanged();
+
+ if(hasChanged){
+
+ dirtyFiles = true;
+
+ };
+
+ });
+
+
+
+ if(dirtyFiles===true || (this.fileSystemChanged()===true) ){
+ // we invalidate all assemblies and get a fresh scan.
+ this.reset();
+
+ }
+
+ }
+
+ var list = [];
+
+ _.each( // for each sorted element...
+ sorter().sort(this.buildBundle( key ), function(item){
+ return {
+ key : item.key,
+ dependsOn : item.dependsOn.slice(0)
+ };
+ }),
+
+ // push the actual asset into the array...
+ function( item ){
+
+ list.push(this.assetIndex[item.key]);
+
+ },this);
+
+ this.assemblies[key] = Assembly(list, {
+ outputPath : this.outputPath,
+ assetsPath : this.assetsPath,
+ buildPath : this.buildPath
+ });
+
+ return this.assemblies[key];
+
+ },
+
+ buildBundle : function( key ){
+
+ var self = this; // keep a reference to this
+
+ var single;
+ var initialFiles = [];
+
+ // an object to quickly check if we have a file.
+ var lookup = {};
+
+ // if it's a file...
+ if(this.assetIndex[key]){
+
+ // get the file requested...
+ single = key;
+
+ // add it to 'got'
+ initialFiles.push(single);
+ lookup[key] = this.assetIndex[single];
+
+ if(_.isEmpty(this.assetIndex[key].fileDependencies()) && _.isEmpty(this.assetIndex[key].bundleDependencies()) ){
+ // early exit: Assembly is a single file without dependences. We're done!
+ return [this.convertToSortable(key)];
+
+ }
+ // else if it's a directory we know about..
+ }else if(this.bundleIndex[key]){
+
+ // get all the files from the directory requested (and the sub directories)
+ initialFiles = this.getFilesFromBundle(key);
+
+ _.each(initialFiles, function( file ){
+
+ lookup[file] = this.assetIndex[file];
+
+ }, this);
+
+ // or we throw up.
+ }else{
+
+ throw new Error('No such asset: No files or directories found matching ' + key);
+
+ }
+
+ var filesThatNeedFiles = [];
+ var finalList = [];
+
+ // process 1: converting a collection of file keys to sortable objects (with keys an dependencies)
+ _.each(initialFiles, function(file){
+
+ filesThatNeedFiles = filesThatNeedFiles.concat(this.convertToSortable(file));
+
+ }, this);
+
+ // process 2 : find which items actually do need files...
+ while(filesThatNeedFiles.length > 0){
+
+ // pluck one from the filesThatNeedFiles
+ var testItem = filesThatNeedFiles.splice(-1)[0];
+
+ _.each(testItem.dependsOn, function( dependency, index){
+
+ var done;
+
+ if(!lookup[dependency]){
+
+ // add the file to filesThatNeedFiles...
+ if(this.assetIndex[dependency]){
+
+ done = this.convertToSortable(dependency);
+ lookup[done.key] = 1;
+ filesThatNeedFiles.push( done )
+
+ }
+
+ }
+
+ }, this);
+
+ finalList.push(testItem);
+
+ }
+
+ return finalList;
+
+ },
+
+ getFilesFromBundle : function( key ){
+
+ var self = this;
+
+ return (function getList(id){
+
+ var files = self.bundleIndex[id].files.slice(0); // get the files
+
+ _.each(self.bundleIndex[id].bundles, function(bundle){
+
+ files = files.concat( getList( bundle ) );
+
+ }, this);
+
+ return files;
+
+ }(key));
+
+ },
+
+ fileSystemChanged : function(){
+
+ var start = (new Date()).getTime();
+ var changesDetected = false;
+
+ _.each(this.bundleIndex, function(bundle, index){
+
+ try{
+
+ var current = fs.statSync(path.normalize(this.assetsPath + '/' + index)).mtime.getTime();
+
+ if(current!==bundle.mtime ){
+
+ changesDetected = true;
+ bundle.mtime = current;
+
+ }
+
+ }catch(e){
+
+ // we've got a missing directory.
+ changesDetected = true;
+
+ }
+
+
+ }, this);
+
+
+
+ if(changesDetected){
+
+ return true;
+
+ }else{
+
+ console.log( 'Elapsed: ' + ((new Date()).getTime() - start) );
+
+ return false;
+
+ }
+
+ },
+
+ // unit testing helper..
+ query : function(){
+
+ return this.bundleIndex;
+
+ }
+});
+
+exports = module.exports = Manifest;
+

0 comments on commit 038a803

Please sign in to comment.