Permalink
Browse files

changes for protolus-resource updates

  • Loading branch information...
1 parent 6922941 commit f6a4ecc0670a4a272ded8804b2440da89484f7ba @khrome khrome committed Jan 19, 2013
Showing with 98 additions and 45 deletions.
  1. +1 −1 handler-css.js
  2. +1 −1 package.json
  3. +96 −43 protolus-resource.js
View
@@ -14,7 +14,7 @@ var CSSHandler = new Class({
name : 'link',
attrs : {
},
- pathTarget : 'href'
+ target : 'href'
};
}
});
View
@@ -1,7 +1,7 @@
{
"name": "protolus-resource",
"homepage": "https://github.com/Protolus/protolus-resource",
- "version": "0.0.2-alpha",
+ "version": "0.0.3-alpha",
"main": "protolus-resource.js",
"description": "a CommonJS based server-side resource delivery mechanism with client side shim",
"keywords": [
View
@@ -37,7 +37,7 @@ Client.require = function(moduleName, callback){
var lines = Protolus.scripts[moduleName].split("\\n");
ex.lineText = lines[ex.line];
}
- console.log('it\'s a fucking error!', ex);
+ console.log('ERROR', ex);
}
Protolus.loaded[moduleName] = module.exports || exports;
if(callback) callback(Protolus.loaded[moduleName]);
@@ -89,12 +89,18 @@ var Resource = new Class({
].join("\n")]);
}else callback([]);
};
+ this.removeJob();
if(this.options.onLoad) this.options.onLoad(this);
}else{
- if(!this.manifest.autogenerated) require.scan.full(this.manifest, fn.bind(function(data){
- this.removeJob();
- if(this.options.onLoad) this.options.onLoad(this);
- }, this));
+ if(!this.manifest.autogenerated){
+ require.scan.full(this.manifest, fn.bind(function(data){
+ this.removeJob();
+ if(this.options.onLoad) this.options.onLoad(this);
+ }, this));
+ }else{
+ this.removeJob();
+ if(this.options.onLoad) this.options.onLoad(this);
+ }
}
this.registry.register(this.options.name, this);
},
@@ -113,17 +119,18 @@ var Resource = new Class({
delete fileType;
}
this.whenReady(fn.bind(function(){
- var dependencies = this.manifest.resources.slice(0);
- if(fileType) dependencies = array.filter(dependencies, Filters.fileTypeCaselessLower(fileType));
- callback(dependencies);
+ var resources = this.manifest.resources || [];
+ var files = resources.slice(0);
+ if(fileType) dependencies = array.filter(files, Filters.fileTypeCaselessLower(fileType));
+ callback(files);
}, this));
},
files : function(fileType, callback){
var handler = Resource.handlers[fileType];
this.fileNames(fileType, fn.bind(function(fileNames){
var files = [];
array.forEachEmission(fileNames, fn.bind(function(fileName, index, returnFn){
- var path = (this.manifest.base_directory+'/'+this.options.name+'/'+fileName).replace('/./', '/');
+ var path = (this.manifest.base_directory+'/'+this.manifest.name+'/'+fileName).replace('/./', '/');
handler.load(path, fn.bind(function(data){
files.push(data);
returnFn();
@@ -179,6 +186,7 @@ Resource.Handler = new Class({
});
Resource.import = function(name, registry, callback, isDependency){
+ //console.log('imp', arguments, (new Error()).stack);
if(type(registry) == 'function' && !callback){
callback = registry;
delete registry;
@@ -196,20 +204,21 @@ Resource.import = function(name, registry, callback, isDependency){
});
};
if(!(resource = registry.require(name))){
+ //if(!callback) console.log((new Error()).stack)
new Resource(name, function(resource){
if(!registry.require(name)){
registry.register(name, resource);
if(resource.dependencies) handleDependencies(resource.dependencies, function(){
callback(resource);
});
- else callback(resource);
- }else callback(registry.require(name));
+ else if(callback) callback(resource);
+ }else if(callback) callback(registry.require(name));
})
}else{
if(resource.dependencies) handleDependencies(resource.dependencies, function(){
- callback(resource);
+ if(callback) callback(resource);
});
- else callback(resource);
+ else if(callback) callback(resource);
}
};
@@ -250,28 +259,29 @@ var assembleAttrs = function(attrs, target, value){
prime.each(attrs, function(value, key){
attributes.push(key+'="'+value+'"');
});
- attributes.join(' ');
+ return attributes.join(' ');
};
Resource.head = function(options, callback){ //get head
if(!options.registry) options.registry = Resource.registry;
var compact = (options.compact === false?'':'/compact');
var dependencies = (options.dependencies === false?'':'/dependencies');
var handler = function(resources){
- var tags = [];
+ if(resources.length == 0) callback([]); //we may not have included any
+ var tags = ['<script src="/core/"></script>'];
if(options.inline){
//todo
+ throw('inline not yet implemented.');
}else{
+ var types = prime.keys(Resource.handlers);
if(options.combined === false){
- var types = prime.keys(Resource.handlers);
array.forEach(resources, function(resource){
array.forEach(types, function(type){
var tagInfo = Resource.handlers[type].tagProfile();
tags.push('<'+tagInfo.name+' '+assembleAttrs(tagInfo.attrs, tagInfo.target, compact+dependencies+'/'+resource.options.name)+'></'+tagInfo.name+'>');
});
});
}else{
- var types = prime.keys(Resource.handlers);
array.forEach(types, function(type){
var tagInfo = Resource.handlers[type].tagProfile();
var resList = []
@@ -281,42 +291,85 @@ Resource.head = function(options, callback){ //get head
tags.push('<'+tagInfo.name+' '+assembleAttrs(tagInfo.attrs, tagInfo.target, '/'+type+compact+dependencies+'/'+resList.join('.'))+'></'+tagInfo.name+'>');
});
}
+ if(callback) callback(tags);
}
};
- if(options.dependencies === false && options.explicitDependencies)
- Resource.explicit(options.registry, (options.excludes || []), handler);
- else Resource.includes(options.registry, (options.excludes || []), handler);
+ var results;
+ if(options.dependencies === false && options.explicitDependencies){
+ results = Resource.explicit(options.registry, (options.excludes || []), handler);
+ }else{
+ results = Resource.includes(options.registry, (options.excludes || []), handler);
+ }
}
Resource.handle = function(request, response, passthru){
var url = require('url');
var uri = url.parse(request.url, true);
var value;
var wasResourceRequest = false;
- for(var key in Resource.handlers){
+ var optionNames = ['dependencies', 'compact'];
+ var handlePath = function(path, resources, options){
+ if(path[0] === '/') path = path.substring(1);
+ options.type = path.substring(0, path.indexOf('/'));
+ path = path.substring(path.indexOf('/')+1);
+ while(array.contains(optionNames, path.substring(0, path.indexOf('/')))){
+ var opt = path.substring(0, path.indexOf('/'));
+ options[opt] = true;
+ path = path.substring(path.indexOf('/')+1)
+ }
+ var resourceNames = path.split(".");
+ array.forEach(resourceNames, function(resource){
+ resources.push(resource);
+ });
+ }
+ var options = { excludes : [] };
+ var resources = [];
+ if(uri.pathname === '/core' || uri.pathname.indexOf('/core/') === 0){
+ response.writeHead(200);
+ response.end([
+ 'var Protolus = '+(Client.toString())+';',
+ 'Protolus.require = '+(Client.require.toString())+';',
+ 'Protolus.register = '+(Client.register.toString())+';',
+ 'window.onload = Protolus;'
+ ].join("\n"));
+ }
+ if(uri.pathname.indexOf('/main/') === 0){ //commonJS modules need a wrapper
+ handlePath(uri.pathname, resources, options);
+ var registry = new Resource.Registry();
+ var results = [];
+ array.forEachEmission(resources, function(name, index, rtrn){
+ Resource.import(name, registry, function(resource){
+ rtrn();
+ });
+ }, function(){
+ var results = {};
+ var resourceList = (options.dependencies ? registry.resources : registry.explicit);
+ array.forEachEmission(resourceList, function(resourceName, index, rtrn){
+ var resource = registry.require(resourceName);
+ var path = resource.manifest.base_directory+'/'+resource.manifest.name+'/'+resource.manifest.main;
+ var srcAttr = ' //@sourceURL=/node_modules/'+resource.manifest.name+'/'+resource.manifest.main;
+ fs.readFile(path, 'utf8', function(err, body){
+ results[resourceName] = body+'\n'+srcAttr;
+ rtrn();
+ });
+ }, function(){
+ response.writeHead(200);
+ var registrations = [];
+ prime.each(results, function(js, name){
+ var lines = js.split("\n");
+ var text = JSON.stringify(lines).replace( /\\/g, '\\\\').replace(/'/g, "\\'");
+ registrations.push('Protolus.register(\''+name+'\', \''+text+'\')');
+ });
+ response.end(registrations.join("\n"));
+ });
+ });
+ wasResourceRequest = true;
+ }
+ else for(var key in Resource.handlers){
if(wasResourceRequest) return;
if(uri.pathname.indexOf('/'+key+'/') === 0){
- var rem = uri.pathname.substring(('/'+key+'/').length);
- var resources = rem.split(".");
- var results = [];
- var requestedFileType = key;
- var handler = Resource.handlers[requestedFileType];
- var options = {
- compact : false,
- dependencies : false,
- excludes : []
- }
- var parts = uri.pathname.split('/');
- array.forEach(parts, function(part){
- switch(part.toLowerCase()){
- case 'compact':
- options.compact = true;
- break;
- case 'dependencies':
- options.dependencies = true;
- break;
- }
- });
+ handlePath(uri.pathname, resources, options);
+ var handler = Resource.handlers[key];
var registry = new Resource.Registry();
array.forEachEmission(resources, function(name, index, rtrn){
Resource.import(name, registry, function(resource){
@@ -327,7 +380,7 @@ Resource.handle = function(request, response, passthru){
var resourceList = (options.dependencies ? registry.resources : registry.explicit);
array.forEachEmission(resourceList, function(resourceName, index, rtrn){
var resource = registry.require(resourceName);
- resource.files(requestedFileType, function(files, fileNames){
+ resource.files(key, function(files, fileNames){
array.forEach(files, function(file, index){
handler.handle({
body : file,

0 comments on commit f6a4ecc

Please sign in to comment.