Permalink
Browse files

basic version up and running

  • Loading branch information...
0 parents commit 26ae3d8ef7e71be5883efebf397002e1dd4783ad @jairajs89 committed Sep 6, 2012
Showing with 748 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +106 −0 apis.js
  3. +103 −0 client.js
  4. 0 empty.js
  5. +73 −0 flags.js
  6. +23 −0 package.json
  7. +92 −0 run.js
  8. +66 −0 watcher.js
  9. +283 −0 zerver.js
@@ -0,0 +1,2 @@
+.DS_Store
+node_modules
106 apis.js
@@ -0,0 +1,106 @@
+var fs = require('fs');
+
+var CHANGE_TIMEOUT = 1000,
+ ROOT_DIR = process.cwd(),
+ CLIENT_JS = 'client.js',
+ INSERT_NAME = '{{__API_NAME__}}',
+ INSERT_API = '{{__API_OBJ__}}',
+ INSERT_FUNCTIONS = '{{__API_FUNCTIONS__}}';
+
+var setupComplete = false,
+ scriptTemplate = fs.readFileSync(__dirname + '/' + CLIENT_JS) + '',
+ apiNames = [],
+ apis = {},
+ apiScripts = {};
+
+
+
+exports.setup = function (apiDir) {
+ if (setupComplete) {
+ throw Error('apis can be setup only once');
+ }
+ setupComplete = true;
+
+ //TODO: validate apiDir
+
+ try {
+ apiNames = fs.readdirSync('./' + apiDir);
+ }
+ catch (err) {}
+
+ apiNames.forEach(function (fileName) {
+ var len = fileName.length;
+
+ if (fileName.substr(len-3) !== '.js') {
+ return;
+ }
+
+ var apiName = fileName.substr(0, len-3),
+ fileName = ROOT_DIR + '/' + apiDir + '/' + apiName;
+
+ var api = require(fileName);
+ apis[apiName] = api;
+
+ var apiObj = {},
+ apiFunctions = {},
+ file = scriptTemplate;
+
+ setupAPIObj(api, apiObj, apiFunctions);
+
+ file = file.replace(INSERT_NAME , JSON.stringify(apiName) );
+ file = file.replace(INSERT_API , JSON.stringify(apiObj) );
+ file = file.replace(INSERT_FUNCTIONS, JSON.stringify(apiFunctions));
+
+ apiScripts[apiName] = file;
+ });
+};
+
+
+
+exports.get = function (apiName) {
+ return apis[apiName];
+};
+
+
+
+exports.getScript = function (apiName) {
+ return apiScripts[apiName];
+};
+
+
+
+exports.getNames = function () {
+ var names = [];
+
+ for (var apiName in apis) {
+ names.push(apiName);
+ }
+
+ return names;
+};
+
+
+
+function setupAPIObj (api, obj, functions) {
+ var value;
+
+ for (var key in api) {
+ value = api[key];
+
+ switch (typeof value) {
+ case 'function':
+ functions[key] = true;
+ break;
+
+ case 'object':
+ obj[key] = {};
+ functions[key] = {};
+ setupAPIObj(value, obj[key], functions[key]);
+ break;
+
+ default:
+ obj[key] = value;
+ break;
+ }
+ }
+}
103 client.js
@@ -0,0 +1,103 @@
+//TODO: remove dependency on JSON
+
+(function (window) {
+ var API_DIR = 'zerver';
+
+ var apiName = {{__API_NAME__}},
+ apiObj = {{__API_OBJ__}},
+ apiFunctions = {{__API_FUNCTIONS__}};
+
+ window[apiName] = setupFunctions(apiObj, apiFunctions, [ apiName ]);
+
+ function setupFunctions (obj, functions, tree) {
+ var value;
+
+ for (var key in functions) {
+ value = functions[key];
+
+ if (value === true) {
+ obj[key] = function () {
+ var args = Array.prototype.slice.call(arguments),
+ numArgs = args.length,
+ callback = args[numArgs - 1];
+
+ if (typeof callback === 'function') {
+ args.pop();
+ }
+ else {
+ callback = function () {};
+ }
+
+ apiCall(tree.concat(key), args, callback);
+ };
+ }
+
+ else if ((typeof value === 'object') && (typeof obj[key] === 'object')) {
+ obj[key] = setupFunctions(obj[key], value, tree.concat([ key ]));
+ }
+ }
+
+ return obj;
+ }
+
+ function apiCall (tree, args, callback) {
+ var url = '/' + API_DIR,
+ data = JSON.stringify(args),
+ done = false,
+ xhr;
+
+ for (var i=0, len=tree.length; i<len; i++) {
+ url += '/' + encodeURIComponent( tree[i] );
+ }
+
+ if (window.XMLHttpRequest) {
+ xhr = new XMLHttpRequest();
+ }
+ else {
+ xhr = new ActiveXObject('Microsoft.XMLHTTP');
+ }
+
+ xhr.open('POST', url, true);
+ xhr.send(data);
+
+ xhr.onreadystatechange = function () {
+ if (done || (xhr.readyState !== 4)) {
+ return;
+ }
+ done = true;
+
+ var data = [],
+ errorType, errorString;
+
+ if (xhr.status === 200) {
+ try {
+ var response = JSON.parse(xhr.responseText);
+
+ if (response.data) {
+ data = response.data;
+ }
+ else {
+ errorType = 'library';
+ errorString = response.error;
+ }
+ }
+ catch (err) {
+ errorType = 'zerver';
+ errorString = 'failed to parse response';
+ }
+ }
+ else {
+ errorType = 'zerver';
+ errorString = 'http error, ' + xhr.status;
+ }
+
+ var context = {
+ error : !!errorType ,
+ errorType : errorType ,
+ errorString : errorString
+ };
+
+ callback.apply(context, data);
+ };
+ }
+})(window);
No changes.
@@ -0,0 +1,73 @@
+var path = require('path');
+
+
+
+var FLAG_MATCHER = /^(\w+)\=(\w*)$/,
+ FUNC_MATCHER = /^[^\(]*\(([^\)]*)/;
+
+var flagHandlers = {};
+
+
+
+exports.add = function (flag, handler) {
+ flagHandlers[flag] = handler;
+};
+
+
+
+exports.run = function () {
+ process.argv.slice(2).forEach(function (arg) {
+ if (arg.substr(0, 2) === '--') {
+ var match = FLAG_MATCHER.exec( arg.substr(2) );
+
+ if (match) {
+ try {
+ flagHandlers[ match[1] ]( match[2] );
+ }
+ catch (err) {
+ usageError();
+ }
+ }
+ else {
+ usageError();
+ }
+ }
+
+ else if (arg[0] === '-') {
+ Array.prototype.slice.call( arg.substr(1) ).forEach(function (flag) {
+ try {
+ flagHandlers[flag]();
+ }
+ catch (err) {
+ usageError();
+ }
+ });
+ }
+
+ else {
+ usageError();
+ return;
+ }
+ });
+};
+
+
+
+function usageError () {
+ var usage = 'Usage: ' + path.basename( process.argv[1] ),
+ match;
+
+ for (var flag in flagHandlers) {
+ match = FUNC_MATCHER.exec( flagHandlers[flag] );
+
+ if ((flag.length > 1) || (match && match[1])) {
+ usage += ' [--' + flag + '=VALUE]';
+ }
+ else {
+ usage += ' [-' + flag + ']';
+ }
+ }
+
+ console.error(usage);
+ process.exit(1);
+}
@@ -0,0 +1,23 @@
+{
+ "author": "Jairaj Sethi <j@jairaj.org> (http://jairaj.org/)",
+ "name": "zerver",
+ "description": "client-integrated webapp server",
+ "version": "0.0.1",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/jairajs89/zerver.git"
+ },
+ "main": "empty.js",
+ "engines": {
+ "node": "0.8.6"
+ },
+ "dependencies": {
+ "mime": "1" ,
+ "stalker" : "0.0.17" ,
+ "findit" : "0.1.2"
+ },
+ "bin": {
+ "zerver": "run.js"
+ },
+ "license": "MIT"
+}
Oops, something went wrong.

0 comments on commit 26ae3d8

Please sign in to comment.