UNPKG

trinitywatch

Version:

A beta tool for auto refreshing Quickbase code files in Sublime Text 2

265 lines (255 loc) 10.3 kB
var exec = require('child_process').exec; var fs = require('fs'); var watch = require('node-watch'); var prompt = require('prompt'); var $ = require('jQuery'); function convertTrinitywatch() { var fileString = ""; var pageid = []; var pagename = []; var files = []; var pagecount = 1; var prompts = ['realm', 'appid', 'token', 'username', { name: 'password', hidden: true }, 'pagename', 'pageid', 'path']; var defaultResults; fs.readFile('trinitywatch.json', 'utf-8', function(err, data) { if (err) { console.log(err); } defaultResults = JSON.parse(data); $.each(prompts, function(k, v) { tempv = (typeof v === 'object') ? v.name : v; if (defaultResults[tempv]) { prompts = $.grep(prompts, function(value) { return value != v; }); } }); if (prompts.length) { prompt.start(); prompt.get(prompts, function(err, results) { $.qbloader(results); }); } else { $.qbloader({}); } }); $.extend({ qbloader: function(results) { result = $.extend(results, defaultResults); var file = result.path.split(","); $.each(file, function(k, v) { files.push($.trim(result.directory) + $.trim(v)); }); fileString = files.join(" "); //result.path.replace(/,/g, ""); if (result.pagename) { $.each(result.pagename.split(","), function(k, v) { pagename.push($.trim(v)); }); } if (result.pageid) { $.each(result.pageid.split(","), function(k, v) { pageid.push($.trim(v)); }); } if (result.pageid && pageid.length !== file.length) { console.log("pageid length and path length need to be equal"); return false; } if (result.pagename && pagename.length !== file.length) { console.log("pagename length and path length need to be equal"); return false; } $.quickbase.api("API_Authenticate", { options: { realm: result.realm, dbid: 'main', username: result.username, password: result.password, apptoken: result.token }, success: function(response) { result.ticket = $("ticket", response).text(); if (result.pageid || result.pagename) { if (pageid && pageid.length >= 1) { $.each(pageid, function(i, thisPage) { $.getDBPage(result, $.trim(file[i]), { pageid: thisPage }, pageid.length); }); } else if (pagename && pagename.length >= 1) { $.each(pagename, function(i, thisPage) { $.getDBPage(result, $.trim(file[i]), { pagename: thisPage }, pagename.length); }); } } else { $.each(file, function(i, thisFile) { fs.writeFile($.trim(thisFile), $.trim($("pagebody", db_resp).text().replace(/<BR\/>/g, "\n")), function(err) { $.fsWatch(files, result); }); }); exec('open -a Sublime\\ Text\\ 2 ' + fileString, function(error, stdout, stderr) { if (error) { console.log(error); } }); } }, error: function(error) { console.log(error); } }); } }); $.extend({ getDBPage: function(result, file, opts, pagenum) { $.quickbase.api("API_GetDBPage", { options: { realm: result.realm, dbid: result.appid, ticket: result.ticket, pageid: (opts.pageid) ? opts.pageid : "", pagename: (!opts.pageid && opts.pagename) ? opts.pagename : "" }, success: function(db_resp) { fs.writeFile(file, $.trim($("pagebody", db_resp).text().replace(/<BR\/>/g, "\n")), function(err) { if (pagecount === pagenum) { $.fsWatch(files, result); pagecount = 1; } else { pagecount++; } }); exec('open -a Sublime\\ Text\\ 2 ' + fileString, function(error, stdout, stderr) { if (error) { console.log(error); } }); } }); } }); $.extend({ fsWatch: function(files, result) { console.log('TrinityWatch is now spying for file changes at: ' + files.join(" AND ")); watch(files, function(filename) { var curpageid = (pageid.length >=1) ? pageid[files.indexOf(filename)] : ""; var curpagename = (pagename.length >=1) ? pagename[files.indexOf(filename)] : ""; fs.readFile(filename, 'utf-8', function(err, data) { $.quickbase.api("API_AddReplaceDBPage", { options: { realm: result.realm, dbid: result.appid, ticket: result.ticket, pageid: (result.pageid) ? curpageid : "", pagename: (!result.pageid && result.pagename) ? curpagename : "", pagetype: 1, pagebody: data }, success: function(success) { exec('/reload.sh', function(error, stdout, stderr) { if (error) { console.log(error); } }); result.pageid = $("pageID", success).text(); console.log($("errtext", success).text()); }, error: function(error) { console.log(error); } }); }); }); } }); $.extend({ quickbase: { Defaults: { apptoken: '' }, get: function(options) { gDefaults = { dataType: "xml", async: false }; g = $.extend(gDefaults, options); var result = null; if (this.apptoken) { url += "&apptoken=" + this.apptoken; } $.ajax({ url: g.url, type: 'get', dataType: g.dataType, async: g.async, cache: false, success: function(data) { result = data; } }); return result; }, post: function(url, request, params, show) { if (this.Defaults.apptoken) { request += "<apptoken>" + this.Defaults.apptoken + "</apptoken>\n"; } fullrequest = "<qdbapi>\n" + request + "</qdbapi>"; var result = null; if (show) { //console.log(fullrequest); } $.ajax({ url: url, type: "POST", contentType: 'application/xml', dataType: "text", processData: false, data: fullrequest, success: function(data) { params.success(data); }, error: function(error) { params.error(error); } }); }, api: function(api, params) { var show = false; var o = $.extend(this.Defaults, this.variables, params.options); if (o.tablename && !o.dbid) { o['dbid'] = o.tables[o.tablename]; } if (!o.dbid) { return false; } if (api !== "API_Authenticate") { show = true; } url = 'https://' + o.realm + ".quickbase.com/db/" + o.dbid + "?a=" + api; request = ""; $.each(o, function(k, v) { if (k !== "dbid" && k !== "realm") { if (o[k]) { if (typeof o[k] === "object") { $.each(o[k], function(fid, value) { request += "<" + k + " fid='" + value.fid + "'>" + value.value + "</" + k + ">\n"; }); } else { if (k === "pagebody") { request += "<" + k + ">\n<![CDATA[" + o[k] + "\n]]></" + k + ">\n"; } else { request += "<" + k + ">" + o[k] + "</" + k + ">\n"; } } } } }); return this.post(url, request, params, show); } } }); } exports.convert = convertTrinitywatch;