Skip to content

Instantly share code, notes, and snippets.

@jamesdavidson
Created July 7, 2012 15:47
Show Gist options
  • Save jamesdavidson/3066961 to your computer and use it in GitHub Desktop.
Save jamesdavidson/3066961 to your computer and use it in GitHub Desktop.
Cow-Chap. Versatile Javascript. Compiles web-apps from an assortment of resources. Deploys to CouchDB. More info at http://cow-chap-js.info
/* ---------------------
# NOTICE:
This script can be run in the browser or on the server.
Edit this script before you use it.
I can not stress this enough!
Edit this script and fill it with your own config information!
# INTRODUCTION:
Take a few templates, a few scripts, some images and some stylesheets.
Pass them to Cow-Chap (using NodeJS from the command line or using a
browser-based IDE such as the one at http://cow-chap-js.info/garden/new
Cow-Chap generates a little loader HTML and you write your web-application.
This script uses two popular libraries: tmpvar's jsdom and caolan's async.
The end result is a .json file (with attachments embedded, base64 encoded)
which can then be deployed with ease to any CouchDB.
For more information on server configuration ie pointing a domain name
like myawesomenewapp.com to the server or setting up users/groups check
out cow-chap-js.info/wiki/deployment.
# BOILERPLATE
Cow-Chap is a development tool which does away with the boilerplate.
You can supply your own modules or drop in some classics ready to roll.
Libraries available: Backbone, jQuery, Twitter Bootstrap, Zurb, jQ Mobile,
Sencha*,
Remember, Cow-Chap is not for websites, but for web applications.
# STRUCTURE
1) Load configuration
2) Browser: wait for file input OR other input
Server: readSync from file
3) Browser: create temporary database and upload
: use server replication to send to remote host
Server: use cradle to post (and configure)
--------------------- */
// dependencies
// we should load different dependencies if we're in the browser...
var request = require('request'), // for POSTing to the CouchDB target
fs = require('fs'), // for loading attachments
jsdom = require('jsdom'), // for generating the index.html which loads apps.js
// and all the resources ie img/js/jade/htm etc
async = require('async');
var config = {
environment: 'server',
target: 'http://admin:pass1234@localhost:5984/testing/',
vhost: 'http://my.app.url/'
};
var app = {
_id: '_design/name',
_attachments: {
"build-tool":
{
"content_type":"text/javascript",
"data": fs.readFileSync('cow-chap.js').toString('base64')
}
},
rewrites: ['hello'],
views: {},
validate_doc_update: function(){},
lists: null,
shows: null
};
request // the original and the best, cheers mikeal
.get // first we try to GET the design document (because it might
( // already exist and we would need the _rev value then)
{url:config.target + app._id, json:true},
function(err, res, body)
{
console.log(body);
app._rev = body._rev;
request.post
(
{url:config.target, json:app},
function(err,res,body)
{
console.log(body);
if(!err)console.log('App deployed.');
}
);
}
);
/*
// super secret config variable coming up
// do not share or include with app for obvious reasons!
// might I suggest you add cow-chap.js to your .gitignore
var target = 'http://admin:pass1234@localhost:5984/db';
// declare and type the app's components
var app = {
meta: {},
main: function(window){},
rewrites:[],
shows: {},
views: {},
lists: {},
validate_doc_update: function(doc,req){}
};
app.meta.author = {name:'James Davidson', twitter:'jsdavo', email:'[email protected]'};
app.meta.dev = {repo:'https://gist.github.com/gists/306696'};
app.main = function(window)
{
// do some bits and pieces here
};
app.rewrites.push({from:'/',to:'_show/loader',method:'GET'});
app.rewrites.push({from:'/*',to:'*',method:'GET'});
apps.views.timestamp = { map: function(doc){emit(key,value);}, reduce: function(key,values,rereduce){} };
// submit to server
request.post(target, {data:app,json:true}, function(err,data){});
// now use the _rev and submit all the files as well
// catch the errors and deal appropriately
// ie ask for a password if necessary
*/
@jamesdavidson
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment