diff --git a/.gitignore b/.gitignore index d1d04b1d..1171d6d8 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,6 @@ build # Tmp directory .tmp + +# Packages for testing +test/packages \ No newline at end of file diff --git a/README.md b/README.md index 31ca68c6..52f8fab9 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,6 @@ [![Build Status](https://travis-ci.org/CodeboxIDE/codebox.png?branch=master)](https://travis-ci.org/CodeboxIDE/codebox) [![NPM version](https://badge.fury.io/js/codebox.svg)](http://badge.fury.io/js/codebox) -#### :warning: Instructions are for the not yet published version 1.0.0 - Codebox is a complete and modular Cloud IDE. It can run on any unix-like machine (Linux, Mac OS X). It is an open source component of [codebox.io](https://www.codebox.io) (Cloud IDE as a Service). The IDE can run on your desktop (Linux or Mac), on your server or the cloud. You can use the [codebox.io](https://www.codebox.io) service to host and manage IDE instances. @@ -36,7 +34,7 @@ $ npm install -g codebox And start the IDE from the command line: ``` -$ codebox --root=./myworkspace --open +$ codebox run ./myworkspace --open ``` Use this command to run and open Codebox IDE. By default, Codebox uses GIT to identify you, you can use the option ```--email=john.doe@gmail.com``` to define the email you want to use during GIT operations. @@ -53,21 +51,6 @@ Others comand line options are available and can be list with: ```codebox --help -p, --port [port] HTTP port ``` -#### Developing and testing packages - -Download and build the source code: - -``` -$ git clone https://github.com/CodeboxIDE/codebox.git -$ cd ./codebox -$ npm install . -$ grunt -``` - -Then you can easily link packages for testing by creating a folder that will contains all your packages (each should start with the prefix `package-`), then run the command `grunt link --origin=../mypackages`. This command will create symlinks between all the packages in `../mypackages` and the folder where are stored packages used by codebox. - -Everytime you update the code of your package, simply run `grunt resetPkg --pkg=mypackage` in it and restart codebox. - #### Need help? The IDE's documentation can be found at [help.codebox.io](http://help.codebox.io). Feel free to ask any questions or signal problems by adding issues. diff --git a/bin/codebox.js b/bin/codebox.js index 1f3f6a56..4710a5d6 100755 --- a/bin/codebox.js +++ b/bin/codebox.js @@ -10,67 +10,110 @@ var codebox = require("../lib"); var gitconfig = require('../lib/utils/gitconfig'); +function printError(err) { + console.log(err.stack || err.message || err); + process.exit(1); +} + program .version(pkg.version) -.option('-r, --root [path]', 'Root folder for the workspace, default is current directory', "./") -.option('-t, --templates [list]', 'Configuration templates, separated by commas', "") -.option('-p, --port [port]', 'HTTP port', 3000) -.option('-o, --open', 'Open the IDE in your favorite browser') -.option('-e, --email [email address]', 'Email address to use as a default authentication') -.option('-u, --users [list users]', 'List of coma seperated users and password (formatted as "username:password")'); - - -program.on('--help', function(){ +.on('--help', function(){ console.log(' Examples:'); console.log(''); - console.log(' $ codebox --root=./myfolder'); + console.log(' $ codebox run ./myfolder'); console.log(''); }); -program.parse(process.argv); - -// Parse auth users -var users = !program.users ? {} : _.object(_.map(program.users.split(','), function(x) { - // x === 'username:password' - return x.split(':', 2); -})); +//// Run Codebox +//// +program +.command('run [root]') +.description('run codebox') +.option('-t, --templates [list]', 'Configuration templates, separated by commas', "") +.option('-p, --port [port]', 'HTTP port', 3000) +.option('-o, --open', 'Open the IDE in your favorite browser') +.option('-e, --email [email address]', 'Email address to use as a default authentication') +.option('-u, --users [list users]', 'List of coma seperated users and password (formatted as "username:password")', function (val) { + return _.object(_.map((val || "").split(','), function(x) { + // x === 'username:password' + return x.split(':', 2); + })); +}, {}) +.action(function(root, opts) { + // Generate configration + var options = { + root: path.resolve(process.cwd(), root || "./"), + port: opts.port, + auth: { + users: opts.users + } + }; -// Generate configration -var options = { - root: path.resolve(process.cwd(), program.root), - port: program.port, - auth: { - users: users - } -}; + codebox.start(options) + .then(function() { + if (program.email) return program.email; + // Path to user's .gitconfig file + var configPath = path.join( + process.env.HOME, + '.gitconfig' + ); -codebox.start(options) -.then(function() { - if (program.email) return program.email; + // Codebox git repo: use to identify the user + return gitconfig(configPath) + .get("user") + .get("email") + .fail(function() { + return ""; + }); + }) + .then(function(email) { + var token = opts.users[email] || Math.random().toString(36).substring(7); + var url = "http://localhost:"+options.port; - // Path to user's .gitconfig file - var configPath = path.join( - process.env.HOME, - '.gitconfig' - ); + console.log("\nCodebox is running at", url); - // Codebox git repo: use to identify the user - return gitconfig(configPath) - .get("user") - .get("email") - .fail(function() { - return ""; - }); -}) -.then(function(email) { - var token = users[email] || Math.random().toString(36).substring(7); - var url = "http://localhost:"+program.port; + if (program.open) open(url+"/?email="+email+"&token="+token); + }) + .fail(printError); +}); - console.log("\nCodebox is running at", url); +//// Install packages +//// +program +.command('install') +.description('pre-install packages') +.option('-r, --root [path]', 'Root folder to store packages') +.option('-p, --packages ', 'Comma separated list of packages to install', function (val) { + return _.chain(val.split(",")) + .compact() + .map(function(pkgref) { + var parts = pkgref.split(":"); + var name = _.first(parts); + var url = parts.slice(1).join(":"); + if (!name || !url) throw "Packages need to be formatted as 'name:url'"; - if (program.open) open(url+"/?email="+email+"&token="+token); -}) -.fail(function(err) { - console.log(err.stack || err.message || err); + return [name,url]; + }) + .object() + .value() +}, []) +.action(function(opts) { + codebox.prepare({ + packages: { + root: opts.root? path.resolve(process.cwd(), opts.root) : undefined, + install: opts.packages, + defaults: null + } + }) + .then(function() { + process.exit(0); + }) + .fail(printError); }); + +program.parse(process.argv); + +if (!process.argv.slice(2).length) { + program.outputHelp(); +} diff --git a/editor/collections/packages.js b/editor/collections/packages.js index a356dfd6..f461ceeb 100644 --- a/editor/collections/packages.js +++ b/editor/collections/packages.js @@ -1,5 +1,6 @@ var Q = require("q"); var _ = require("hr.utils"); +var $ = require("jquery"); var Collection = require("hr.collection"); var logger = require("hr.logger")("packages"); @@ -16,38 +17,42 @@ var Packages = Collection.extend({ .then(this.reset.bind(this)); }, - // Load all plugins from backend - loadAll: function() { + // Load all plugins from backend (using bundle) + loadAll: function(bundle) { var that = this; var errors = []; - return this.listAll() - .then(function() { - return that.reduce(function(prev, pkg) { - errors = errors.concat(_.map(pkg.get("errors"), function(e) { - return { - 'name': pkg.get("name"), - 'error': e - }; - })); - - return prev.then(pkg.load.bind(pkg)) - .fail(function(err) { - errors.push({ - 'name': pkg.get("name"), - 'error': err + if (bundle) { + return Q($.getScript("/packages.js")); + } else { + return this.listAll() + .then(function() { + return that.reduce(function(prev, pkg) { + errors = errors.concat(_.map(pkg.get("errors"), function(e) { + return { + 'name': pkg.get("name"), + 'error': e + }; + })); + + return prev.then(pkg.load.bind(pkg)) + .fail(function(err) { + errors.push({ + 'name': pkg.get("name"), + 'error': err + }); + return Q(); }); - return Q(); - }); - }, Q()); - }) - .then(function() { - if (errors.length > 0) { - var e = new Error("Error loading packages"); - e.errors = errors; - return Q.reject(e); - } - }); + }, Q()); + }) + .then(function() { + if (errors.length > 0) { + var e = new Error("Error loading packages"); + e.errors = errors; + return Q.reject(e); + } + }); + } } }); diff --git a/editor/core/application.js b/editor/core/application.js index 08eb893f..8b94f87d 100644 --- a/editor/core/application.js +++ b/editor/core/application.js @@ -1,10 +1,13 @@ var _ = require("hr.utils"); var $ = require("jquery"); var Q = require("q"); - +var logger = require("hr.logger")("app"); var Application = require("hr.app"); var GridView = require("hr.gridview"); +var dialogs = require("../utils/dialogs"); +var workspace = require("./workspace"); + // Define base application var CodeboxApplication = Application.extend({ el: null, @@ -14,16 +17,29 @@ var CodeboxApplication = Application.extend({ routes: {}, initialize: function() { - Application.__super__.initialize.apply(this, arguments); + CodeboxApplication.__super__.initialize.apply(this, arguments); this.grid = new GridView({ columns: 10 }, this); this.grid.$el.addClass("main-grid"); this.grid.appendTo(this); + + // Signal offline + function updateOnlineStatus(event) { + logger.log("connection changed", navigator.onLine); + if (!navigator.onLine) { + dialogs.alert("It looks like you lost your internet connection. The IDE requires an internet connection."); + } else { + dialogs.alert("Your internet connection is up again. Restart your navigator tab to ensure that codebox works perfectly."); + } + } + window.addEventListener('online', updateOnlineStatus); + window.addEventListener('offline', updateOnlineStatus); }, render: function() { + this.head.title(workspace.get('title')); return this.ready(); }, @@ -33,4 +49,4 @@ var CodeboxApplication = Application.extend({ }, }); -module.exports = new CodeboxApplication(); +module.exports = new CodeboxApplication(); diff --git a/editor/core/workspace.js b/editor/core/workspace.js new file mode 100644 index 00000000..6621de79 --- /dev/null +++ b/editor/core/workspace.js @@ -0,0 +1,3 @@ +var Workspace = require("../models/workspace"); + +module.exports = new Workspace(); diff --git a/editor/main.js b/editor/main.js index 461f2560..a173d424 100644 --- a/editor/main.js +++ b/editor/main.js @@ -4,10 +4,15 @@ var Q = require("q"); var logger = require("hr.logger")("app"); +Q.onerror = function (error) { + logger.exception("Uncaught Error:", error); +}; + var app = require("./core/application"); var commands = require("./core/commands"); var packages = require("./core/packages"); var user = require("./core/user"); +var workspace = require("./core/workspace"); var users = require("./core/users"); var settings = require("./core/settings"); var dialogs = require("./utils/dialogs"); @@ -23,6 +28,7 @@ window.codebox = { require: codeboxRequire, app: app, user: user, + workspace: workspace, root: new File(), settings: settings }; @@ -44,12 +50,17 @@ commands.register({ // Start running the applications logger.log("start application"); Q.delay(500) -.then(codebox.user.whoami.bind(codebox.user)) -.then(codebox.root.stat.bind(codebox.root, "./")) -.then(settings.load.bind(settings)) -.then(users.listAll.bind(users)) .then(function() { - return packages.loadAll() + return Q.all([ + codebox.user.whoami(), + codebox.root.stat('./'), + codebox.workspace.about(), + settings.load(), + users.listAll() + ]); +}) +.then(function() { + return packages.loadAll(!codebox.workspace.get('debug')) .fail(function(err) { var message = "

"+err.message+"

"; if (err.errors) { diff --git a/editor/models/command.js b/editor/models/command.js index cc10352f..f1933d81 100644 --- a/editor/models/command.js +++ b/editor/models/command.js @@ -78,7 +78,7 @@ var Command = Model.extend({ return that.get("run").apply(that, [ args || {}, that.collection.context, origin ]); }) .fail(function(err) { - logger.error("Command failed", err); + logger.exception("Command failed", err); }); }, diff --git a/editor/models/file.js b/editor/models/file.js index f302aba9..13babebd 100644 --- a/editor/models/file.js +++ b/editor/models/file.js @@ -1,3 +1,5 @@ +var path = require("path"); +var axios = require("axios"); var Q = require("q"); var _ = require("hr.utils"); var Model = require("hr.model"); @@ -67,7 +69,7 @@ var File = Model.extend({ // Check if a file is a buffer or exists isBuffer: function() { - return this.get("buffer") != null; + return _.isString(this.get("buffer")); }, // Test if a path is child @@ -111,7 +113,6 @@ var File = Model.extend({ var p; - if (this.isBuffer()) p = Q(hash.btoa(this.get("buffer"))); else { p = rpc.execute("fs/read", { @@ -131,17 +132,22 @@ var File = Model.extend({ }, // Write file content - write: function(content) { + write: function(content, opts) { var that = this; + opts = _.defaults(opts || {}, { + base64: false + }); return Q() .then(function() { - if (that.isBuffer()) return Q(that.set("buffer", content)); + if (that.isBuffer()) { + return File.blobToString(content) + .then(function(s) { + that.set("buffer", s); + }); + } - return rpc.execute("fs/write", { - 'path': that.get("path"), - 'content': hash.btoa(content) - }); + return File.writeContent(that.get("path"), content); }) .then(function() { that.trigger("write", content); @@ -188,22 +194,20 @@ var File = Model.extend({ }, // Save file - save: function(content) { + save: function(content, opts) { var that = this; return Q() .then(function() { - if (!that.isBuffer() || !that.options.saveAsFile) return that.write(content); + if (!that.isBuffer() || !that.options.saveAsFile) return that.write(content, opts); return dialogs.prompt("Save as:", that.get("name")) - .then(function(_path) { - return rpc.execute("fs/write", { - 'path': _path, - 'content': hash.btoa(content), + .then(function(filename) { + return File.writeContent(filename, content, { 'override': false }) .then(function() { - return that.stat(_path); + return that.stat(filename); }) .fail(dialogs.error); }); @@ -221,7 +225,7 @@ var File = Model.extend({ buffer: function(name, content, id, options) { var f = new File(options || {}, { 'name': name, - 'buffer': content, + 'buffer': content || "", 'path': "buffer://"+(id || _.uniqueId("tmp")), 'directory': false }); @@ -249,6 +253,73 @@ var File = Model.extend({ .then(function(f) { return new File({}, f); }); + }, + + // Save as + saveAs: function(filename, content, opts) { + var f = File.buffer(filename); + return f.save(content, opts); + }, + + // Convert blob to string + blobToString: function(b) { + var d = Q.defer(); + + if (b instanceof Blob) { + var reader = new window.FileReader(); + reader.onerror = function(err) { + d.reject(err); + } + reader.onload = function() { + d.resolve(reader.result); + }; + reader.readAsText(b); + } else { + d.resolve(b); + } + + return d.promise; + }, + + // Write content to a file (blob, arraybuffer, string) + writeContent: function(filename, content, opts) { + opts = _.defaults(opts || {}, { + base64: false + }); + var useUpload = false; + + return Q() + .then(function() { + if (_.isString(content)) { + if (opts.base64) return content; + + useUpload = (content.length > 1000); + opts.base64 = true; + return hash.btoa(content); + } else { + useUpload = true; + return content; + } + }) + .then(function(_content) { + if (useUpload) { + opts.path = path.dirname(filename); + + var data = new FormData(); + var blob = new Blob([_content]); + + _.each(opts, function(value, key) { + data.append(key, JSON.stringify(value)); + }); + data.append("content", blob, path.basename(filename)); + + return Q(axios.put('/rpc/fs/upload', data)); + } else { + opts.path = filename; + opts.content = _content; + return rpc.execute("fs/write", opts); + } + }); } }); diff --git a/editor/models/package.js b/editor/models/package.js index 3e124308..59d97560 100644 --- a/editor/models/package.js +++ b/editor/models/package.js @@ -4,6 +4,39 @@ var _ = require("hr.utils"); var Model = require("hr.model"); var logger = require("hr.logger")("package"); +function getScript(url, callback) { + var head = document.getElementsByTagName("head")[0]; + var script = document.createElement("script"); + script.src = url; + + // Handle Script loading + { + var done = false; + + // Attach handlers for all browsers + script.onload = script.onreadystatechange = function(){ + if ( !done && (!this.readyState || + this.readyState == "loaded" || this.readyState == "complete") ) { + done = true; + if (callback) + callback(); + + // Handle memory leak in IE + script.onload = script.onreadystatechange = null; + } + }; + + script.onerror = function(err) { + callback(err); + }; + } + + head.appendChild(script); + + // We handle everything using the script element injection + return undefined; +} + var Package = Model.extend({ defaults: { name: null, @@ -30,16 +63,14 @@ var Package = Model.extend({ if (!this.get("browser")) return Q(); logger.log("Load", this.get("name")); - $.getScript(this.url()+"/pkg-build.js") - .done(function(script, textStatus) { - d.resolve(); - }) - .fail(function(jqxhr, settings, exception) { - logger.error("Error loading plugin:", exception.stack || exception.message || exception); - d.reject(exception); + getScript(this.url()+"/pkg-build.js", function(err) { + if (!err) return d.resolve(); + + logger.exception("Error loading plugin:", err); + d.reject(err); }); - return d.promise.timeout(5000, "This addon took to long to load (> 5seconds)"); + return d.promise.timeout(10000, "This addon took to long to load (> 10seconds)"); }, /** diff --git a/editor/models/workspace.js b/editor/models/workspace.js new file mode 100644 index 00000000..ba1483c5 --- /dev/null +++ b/editor/models/workspace.js @@ -0,0 +1,26 @@ +var Q = require("q"); +var _ = require("hr.utils"); +var Model = require("hr.model"); +var logger = require("hr.logger")("workspace"); + +var rpc = require("../core/rpc"); + +var Workspace = Model.extend({ + defaults: { + id: "", + title: "" + }, + + // Identify the workspace + about: function() { + var that = this; + + return rpc.execute("codebox/about") + .then(function(data) { + return that.set(data); + }) + .thenResolve(that); + }, +}); + +module.exports = Workspace; diff --git a/editor/resources/stylesheets/ui/dialogs.less b/editor/resources/stylesheets/ui/dialogs.less index 302eb1cd..00938ba2 100644 --- a/editor/resources/stylesheets/ui/dialogs.less +++ b/editor/resources/stylesheets/ui/dialogs.less @@ -6,12 +6,23 @@ left: 0px; right: 0px; background: rgba(0, 0, 0, 0.4); + overflow-y: auto; + + &.size-large .dialog-wrapper { + width: @dialog-size-large; + margin-left: -@dialog-size-large/2; + } + + &.size-small .dialog-wrapper { + width: @dialog-size-small; + margin-left: -@dialog-size-small/2; + } .dialog-wrapper { position: absolute; z-index: 1001; - top: 10%; + margin: 2% 0px; left: 50%; width: @dialog-size-medium; @@ -19,16 +30,6 @@ background: @color-0-1; - &.size-large { - width: @dialog-size-large; - margin-left: -@dialog-size-large/2; - } - - &.size-small { - width: @dialog-size-small; - margin-left: -@dialog-size-small/2; - } - .dialog-list { input { width: 100%; diff --git a/editor/utils/dialogs.js b/editor/utils/dialogs.js index 47dc812d..aff9513f 100644 --- a/editor/utils/dialogs.js +++ b/editor/utils/dialogs.js @@ -57,6 +57,7 @@ var openAlert = function(text, options) { }); }; var openErrorAlert = function(err) { + console.log("error", err); return openAlert("Error: "+(err.message || err)) .fin(function() { return Q.reject(err); @@ -123,6 +124,7 @@ var openSchema = function(schema, values) { }); }; + module.exports = { open: open, alert: openAlert, diff --git a/editor/utils/hash.js b/editor/utils/hash.js index 833f94f5..bd6c53e8 100644 --- a/editor/utils/hash.js +++ b/editor/utils/hash.js @@ -1,77 +1,72 @@ var crc32table = "00000000 77073096 EE0E612C 990951BA 076DC419 706AF48F E963A535 9E6495A3 0EDB8832 79DCB8A4 E0D5E91E 97D2D988 09B64C2B 7EB17CBD E7B82D07 90BF1D91 1DB71064 6AB020F2 F3B97148 84BE41DE 1ADAD47D 6DDDE4EB F4D4B551 83D385C7 136C9856 646BA8C0 FD62F97A 8A65C9EC 14015C4F 63066CD9 FA0F3D63 8D080DF5 3B6E20C8 4C69105E D56041E4 A2677172 3C03E4D1 4B04D447 D20D85FD A50AB56B 35B5A8FA 42B2986C DBBBC9D6 ACBCF940 32D86CE3 45DF5C75 DCD60DCF ABD13D59 26D930AC 51DE003A C8D75180 BFD06116 21B4F4B5 56B3C423 CFBA9599 B8BDA50F 2802B89E 5F058808 C60CD9B2 B10BE924 2F6F7C87 58684C11 C1611DAB B6662D3D 76DC4190 01DB7106 98D220BC EFD5102A 71B18589 06B6B51F 9FBFE4A5 E8B8D433 7807C9A2 0F00F934 9609A88E E10E9818 7F6A0DBB 086D3D2D 91646C97 E6635C01 6B6B51F4 1C6C6162 856530D8 F262004E 6C0695ED 1B01A57B 8208F4C1 F50FC457 65B0D9C6 12B7E950 8BBEB8EA FCB9887C 62DD1DDF 15DA2D49 8CD37CF3 FBD44C65 4DB26158 3AB551CE A3BC0074 D4BB30E2 4ADFA541 3DD895D7 A4D1C46D D3D6F4FB 4369E96A 346ED9FC AD678846 DA60B8D0 44042D73 33031DE5 AA0A4C5F DD0D7CC9 5005713C 270241AA BE0B1010 C90C2086 5768B525 206F85B3 B966D409 CE61E49F 5EDEF90E 29D9C998 B0D09822 C7D7A8B4 59B33D17 2EB40D81 B7BD5C3B C0BA6CAD EDB88320 9ABFB3B6 03B6E20C 74B1D29A EAD54739 9DD277AF 04DB2615 73DC1683 E3630B12 94643B84 0D6D6A3E 7A6A5AA8 E40ECF0B 9309FF9D 0A00AE27 7D079EB1 F00F9344 8708A3D2 1E01F268 6906C2FE F762575D 806567CB 196C3671 6E6B06E7 FED41B76 89D32BE0 10DA7A5A 67DD4ACC F9B9DF6F 8EBEEFF9 17B7BE43 60B08ED5 D6D6A3E8 A1D1937E 38D8C2C4 4FDFF252 D1BB67F1 A6BC5767 3FB506DD 48B2364B D80D2BDA AF0A1B4C 36034AF6 41047A60 DF60EFC3 A867DF55 316E8EEF 4669BE79 CB61B38C BC66831A 256FD2A0 5268E236 CC0C7795 BB0B4703 220216B9 5505262F C5BA3BBE B2BD0B28 2BB45A92 5CB36A04 C2D7FFA7 B5D0CF31 2CD99E8B 5BDEAE1D 9B64C2B0 EC63F226 756AA39C 026D930A 9C0906A9 EB0E363F 72076785 05005713 95BF4A82 E2B87A14 7BB12BAE 0CB61B38 92D28E9B E5D5BE0D 7CDCEFB7 0BDBDF21 86D3D2D4 F1D4E242 68DDB3F8 1FDA836E 81BE16CD F6B9265B 6FB077E1 18B74777 88085AE6 FF0F6A70 66063BCA 11010B5C 8F659EFF F862AE69 616BFFD3 166CCF45 A00AE278 D70DD2EE 4E048354 3903B3C2 A7672661 D06016F7 4969474D 3E6E77DB AED16A4A D9D65ADC 40DF0B66 37D83BF0 A9BCAE53 DEBB9EC5 47B2CF7F 30B5FFE9 BDBDF21C CABAC28A 53B39330 24B4A3A6 BAD03605 CDD70693 54DE5729 23D967BF B3667A2E C4614AB8 5D681B02 2A6F2B94 B40BBE37 C30C8EA1 5A05DF1B 2D02EF8D"; var utf8Encode = function (string) { - string = string.replace(/\r\n/g,"\n"); - var utftext = ""; - - for (var n = 0; n < string.length; n++) { - - var c = string.charCodeAt(n); - - if (c < 128) { - utftext += String.fromCharCode(c); - } - else if((c > 127) && (c < 2048)) { - utftext += String.fromCharCode((c >> 6) | 192); - utftext += String.fromCharCode((c & 63) | 128); - } - else { - utftext += String.fromCharCode((c >> 12) | 224); - utftext += String.fromCharCode(((c >> 6) & 63) | 128); - utftext += String.fromCharCode((c & 63) | 128); - } + string = string.replace(/\r\n/g,"\n"); + var utftext = ""; + for (var n = 0; n < string.length; n++) { + var c = string.charCodeAt(n); + + if (c < 128) { + utftext += String.fromCharCode(c); + } + else if((c > 127) && (c < 2048)) { + utftext += String.fromCharCode((c >> 6) | 192); + utftext += String.fromCharCode((c & 63) | 128); } + else { + utftext += String.fromCharCode((c >> 12) | 224); + utftext += String.fromCharCode(((c >> 6) & 63) | 128); + utftext += String.fromCharCode((c & 63) | 128); + } + } - return utftext; + return utftext; }; /** * Convert value as 8-bit unsigned integer to 2 digit hexadecimal number. */ -var hex8 = function(val) -{ - var n = val & 0xFF, - str = n.toString(16).toUpperCase() - ; +var hex8 = function(val) { + var n = val & 0xFF, + str = n.toString(16).toUpperCase() + ; - while(str.length < 2) - str = "0" + str; + while(str.length < 2) + str = "0" + str; - return str; + return str; }; /** * Convert value as 16-bit unsigned integer to 4 digit hexadecimal number. */ -var hex16 = function(val) -{ - return hex8(val >> 8) + hex8(val); +var hex16 = function(val) { + return hex8(val >> 8) + hex8(val); }; /** * Convert value as 32-bit unsigned integer to 8 digit hexadecimal number. */ -var hex32 = function(val) -{ - return hex16(val >> 16) + hex16(val); +var hex32 = function(val) { + return hex16(val >> 16) + hex16(val); }; var crc32= function(str) { - str = utf8Encode(str); + str = utf8Encode(str); - var crc = 0; - var x = 0; - var y = 0; + var crc = 0; + var x = 0; + var y = 0; - crc = crc ^ (-1); - for (var i = 0, iTop = str.length; i < iTop; i++) { - y = (crc ^ str.charCodeAt(i)) & 0xFF; - x = "0x" + crc32table.substr(y * 9, 8); - crc = (crc >>> 8) ^ x; - } + crc = crc ^ (-1); + for (var i = 0, iTop = str.length; i < iTop; i++) { + y = (crc ^ str.charCodeAt(i)) & 0xFF; + x = "0x" + crc32table.substr(y * 9, 8); + crc = (crc >>> 8) ^ x; + } - return (crc ^ (-1)).toString(); + return (crc ^ (-1)).toString(); }; /*\ @@ -85,7 +80,6 @@ var crc32= function(str) { /* Array of bytes to base64 string decoding */ function b64ToUint6 (nChr) { - return nChr > 64 && nChr < 91 ? nChr - 65 : nChr > 96 && nChr < 123 ? @@ -98,11 +92,9 @@ function b64ToUint6 (nChr) { 63 : 0; - } function base64DecToArr (sBase64, nBlocksSize) { - var sB64Enc = sBase64.replace(/[^A-Za-z0-9\+\/]/g, ""), nInLen = sB64Enc.length, nOutLen = nBlocksSize ? Math.ceil((nInLen * 3 + 1 >> 2) / nBlocksSize) * nBlocksSize : nInLen * 3 + 1 >> 2, taBytes = new Uint8Array(nOutLen); @@ -118,14 +110,12 @@ function base64DecToArr (sBase64, nBlocksSize) { } } - return taBytes; } /* Base64 string to array encoding */ function uint6ToB64 (nUint6) { - return nUint6 < 26 ? nUint6 + 65 : nUint6 < 52 ? @@ -138,11 +128,9 @@ function uint6ToB64 (nUint6) { 47 : 65; - } function base64EncArr (aBytes) { - var nMod3, sB64Enc = ""; for (var nLen = aBytes.length, nUint24 = 0, nIdx = 0; nIdx < nLen; nIdx++) { @@ -156,7 +144,6 @@ function base64EncArr (aBytes) { } return sB64Enc.replace(/A(?=A$|$)/g, "="); - } /* UTF-8 array to DOMString and vice versa */ @@ -255,5 +242,8 @@ module.exports = { }, 'btoa': function(s) { return base64EncArr(strToUTF8Arr(s)); + }, + 'base64': { + 'encodeArray': base64EncArr } }; diff --git a/editor/utils/upload.js b/editor/utils/upload.js index 8a40d1f9..fb7757ca 100644 --- a/editor/utils/upload.js +++ b/editor/utils/upload.js @@ -154,7 +154,7 @@ var Uploader = Class.extend({ var formData = new FormData(); formData.append(filename, file); _.each(that.options.data, function(v, k) { - formData.append(k, v); + formData.append(k, JSON.stringify(v)); }); progress(0); diff --git a/gulpfile.js b/gulpfile.js index a2c71e1a..59401bf9 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -10,19 +10,35 @@ var rename = require('gulp-rename'); var uglify = require('gulp-uglify'); var stringify = require('stringify'); var merge = require('merge-stream'); +var child_process = require('child_process'); + +var debug = !!process.env.DEBUG; + +function exec(cmd, cb) { + var c = child_process.exec.apply(child_process, arguments); + c.stdout.pipe(process.stdout); + c.stderr.pipe(process.stderr); +} // Compile Javascript gulp.task('scripts', function() { - return gulp.src('editor/main.js') + var out = gulp.src('editor/main.js') .pipe(browserify({ debug: false, transform: ['stringify', 'require-globify'] - })) - //.pipe(uglify()) - .pipe(rename('application.js')) + })); + + if (!debug) out = out.pipe(uglify()) + + return out.pipe(rename('application.js')) .pipe(gulp.dest('./build/static/js')); }); +// Dedupe modules +gulp.task('dedupe', function (cb) { + exec('npm dedupe', cb); +}); + // Copy html gulp.task('html', function() { return gulp.src('editor/index.html') @@ -62,11 +78,57 @@ gulp.task('styles', function() { // Clean output gulp.task('clean', function(cb) { del([ + '.tmp/**', 'build/**', 'packages/*/pkg-build.js' ], cb); }); +// Build client code +gulp.task('build', function(cb) { + runSequence('clean', 'dedupe', ['scripts', 'styles', 'html', 'assets'], cb); +}); + +// Dedupe modules +gulp.task('preinstall-addons', function (cb) { + exec('./bin/codebox.js install --root=./.tmp/packages', { + env: _.extend({}, process.env, { + CODEBOX_DEBUG: debug + }) + }, cb); +}); + +// Copy everything to .tmp +gulp.task('copy-tmp', function() { + return gulp.src([ + // Most files except the ones below + "./**", + + // Ignore gitignore + "!.gitignore", + + // Ignore dev related things + "!./tmp/**", + "!./.git/**", + "!./packages/**", + "!./node_modules/**", + '!./node_modules', + "!./test/**", + '!./test', + + ]) + .pipe(gulp.dest('.tmp')); +}); + +// Publish to NPM +gulp.task('pre-publish', function(cb) { + debug = false; + runSequence('clean', 'build', 'copy-tmp', 'preinstall-addons', cb); +}); +gulp.task('publish', ['pre-publish'], function(cb) { + exec('cd ./.tmp && npm publish', cb); +}); + gulp.task('default', function(cb) { - runSequence('clean', ['scripts', 'styles', 'html', 'assets'], cb); + runSequence('build', cb); }); diff --git a/lib/configs/default.js b/lib/configs/default.js index 04240f26..fc425013 100644 --- a/lib/configs/default.js +++ b/lib/configs/default.js @@ -1,15 +1,22 @@ var _ = require('lodash'); var crc = require('crc'); +var path = require('path'); // Base structure for a configuration module.exports = function(options) { options = _.merge(options, { + // Debug + 'debug': false, + // Port for running the webserver 'port': 3000, // Root folder 'root': process.cwd(), + // Workspace title + 'title': "Codebox", + // Workspace id 'id': null, @@ -18,15 +25,18 @@ module.exports = function(options) { // Events reporting 'reporting': { - 'timeout': 180 * 1e3 + 'timeout': 180 }, // Authentication settings 'auth': { - 'basic': true + // Redirect user to this url for auth + 'redirect': undefined, }, // Hooks + // If value is string: POST to the url + // If function: executed 'hooks': { 'users.auth': function(data) { if (!data.email || !data.token) throw "Need 'token' and 'email' for auth hook"; @@ -45,6 +55,21 @@ module.exports = function(options) { 'email': data.email }; }, + 'events': undefined, + 'settings.get': undefined, + 'settings.set': undefined + }, + + // Packages + 'packages': { + // Path to store all packages for the user + 'root': undefined, + + // Path to default packages + 'defaults': path.resolve(__dirname, "../../packages"), + + // Packages to install when booting + 'install': {} } }, _.defaults); diff --git a/lib/configs/env.js b/lib/configs/env.js new file mode 100644 index 00000000..8828fcb0 --- /dev/null +++ b/lib/configs/env.js @@ -0,0 +1,50 @@ +var _ = require('lodash'); + +var alias = { + 'PORT': 'CODEBOX_PORT' +} + +var parseEnv = function (env, parent, prefix) { + var k, v, envVar, parsedEnv; + + if (!parent) { + parent = {}; + } + + for(k in parent) { + v = parent[k]; + + envVar = prefix? prefix + '_': ''; + envVar += k.toUpperCase(); + + if (_.isObject(v) && !_.isArray(v) && !_.isFunction(v)) { + parseEnv(env, v, envVar); + } + else { + envVar = envVar.replace(/\./g, '__'); + if (envVar in env) { + if (_.isArray(v) && (v.length == 0 || _.isString(v[0]))) { + parent[k] = _.compact(env[envVar].split(",")); + } else if (_.isNumber(v)) { + parent[k] = parseInt(env[envVar]); + } else if (_.isBoolean(v)) { + parent[k] = (env[envVar] == "false"? false : true); + } else { + parent[k] = env[envVar] + } + } + } + } + + return parent; +}; + + +// Extend configuration with environment variables +module.exports = function(options) { + var env = _.clone(process.env); + _.each(alias, function(to, from) { + if (env[from]) env[to] = env[from] || env[to]; + }); + return parseEnv(env, options, 'CODEBOX'); +}; \ No newline at end of file diff --git a/lib/configs/index.js b/lib/configs/index.js index c6f23484..5574c50c 100644 --- a/lib/configs/index.js +++ b/lib/configs/index.js @@ -3,12 +3,13 @@ var Q = require('q'); var TEMPLATES = { 'default': require('./default'), - 'local': require('./local') + 'local': require('./local'), + 'env': require('./env') }; // Generate a complete config from templates module.exports = function(options) { - var templates = _.unique(["default"].concat((options.templates || "local").split(","))); + var templates = _.unique(["default"].concat((options.templates || "local,env").split(","))); return _.reduce(templates, function(prev, template) { return prev.then(function(_options) { diff --git a/lib/configs/local.js b/lib/configs/local.js index 42dbcedc..7da404b8 100644 --- a/lib/configs/local.js +++ b/lib/configs/local.js @@ -6,51 +6,48 @@ var wrench = require('wrench'); var logger = require("../utils/logger")("local"); -var LOCAL_SETTINGS_DIR = path.join( - process.env.HOME, - '.codebox' -); - -var SETTINGS_FILE = path.join(LOCAL_SETTINGS_DIR, 'settings.json') +var LOCAL_SETTINGS_DIR = process.env.CODEBOX_LOCAL_FOLDER || path.join(process.env.HOME,'.codebox') +var SETTINGS_FILE = path.join(LOCAL_SETTINGS_DIR, 'settings.json'); // Base structure for a local workspace // Store the workspace configuration in a file, ... module.exports = function(options) { - options = _.defaults(options, { - - }); - - options.hooks = _.defaults(options.hooks, { - 'settings.get': function(args) { - return Q.nfcall(fs.readFile, SETTINGS_FILE, "utf-8") - .then(JSON.parse) - .fail(_.constant({})) - .then(function(config) { - if (!config[options.id]) config[options.id] = {}; - return config[options.id][args.user] || {}; - }); + options = _.merge(options, { + 'hooks': { + 'settings.get': function(args) { + return Q.nfcall(fs.readFile, SETTINGS_FILE, "utf-8") + .then(JSON.parse) + .fail(_.constant({})) + .then(function(config) { + if (!config[options.id]) config[options.id] = {}; + return config[options.id][args.user] || {}; + }); + }, + + 'settings.set': function(args) { + return Q.nfcall(fs.readFile, SETTINGS_FILE, "utf-8") + .then(JSON.parse) + .fail(_.constant({})) + .then(function(config) { + if (!config[options.id]) config[options.id] = {}; + config[options.id][args.user] = args.settings; + + return Q.nfcall(fs.writeFile, SETTINGS_FILE, JSON.stringify(config)) + .thenResolve(config); + }) + .then(function(config) { + return config[options.id][args.user] || {}; + }); + } }, - - 'settings.set': function(args) { - return Q.nfcall(fs.readFile, SETTINGS_FILE, "utf-8") - .then(JSON.parse) - .fail(_.constant({})) - .then(function(config) { - if (!config[options.id]) config[options.id] = {}; - config[options.id][args.user] = args.settings; - - return Q.nfcall(fs.writeFile, SETTINGS_FILE, JSON.stringify(config)) - .thenResolve(config); - }) - .then(function(config) { - return config[options.id][args.user] || {}; - }); + packages:{ + 'root': path.resolve(LOCAL_SETTINGS_DIR, 'packages') } - }); + }, _.defaults); // Create .codebox folder logger.log("Creating", LOCAL_SETTINGS_DIR); - wrench.mkdirSyncRecursive(LOCAL_SETTINGS_DIR); + wrench.mkdirSyncRecursive(LOCAL_SETTINGS_DIR, 0777); return options; }; diff --git a/lib/events.js b/lib/events.js index 5eb2e64c..fb9ed962 100644 --- a/lib/events.js +++ b/lib/events.js @@ -38,7 +38,7 @@ var init = function(config) { var eventQueue = []; // Send events and empty queue - var sendEvents = _.debounce(function sendEvents() { + var sendEvents = _.debounce(function() { logger.log("report", _.size(eventQueue), "events"); // Hit hook @@ -48,7 +48,7 @@ var init = function(config) { eventQueue = []; }, timeout); - var queueEvent = function queueEvent(eventData) { + var queueEvent = function(eventData) { eventQueue.push(eventData); }; @@ -74,7 +74,7 @@ var init = function(config) { sendEvents(); }); - logger.log("events are ready"); + logger.log("events are ready, reporting is debounced to", (timeout/1000).toFixed(0)+"s"); }; diff --git a/lib/hooks.js b/lib/hooks.js index 2a0cf127..540e739a 100644 --- a/lib/hooks.js +++ b/lib/hooks.js @@ -4,6 +4,7 @@ var request = require('request'); var logger = require("./utils/logger")("hooks"); +var BOXID = null; var HOOKS = {}; var POSTHOOKS = { 'users.auth': function(data) { @@ -36,6 +37,7 @@ var use = function(hook, data) { // Do http requests request.post(handler, { 'body': { + 'id': BOXID, 'data': data, 'hook': hook }, @@ -74,6 +76,7 @@ var use = function(hook, data) { var init = function(options) { logger.log("init hooks"); + BOXID = options.id; HOOKS = options.hooks; SECRET_TOKEN = options.secret; }; diff --git a/lib/index.js b/lib/index.js index 69d1f597..c89e973f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,13 +1,16 @@ var Q = require('q'); var _ = require('lodash'); var path = require('path'); +var os = require('os'); +var uuid = require('uuid'); +var fs = require('fs'); var http = require('http'); var express = require('express'); var bodyParser = require('body-parser'); -var basicAuth = require('basic-auth-connect'); +var basicAuth = require('basic-auth'); var cookieParser = require('cookie-parser'); var session = require('express-session'); -var multipart = require('connect-multiparty'); +var Busboy = require('busboy'); var configs = require('./configs'); var hooks = require('./hooks'); @@ -21,6 +24,14 @@ var logging = require('./utils/logger'); var logger = logging("main"); +var _middleware = function(fn) { + var __middleware; + return function(req, res, next) { + if (!__middleware) __middleware = fn(); + return __middleware.apply(this, arguments); + }; +} + var prepare = function(config) { return Q() .then(_.partial(logging.init, config)) @@ -38,20 +49,64 @@ var prepare = function(config) { .then(_.partial(packages.init, config)) }; -var multipartMiddleware = multipart(); -var bodyParserMiddleware = bodyParser(); - var start = function(config) { var app = express(); var server = http.createServer(app); + // Parse form data app.use(function(req, res, next) { - if (req.method == "PUT") { - multipartMiddleware(req, res, next); - } else { - bodyParserMiddleware(req, res, next); - } + if ( + (req.headers['content-type'] || "").indexOf('multipart/form-data') < 0 + && req.method.toLowerCase() != "put" + ) return next(); + + var files = {}, fields = {}; + + var busboy = new Busboy({ + headers: req.headers + }); + busboy.on('file', function(fieldname, file, filename, encoding, mimetype) { + var saveTo = path.join(os.tmpDir(), uuid.v4()); + file.pipe(fs.createWriteStream(saveTo)); + files[fieldname] = { + filename: filename, + path: saveTo, + mimetype: mimetype + }; + }); + busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated) { + var pval; + + try { + pval = JSON.parse(val); + } catch (e) { + pval = val; + } + + if (fields[fieldname]) { + if (!_.isArray(fields[fieldname])) fields[fieldname] = [fields[fieldname]]; + fields[fieldname].push(pval); + } else { + fields[fieldname] = pval; + } + }); + busboy.on('finish', function() { + req.body = fields; + req._body = true; + req.files = files; + + res.on ('finish', function () { + _.each(req.files, function(file) { + try { fs.unlinkSync(file.path); } catch(e) {} + }); + req.files = {}; + }); + + next(); + }); + req.pipe(busboy); }); + app.use(bodyParser()); app.use(cookieParser()); // Auth @@ -60,6 +115,8 @@ var start = function(config) { resave: false, saveUninitialized: true })); + + // Auth by query strings app.use("/", function(req, res, next) { var args = _.extend({}, req.query, req.body); if (args.email && args.token) { @@ -72,24 +129,32 @@ var start = function(config) { next(); } }); + + // Auth app.use(function(req, res, next) { - var doAuth = basicAuth(function(user, pass, fn){ - users.auth(user, pass) + if (req.session.userId) return next(); + + var auth = basicAuth(req); + + // Do basic auth + if (auth && auth.name && auth.pass) { + users.auth(auth.name, auth.pass, req) .then(function(user) { - fn(null, user) + req.user = user; + next(); }) - .fail(fn); - }); - - if (req.session.userId || !config.auth.basic) return next(); - doAuth(req, res, next); + .fail(next); + } else { + if (config.auth.redirect) { + console.log('no auth, redirect to', config.auth.redirect); + res.redirect(config.auth.redirect); + } else { + res.header('WWW-Authenticate', 'Basic realm="codebox"'); + res.status(401); + res.end(); + } + } }); - - // Static files - app.use('/', express.static(path.resolve(__dirname, '../build'))); - app.use('/packages', express.static(path.resolve(__dirname, '../packages'))); - - // Auth app.use(function(req, res, next) { if (req.user) { req.session.userId = req.user.id; @@ -112,15 +177,29 @@ var start = function(config) { } }); + // Static files + app.use('/', express.static(path.resolve(__dirname, '../build'))); + + + // Download packages + app.use('/packages', _middleware(function() { + return express.static(config.packages.root); + })); + app.get('/packages.js', function(req, res, next) { + return packages.bundle() + .then(function(fp) { + fs.createReadStream(fp).pipe(res); + }) + .fail(next); + }); + // RPC services app.use('/rpc', rpc.router); // Fs direct access - var _fsmiddleware; - app.use('/fs', function(req, res, next) { - if (!_fsmiddleware) _fsmiddleware = express.static(workspace.root()); - return _fsmiddleware.apply(this, arguments); - }); + app.use('/fs', _middleware(function() { + return express.static(workspace.root()); + })); // Error handling app.use(function(req, res, next) { @@ -150,7 +229,7 @@ var start = function(config) { logger.error(err.stack || err); }); - return prepare(config) + return prepare(_.extend(config, { run: true })) .then(_.partial(socket.init, server, config)) .then(function() { logger.log(""); diff --git a/lib/packages.js b/lib/packages.js index 7e0326ee..846bf9ae 100644 --- a/lib/packages.js +++ b/lib/packages.js @@ -1,31 +1,54 @@ var Q = require("q"); var _ = require("lodash"); +var fs = require("fs"); +var os = require("os"); var path = require("path"); +var wrench = require("wrench"); +var tmp = require("tmp"); var Packager = require("pkgm"); var pkg = require("../package.json"); var events = require("./events"); var logger = require("./utils/logger")("packages"); -var context; -var manager = new Packager({ - 'engine': "codebox", - 'version': pkg.version, - 'folder': path.resolve(__dirname, "../packages"), - 'lessInclude': path.resolve(__dirname, "../editor/resources/stylesheets/variables.less") -}); - -manager.on("add", function(pkg) { - events.emit("packages:add", pkg.infos()); -}); -manager.on("remove", function(pkg) { - events.emit("packages:remove", pkg.infos()); -}); -manager.on("log", function(log) { - logger[log.type].apply(logger, log.arguments); -}); - -var init = function() { +var context, manager, _bundle; + +// Remove output if folder or symlink +function cleanFolder(outPath) { + try { + var stat = fs.lstatSync(outPath); + if (stat.isDirectory()) { + wrench.rmdirSyncRecursive(outPath); + } else { + fs.unlinkSync(outPath); + } + } catch (e) { + if (e.code != "ENOENT") throw e; + } +} + + +var init = function(config) { + manager = manager = new Packager({ + 'engine': "codebox", + 'version': pkg.version, + 'folder': config.packages.root, + 'lessInclude': path.resolve(__dirname, "../editor/resources/stylesheets/variables.less"), + 'uglify': !config.debug + }); + + manager.on("add", function(pkg) { + logger.log("add package", pkg.pkg.name); + events.emit("packages:add", pkg.infos()); + }); + manager.on("remove", function(pkg) { + logger.log("remove package", pkg.pkg.name); + events.emit("packages:remove", pkg.infos()); + }); + manager.on("log", function(log) { + logger[log.type].apply(logger, log.arguments); + }); + context = { utils: _, promise: Q, @@ -40,11 +63,55 @@ var init = function() { logger.log("Load and prepare packages ("+_.size(pkg.packageDependencies)+" dependencies)"); return Q() + + // Keep package foler clean (only packages, ...) + .then(function() { + var packages = fs.readdirSync(config.packages.root); + + return _.each(packages, function(iPkg) { + var pkgPath = path.resolve(config.packages.root, iPkg); + + if (!fs.existsSync(path.resolve(pkgPath, "package.json"))) { + logger.warn("remove non-package", pkgPath); + cleanFolder(pkgPath); + } + }); + }) + + .then(function() { + var defaultPackagesRoot = config.packages.defaults; + if (!defaultPackagesRoot || defaultPackagesRoot == config.packages.root) return; + + // Copy default packages to the packages folder + var defaultPackages = fs.readdirSync(defaultPackagesRoot); + + // Create packages folder + wrench.mkdirSyncRecursive(config.packages.root); + + return _.each(defaultPackages, function(defaultPkg) { + var pkgPath = path.resolve(defaultPackagesRoot, defaultPkg); + var outPath = path.resolve(config.packages.root, defaultPkg); + + // Remove output if folder or symlink + cleanFolder(outPath); + + // Create a new symlink + logger.log("symlink default package", defaultPkg); + fs.symlinkSync( + pkgPath, + outPath + ); + }); + }) .then(function() { - return manager.prepare(pkg.packageDependencies); + return manager.prepare(_.extend(pkg.packageDependencies, config.packages.install || {})); }) .then(function() { + if (!config.run) return; return manager.runAll(context); + }) + .then(function() { + return bundle(true); }); }; @@ -53,6 +120,9 @@ var install = function(url) { .then(function(pkg) { return pkg.run(context) .thenResolve(pkg); + }) + .then(function() { + return bundle(true); }); }; @@ -64,10 +134,33 @@ var list = function() { return manager.orderedPackages(); }; +var bundle = function(force) { + var exists = true; + + return Q() + .then(function() { + if (_bundle) return _bundle ; + exists = false; + + return Q.nfcall(tmp.file).get(0); + }) + .then(function(b) { + _bundle = b; + if (exists && force != true) return; + return manager.bundleAll(_bundle ); + }) + .then(function() { + return _bundle ; + }); +}; + module.exports = { init: init, - manager: manager, + manager: function() { + return manager; + }, install: install, uninstall: uninstall, - list: list + list: list, + bundle: bundle }; diff --git a/lib/services/codebox.js b/lib/services/codebox.js index ee6a03c2..1d211a59 100644 --- a/lib/services/codebox.js +++ b/lib/services/codebox.js @@ -2,10 +2,15 @@ var fs = require("fs"); var path = require("path"); var pkg = require("../../package.json"); +var workspace = require('../workspace'); + // About this current version var about = function(args) { return { - 'version': pkg.version + 'id': workspace.config('id'), + 'title': workspace.config('title'), + 'version': pkg.version, + 'debug': workspace.config('debug') }; }; diff --git a/lib/services/fs.js b/lib/services/fs.js index 75f165ce..6f872ef2 100644 --- a/lib/services/fs.js +++ b/lib/services/fs.js @@ -5,6 +5,7 @@ var path = require('path'); var wrench = require('wrench'); var stream = require('stream'); var mime = require('mime'); +var base64Stream = require('base64-stream'); var workspace = require('../workspace'); var base64 = require('../utils/base64'); @@ -118,8 +119,10 @@ var write = function(args) { 'createParent': true }); + var isStream = args.content instanceof stream.Readable; + if (!args.path || args.content == null) throw "Need 'path' and 'content'"; - if (args.base64) args.content = base64.atob(args.content); + if (args.base64 && !isStream) args.content = base64.atob(args.content); return workspace.path(args.path) .then(function(_path) { @@ -132,7 +135,7 @@ var write = function(args) { } // Write stream - if (args.content instanceof stream.Readable) { + if (isStream) { var d = Q.defer(); var writeStream = fs.createWriteStream(_path); @@ -144,7 +147,11 @@ var write = function(args) { d.reject(err); }); - args.content.pipe(writeStream); + var s = args.content; + + if (args.base64) s = s.pipe(base64Stream.decode()); + + s.pipe(writeStream); return d.promise; } else { @@ -217,13 +224,12 @@ var rename = function(args) { // Upload a file var upload = function(args, meta) { args.path = args.path || "."; - return _.reduce(meta.req.files, function(prev, file) { return prev .then(function() { return write(_.extend({}, args, { - 'path': path.join(args.path, file.originalFilename), - 'base64': false, + 'path': path.join(args.path, file.filename), + 'base64': args.base64? true : false, 'content': fs.createReadStream(file.path) })); }); diff --git a/lib/utils/logger.js b/lib/utils/logger.js index 93913bff..425bba7a 100644 --- a/lib/utils/logger.js +++ b/lib/utils/logger.js @@ -6,7 +6,8 @@ var enabled = true; // Colors for log types var colors = { 'log': ['\x1B[36m', '\x1B[39m'], - 'error': ['\x1B[31m', '\x1B[39m'] + 'error': ['\x1B[31m', '\x1B[39m'], + 'warn': ['\x1B[33m', '\x1B[39m'] }; // Base print method @@ -20,6 +21,7 @@ var print = function(logType, logSection) { var error = _.partial(print, 'error'); var log = _.partial(print, 'log'); +var warn = _.partial(print, 'warn'); var exception = _.wrap(error, function(func, logSection, err, kill) { func(logSection, err.message || err); if (err.stack) console.error(err.stack); @@ -44,6 +46,7 @@ module.exports = function(name) { return { 'log': _.partial(log, name), 'error': _.partial(error, name), + 'warn': _.partial(warn, name), 'exception': _.partial(exception, name) }; }; diff --git a/lib/workspace.js b/lib/workspace.js index 612d004c..3dbe93fb 100644 --- a/lib/workspace.js +++ b/lib/workspace.js @@ -6,9 +6,11 @@ var events = require('./events'); var logger = require('./utils/logger')("workspace"); var root = null; +var _config = {}; // Init the workspace var init = function(config) { + _config = config; root = path.resolve(config.root); logger.log("Working on ", root); @@ -38,5 +40,10 @@ module.exports = { init: init, path: getPath, relative: relativePath, - root: function() { return root; } + root: function() { return root; }, + config: function(str) { + return str.split('.').reduce(function(obj, i) { + return obj[i]; + }, _config); + } }; diff --git a/package.json b/package.json index 515f6856..f091b217 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "codebox", "description": "Extensible hybrid IDE", - "version": "1.0.0", + "version": "1.0.0-alpha.5", "author": "FriendCode Inc. ", "license": "Apache 2", "preferGlobal": true, @@ -29,7 +29,8 @@ "dependencies": { "q": "~1.2.0", "lodash": "2.4.1", - "pkgm": "3.1.0", + "pkgm": "3.3.0", + "tmp": "0.0.25", "express": "4.6.1", "express-session": "1.7.0", "wrench": "1.5.8", @@ -39,12 +40,14 @@ "eventemitter2": "0.4.14", "crc": "0.2.1", "request": "2.37.0", - "commander": "2.3.0", + "commander": "2.8.0", "open": "0.0.5", "ini": "1.2.1", - "basic-auth-connect": "1.0.0", - "connect-multiparty": "1.1.0", - "mime": "1.3.4" + "basic-auth": "1.0.0", + "mime": "1.3.4", + "busboy": "0.2.9", + "uuid": "2.0.1", + "base64-stream": "0.1.2" }, "devDependencies": { "gulp": "^3.8.11", @@ -60,27 +63,27 @@ "mocha": "2.2.4", "chai": "2.2.0", "jquery": "~2.1.3", - "hr.utils": "*", - "hr.app": "*", - "hr.list": "*", - "hr.storage": "*", - "hr.model": "0.1.1", - "hr.view": "*", - "hr.collection": "0.1.2", - "hr.class": "*", - "hr.dnd": "*", - "hr.gridview": "0.2.0", - "hr.logger": "0.1.1", - "hr.backend": "0.1.1", + "hr.utils": "0.1.0", + "hr.app": "0.2.0", + "hr.list": "0.3.1", + "hr.storage": "0.2.0", + "hr.model": "0.2.0", + "hr.view": "1.0.1", + "hr.collection": "0.2.0", + "hr.class": "1.2.3", + "hr.dnd": "0.2.0", + "hr.gridview": "0.3.0", + "hr.logger": "0.3.0", + "hr.backend": "0.2.0", + "hr.queue": "0.2.0", "octicons": "2.2.0", "mousetrap": "1.5.2", "moment": "2.9.0", "sockjs-client": "1.0.0-beta.12", - "axios": "0.5.2", + "axios": "0.5.4", "merge-stream": "0.1.7" }, "packageDependencies": { - "image": "CodeboxIDE/package-image#1.0.0", "about": "CodeboxIDE/package-about", "command-palette": "CodeboxIDE/package-command-palette", "files-tree": "CodeboxIDE/package-files-tree", @@ -95,7 +98,9 @@ "find": "CodeboxIDE/package-find", "git": "CodeboxIDE/package-git", "settings": "CodeboxIDE/package-settings", - "menubar": "CodeboxIDE/package-menubar" + "menubar": "CodeboxIDE/package-menubar", + "image": "CodeboxIDE/package-image", + "ctags": "CodeboxIDE/package-ctags" }, "scripts": { "test": "export TESTING=true; mocha --reporter list" diff --git a/test/helper.js b/test/helper.js index 5fd4c82d..ffd4a3e5 100644 --- a/test/helper.js +++ b/test/helper.js @@ -1,12 +1,22 @@ var Q = require("q"); var chai = require("chai"); var path = require("path"); +var wrench = require("wrench"); +var os = require("os"); var codebox = require("../lib"); var users = require("../lib/users"); +var packagesFolder = path.resolve(__dirname, "./packages"); +try { + wrench.mkdirSyncRecursive(packagesFolder, 0777); +} catch (e) {} + var config = { log: false, - root: path.resolve(__dirname, "workspace") + root: path.resolve(__dirname, "workspace"), + packages: { + root: packagesFolder + } }; // Expose assert globally