Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

...
  • 3 commits
  • 2 files changed
  • 0 commit comments
  • 2 contributors
Commits on Dec 11, 2011
Lyle Underwood lyleunderwood fix inheritance issue 4cca3ce
Commits on Dec 12, 2011
Lyle Underwood lyleunderwood rebuilt lib/toaster.js f51b566
Commits on Apr 10, 2012
Anderson Arboleya arboleya Merge pull request #20 from lyleunderwood/inheritance-issue
fix inheritance issue
abb066d
Showing with 408 additions and 317 deletions.
  1. +406 −315 lib/toaster.js
  2. +2 −2 src/toaster/core/script.coffee
721 lib/toaster.js
View
@@ -1,14 +1,10 @@
+
(function() {
var ArrayUtil, Builder, Config, FsUtil, Module, Parser, Project, Question, Script, StringUtil, Toaster, argv, colors, cs, exec, fs, optimist, opts, path, pkg, pn, toaster;
- var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
- for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
- function ctor() { this.constructor = child; }
- ctor.prototype = parent.prototype;
- child.prototype = new ctor;
- child.__super__ = parent.prototype;
- return child;
- };
+ var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; }, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
+
toaster = {};
+
pkg = function(ns) {
var curr, index, part, parts, _len;
curr = null;
@@ -28,31 +24,35 @@
}
return curr;
};
- pkg('toaster.generators').Question = Question = (function() {
- function Question() {}
- Question.prototype.ask = function(question, format, fn) {
- var stdin, stdout;
- stdin = process.stdin;
- stdout = process.stdout;
- stdin.resume();
- stdout.write("" + question + " ");
- return stdin.once('data', __bind(function(data) {
- var msg, rule;
- data = data.toString().trim();
- if (format.test(data)) {
- return fn(data);
- } else {
- msg = "" + 'Invalid entry, it should match:'.red;
- rule = "" + (format.toString().cyan);
- stdout.write("\t" + msg + " " + rule + "\n");
- return this.ask(question, format, fn);
- }
- }, this));
+
+ pkg('toaster.utils').StringUtil = StringUtil = (function() {
+
+ function StringUtil() {}
+
+ StringUtil.titleize = function(str) {
+ var index, word, words, _len;
+ words = str.match(/[a-z]+/gi);
+ for (index = 0, _len = words.length; index < _len; index++) {
+ word = words[index];
+ words[index] = StringUtil.ucasef(word);
+ }
+ return words.join(" ");
};
- return Question;
+
+ StringUtil.ucasef = function(str) {
+ var output;
+ output = str.substr(0, 1).toUpperCase();
+ return output += str.substr(1).toLowerCase();
+ };
+
+ return StringUtil;
+
})();
+
pkg('toaster.utils').ArrayUtil = ArrayUtil = (function() {
+
function ArrayUtil() {}
+
ArrayUtil.find = function(source, search, by_property) {
var k, p, v, _i, _len, _len2, _len3;
if (!by_property) {
@@ -82,6 +82,7 @@
}
return null;
};
+
ArrayUtil.find_all = function(source, search, by_property, regexp, unique) {
var item, k, p, v, _i, _len, _len2, _len3, _output, _unique;
_output = [];
@@ -104,9 +105,7 @@
};
}
}
- if (item) {
- _output.push(item);
- }
+ if (item) _output.push(item);
}
} else {
by_property = [].concat(by_property);
@@ -146,6 +145,7 @@
}
return _output;
};
+
ArrayUtil.diff = function(a, b, by_property) {
var diff, item, search, _i, _j, _len, _len2;
diff = [];
@@ -171,9 +171,11 @@
}
return diff;
};
+
ArrayUtil.has = function(source, search, by_property) {
return ArrayUtil.find(source, search, by_property) != null;
};
+
ArrayUtil.replace_into = function(src, index, items) {
items = [].concat(items);
src.splice(index, 1);
@@ -182,19 +184,26 @@
}
return src;
};
+
return ArrayUtil;
+
})();
+
path = require("path");
+
fs = require("fs");
+
pn = path.normalize;
+
exports.FsUtil = pkg('toaster.utils').FsUtil = FsUtil = (function() {
+
function FsUtil() {}
+
FsUtil.snapshots = {};
+
FsUtil.rmdir_rf = function(folderpath, root) {
var file, files, stats, _i, _len;
- if (root == null) {
- root = true;
- }
+ if (root == null) root = true;
files = fs.readdirSync(folderpath);
for (_i = 0, _len = files.length; _i < _len; _i++) {
file = files[_i];
@@ -207,53 +216,57 @@
fs.unlinkSync(file);
}
}
- if (root) {
- return fs.rmdirSync(folderpath);
- }
+ if (root) return fs.rmdirSync(folderpath);
};
+
FsUtil.mkdir_p = function(folderpath) {
var folder, folders, index, _len, _results;
folders = folderpath.split("/");
_results = [];
for (index = 0, _len = folders.length; index < _len; index++) {
folder = folders[index];
- if (folder === "") {
- continue;
+ if (folder === "") continue;
+ if (!path.existsSync(folder = folders.slice(0, index + 1).join("/"))) {
+ _results.push(fs.mkdirSync(folder, 0755));
+ } else {
+ _results.push(void 0);
}
- _results.push(!path.existsSync(folder = folders.slice(0, index + 1).join("/")) ? fs.mkdirSync(folder, 0755) : void 0);
}
return _results;
};
+
FsUtil.find = function(folderpath, pattern, fn) {
- return exec("find " + folderpath + " -name '" + pattern + "'", __bind(function(error, stdout, stderr) {
+ var _this = this;
+ return exec("find " + folderpath + " -name '" + pattern + "'", function(error, stdout, stderr) {
var buffer, item, items, _i, _len, _ref;
buffer = [];
_ref = items = stdout.trim().split("\n");
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
item = _ref[_i];
- if (item !== "." && item !== ".." && item !== "") {
- buffer.push(item);
- }
+ if (item !== "." && item !== ".." && item !== "") buffer.push(item);
}
return fn(buffer);
- }, this));
+ });
};
+
FsUtil.ls_folders = function(basepath, fn) {
- return exec("find -type d", __bind(function(error, stdout, stderr) {
+ var _this = this;
+ return exec("find -type d", function(error, stdout, stderr) {
var buffer, item, items, _i, _len, _ref;
buffer = [];
_ref = items = stdout.trim().split("\n");
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
item = _ref[_i];
- if (item !== "." && item !== ".." && item !== "") {
- buffer.push(item);
- }
+ if (item !== "." && item !== ".." && item !== "") buffer.push(item);
}
return fn(buffer);
- }, this));
+ });
};
+
FsUtil.watched = {};
+
FsUtil.watch_file = function(filepath, onchange, dispatch_create) {
+ var _this = this;
filepath = pn(filepath);
if (dispatch_create) {
if (typeof onchange === "function") {
@@ -274,7 +287,7 @@
this.watched[filepath] = true;
return fs.watchFile(filepath, {
interval: 250
- }, __bind(function(curr, prev) {
+ }, function(curr, prev) {
var ctime, mtime;
mtime = curr.mtime.valueOf() !== prev.mtime.valueOf();
ctime = curr.ctime.valueOf() !== prev.ctime.valueOf();
@@ -285,9 +298,11 @@
action: "updated"
}) : void 0;
}
- }, this));
+ });
};
+
FsUtil.watch_folder = function(folderpath, filter_regexp, onchange, dispatch_create) {
+ var _this = this;
folderpath = pn(folderpath);
if (typeof onchange === "function") {
onchange({
@@ -296,25 +311,36 @@
action: "watching"
});
}
- exec("ls " + folderpath, __bind(function(error, stdout, stderr) {
+ exec("ls " + folderpath, function(error, stdout, stderr) {
var item, _i, _len, _ref, _results;
FsUtil.snapshots[folderpath] = FsUtil.format_ls(folderpath, stdout);
_ref = FsUtil.snapshots[folderpath];
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
item = _ref[_i];
- _results.push(item.type === "folder" ? FsUtil.watch_folder(item.path, filter_regexp, onchange) : filter_regexp === null || filter_regexp.test(item.path) ? (dispatch_create ? onchange({
- type: item.type,
- path: item.path,
- action: "created"
- }) : void 0, FsUtil.watch_file(item.path, onchange)) : void 0);
+ if (item.type === "folder") {
+ _results.push(FsUtil.watch_folder(item.path, filter_regexp, onchange));
+ } else {
+ if (filter_regexp === null || filter_regexp.test(item.path)) {
+ if (dispatch_create) {
+ onchange({
+ type: item.type,
+ path: item.path,
+ action: "created"
+ });
+ }
+ _results.push(FsUtil.watch_file(item.path, onchange));
+ } else {
+ _results.push(void 0);
+ }
+ }
}
return _results;
- }, this));
+ });
return fs.watchFile(folderpath, {
interval: 250
- }, __bind(function(curr, prev) {
- return exec("ls " + folderpath, __bind(function(error, stdout, stderr) {
+ }, function(curr, prev) {
+ return exec("ls " + folderpath, function(error, stdout, stderr) {
var a, b, diff, info, item, snapshot, _i, _len;
a = FsUtil.snapshots[folderpath];
b = FsUtil.format_ls(folderpath, stdout);
@@ -326,35 +352,28 @@
if (info.action === "created") {
if (info.type === "file") {
if (filter_regexp != null) {
- if (!filter_regexp.test(info.path)) {
- continue;
- }
- }
- if (typeof onchange === "function") {
- onchange(info);
+ if (!filter_regexp.test(info.path)) continue;
}
+ if (typeof onchange === "function") onchange(info);
FsUtil.watch_file(info.path, onchange);
} else if (info.type === "folder") {
- if (typeof onchange === "function") {
- onchange(info);
- }
+ if (typeof onchange === "function") onchange(info);
FsUtil.watch_folder(info.path, filter_regexp, onchange, true);
}
} else if (info.action === "deleted") {
- if (this.watched[info.path] === true) {
- this.watched[info.path];
- if (typeof onchange === "function") {
- onchange(info);
- }
+ if (_this.watched[info.path] === true) {
+ _this.watched[info.path];
+ if (typeof onchange === "function") onchange(info);
fs.unwatchFile(item.path);
}
}
}
snapshot = FsUtil.format_ls(folderpath, stdout);
return FsUtil.snapshots[folderpath] = snapshot;
- }, this));
- }, this));
+ });
+ });
};
+
FsUtil.format_ls = function(folderpath, stdout) {
var index, item, list, stats, _len;
list = stdout.toString().trim().split("\n");
@@ -372,37 +391,26 @@
}
return list;
};
+
return FsUtil;
+
})();
- pkg('toaster.utils').StringUtil = StringUtil = (function() {
- function StringUtil() {}
- StringUtil.titleize = function(str) {
- var index, word, words, _len;
- words = str.match(/[a-z]+/gi);
- for (index = 0, _len = words.length; index < _len; index++) {
- word = words[index];
- words[index] = StringUtil.ucasef(word);
- }
- return words.join(" ");
- };
- StringUtil.ucasef = function(str) {
- var output;
- output = str.substr(0, 1).toUpperCase();
- return output += str.substr(1).toLowerCase();
- };
- return StringUtil;
- })();
+
fs = require("fs");
+
cs = require("coffee-script");
+
pkg('toaster.core').Script = Script = (function() {
+
function Script(module, realpath, opts) {
this.module = module;
this.realpath = realpath;
this.opts = opts;
this.getinfo();
}
+
Script.prototype.getinfo = function() {
- var item, klass, repl, requirements, rgx, rgx_ext, _i, _j, _len, _len2, _ref, _ref2, _results;
+ var baseclass, item, klass, repl, requirements, rgx, rgx_ext, _i, _j, _len, _len2, _ref, _ref2, _results;
this.raw = fs.readFileSync(this.realpath, "utf-8");
this.dependencies_collapsed = [];
this.baseclasses = [];
@@ -410,18 +418,14 @@
this.filename = /[\w-]+\.[\w-]+/.exec(this.filepath)[0];
this.filefolder = this.filepath.replace("/" + this.filename, "") + "/";
this.namespace = "";
- if (this.filepath.indexOf("/") === -1) {
- this.filefolder = "";
- }
+ if (this.filepath.indexOf("/") === -1) this.filefolder = "";
this.namespace = this.filefolder.replace(/\//g, ".").slice(0, -1);
rgx = "(^|=\\s*)(class)+\\s+(\\w+)" + "(\\s*$|\\s+(extends)\\s+(\\w+)\\s*$)";
rgx_ext = "(^|=\\s*)(class)\\s(\\w+)\\s(extends)\\s(\\w+)\\s*$";
if (this.raw.match(new RegExp(rgx, "m")) != null) {
if (this.namespace !== "") {
repl = "$1pkg( '" + this.namespace + "' ).$3 = $2 $3";
- if (new RegExp(rgx_ext, "m").test(this.raw)) {
- repl += "$4";
- }
+ if (new RegExp(rgx_ext, "m").test(this.raw)) repl += "$4";
this.raw = this.raw.replace(new RegExp(rgx, "gm"), repl);
}
this.classname = this.raw.match(new RegExp(rgx, "m"))[3];
@@ -433,7 +437,7 @@
_ref2 = (_ref = this.raw.match(new RegExp(rgx_ext, "gm"))) != null ? _ref : [];
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
klass = _ref2[_i];
- this.baseclass = this.klass.match(new RegExp(rgx_ext, "m"))[5];
+ baseclass = klass.match(new RegExp(rgx_ext, "m"))[5];
this.baseclasses.push(baseclass);
}
}
@@ -450,6 +454,7 @@
return _results;
}
};
+
Script.prototype.expand_dependencies = function() {
var dependency, expanded, files, found, index, reg, _len, _ref, _results;
files = this.module.files;
@@ -480,17 +485,126 @@
}
return _results;
};
+
return Script;
+
+ })();
+
+ pkg('toaster.generators').Question = Question = (function() {
+
+ function Question() {}
+
+ Question.prototype.ask = function(question, format, fn) {
+ var stdin, stdout;
+ var _this = this;
+ stdin = process.stdin;
+ stdout = process.stdout;
+ stdin.resume();
+ stdout.write("" + question + " ");
+ return stdin.once('data', function(data) {
+ var msg, rule;
+ data = data.toString().trim();
+ if (format.test(data)) {
+ return fn(data);
+ } else {
+ msg = "" + 'Invalid entry, it should match:'.red;
+ rule = "" + (format.toString().cyan);
+ stdout.write("\t" + msg + " " + rule + "\n");
+ return _this.ask(question, format, fn);
+ }
+ });
+ };
+
+ return Question;
+
+ })();
+
+ pkg('toaster.generators').Config = Config = (function() {
+
+ __extends(Config, Question);
+
+ Config.prototype.tpl = "module '%name'\n\tsrc: '%src'\n\trelease: '%release'\n";
+
+ function Config(basepath) {
+ this.basepath = basepath;
+ }
+
+ Config.prototype.create = function(folderpath) {
+ var default_name, question1, question2, question3;
+ var _this = this;
+ if ((folderpath != null) && folderpath !== true) {
+ this.basepath = "" + this.basepath + "/" + folderpath;
+ }
+ console.log("" + 'Let\'s toast this sly little project! :)'.grey.bold);
+ console.log(". With this as your basepath: " + this.basepath.cyan);
+ console.log(". Please, tell me:");
+ default_name = this.basepath.split("/").pop();
+ question1 = "\tWhat's your module name? (" + default_name + ")";
+ question2 = "\tWhere's its src folder? (i.e. src)";
+ question3 = "\tWhere do you want your release file? " + "(i.e. release/app.js)";
+ return this.ask(question1, /.*/, function(name) {
+ return _this.ask(question2, /.+/, function(src) {
+ return _this.ask(question3, /.+/, function(release) {
+ _this.write(name || default_name, src, release);
+ return process.exit();
+ });
+ });
+ });
+ };
+
+ Config.prototype.write = function(name, src, release) {
+ path = pn("" + this.basepath + "/toaster.coffee");
+ toaster = this.tpl.replace("%name", name);
+ toaster = toaster.replace("%src", src);
+ toaster = toaster.replace("%release", release);
+ fs.writeFileSync(path, toaster);
+ return console.log("" + 'Created'.green.bold + " " + path);
+ };
+
+ return Config;
+
})();
+
+ optimist = require('optimist');
+
+ pkg('toaster').Parser = Parser = (function() {
+
+ function Parser() {
+ var adendo, usage;
+ usage = "" + 'CoffeeToaster'.bold + "\n";
+ usage += " Minimalist dependency management for CoffeeScript\n\n".grey;
+ usage += "" + 'Usage:' + "\n";
+ usage += " toaster [" + 'options'.green + "] [" + 'path'.green + "]\n\n";
+ usage += "" + 'Examples:' + "\n";
+ usage += " toaster -n myawsomeapp (" + 'required'.red + ")\n";
+ usage += " toaster -i [myawsomeapp] (" + 'optional'.green + ")\n";
+ usage += " toaster -w [myawsomeapp] (" + 'optional'.green + ")";
+ adendo = "";
+ this.argv = (this.opts = optimist.usage(usage).alias('n', 'new').describe('n', "Scaffold a very basic new App" + adendo).alias('i', 'init').describe('i', "Create a config (toaster.coffee) file" + adendo).alias('w', 'watch').describe('w', "Start watching/compiling your project" + adendo).alias('c', 'compile').boolean('c').describe('c', "Compile the entire project, without watching it.").alias('d', 'debug').boolean('d')["default"]('d', false).describe('d', 'Debug mode (compile js files individually)').alias('v', 'version').describe('v', '').alias('h', 'help').describe('h', '')).argv;
+ }
+
+ return Parser;
+
+ })();
+
fs = require("fs");
+
path = require("path");
+
pn = path.normalize;
+
exec = (require("child_process")).exec;
+
colors = require('colors');
+
pkg('toaster').Config = Config = (function() {
+
Config.prototype.modules = {};
+
Config.prototype.vendors = {};
+
Config.prototype.builds = {};
+
function Config(basepath) {
var build, filepath, module, vendor;
this.basepath = basepath;
@@ -511,6 +625,7 @@
console.log("\tTry running: " + "toaster -i".green + (" or type " + 'toaster -h'.green + " for more info"));
}
}
+
Config.prototype.module = function(name, params) {
var _ref;
params.name = name;
@@ -519,9 +634,11 @@
params.release = pn("" + this.basepath + "/" + params.release);
return this.modules[name] = params;
};
+
Config.prototype.vendor = function(name, src) {
return this.vendors[name] = pn("" + this.basepath + "/" + src);
};
+
Config.prototype.build = function(name, params) {
var _ref;
params.name = name;
@@ -529,81 +646,85 @@
params.release = pn("" + this.basepath + "/" + params.release);
return this.builds[name] = params;
};
+
return Config;
+
})();
- fs = require('fs');
- path = require('path');
- pkg('toaster.core').Builder = Builder = (function() {
- function Builder(toaster) {
- this.toaster = toaster;
+
+ exports.Project = pkg('toaster.generators').Project = Project = (function() {
+
+ __extends(Project, Question);
+
+ function Project(basepath) {
+ this.basepath = basepath;
}
- Builder.prototype.build = function() {
- var build, builds, modules, name, vendors, _results;
- builds = this.toaster.config.builds;
- _results = [];
- for (name in builds) {
- build = builds[name];
- modules = this.merge_modules(build.modules);
- vendors = this.merge_vendors(this.unify_vendors(build.modules));
- _results.push(fs.writeFileSync(build.release, "" + vendors + "\n" + modules));
+
+ Project.prototype.create = function(folderpath, name, src, release) {
+ var default_name, question1, question2, question3, target;
+ var _this = this;
+ if (!folderpath || folderpath === true) {
+ console.log(("" + 'Error'.bold.red + " You need to inform") + "a target path!");
+ return console.log("\ttoaster new myawesomeapp".green);
}
- return _results;
- };
- Builder.prototype.unify_vendors = function(modules) {
- var module, unique, vendor, _i, _j, _len, _len2, _ref;
- unique = [];
- for (_i = 0, _len = modules.length; _i < _len; _i++) {
- module = modules[_i];
- if ((module = this.toaster.modules[module]) == null) {
- return console.log("MODULE NOT FOUND!");
- }
- _ref = module.vendors;
- for (_j = 0, _len2 = _ref.length; _j < _len2; _j++) {
- vendor = _ref[_j];
- if (!ArrayUtil.has(unique, vendor)) {
- unique.push(vendor);
- }
- }
+ if (folderpath.substr(0, 1) !== "/") {
+ target = "" + this.basepath + "/" + folderpath;
+ } else {
+ target = folderpath;
}
- return unique;
- };
- Builder.prototype.merge_vendors = function(vendors) {
- var buffer, vendor, vendor_name, _i, _len;
- buffer = [];
- for (_i = 0, _len = vendors.length; _i < _len; _i++) {
- vendor_name = vendors[_i];
- if ((vendor = this.toaster.config.vendors[vendor_name]) != null) {
- if (path.existsSync(vendor)) {
- buffer.push(fs.readFileSync(vendor, 'utf-8'));
- } else {
- console.log("WARNING!".bold.yellow, "Vendor".white, vendor_name.yellow.bold, "not found at".white, vendor.yellow.bold);
- }
- } else {
- console.log("WARNING!".bold.yellow, "Vendor".yellow, vendor_name.white.bold, "not found for module".yellow, this.name.white.bold);
- }
+ if ((name != null) && (src != null) && (release != null)) {
+ return this.scaffold(target, name, src, release);
}
- return buffer.join("\n");
+ default_name = target.split('/').pop();
+ console.log("" + 'Let\'s toast something fresh! :)'.grey.bold);
+ console.log(". With this as your basepath: " + target.cyan);
+ console.log(". Please tell me:");
+ question1 = "\tWhat's your app name? (" + default_name + ")";
+ question2 = "\tWhere's its src folder (absolute/relative)? (src)";
+ question3 = "\tWhere do you want your release file? (release/app.js)";
+ return this.ask(question1, /.*/, function(name) {
+ return _this.ask(question2, /.*/, function(src) {
+ return _this.ask(question3, /.*/, function(release) {
+ _this.scaffold(target, name || default_name, src, release);
+ return process.exit();
+ });
+ });
+ });
};
- Builder.prototype.merge_modules = function(modules) {
- var buffer, module, _i, _len;
- buffer = [];
- for (_i = 0, _len = modules.length; _i < _len; _i++) {
- module = modules[_i];
- if ((module = this.toaster.modules[module]) != null) {
- buffer.push(module.compile());
- } else {
- console.log("MODULE NOT FOUN!");
- }
+
+ Project.prototype.scaffold = function(target, name, src, release) {
+ var releasedir, releasefile, srcdir;
+ srcdir = ("" + target + "/") + (src || "src");
+ releasefile = ("" + target + "/") + (release || "release/app.js");
+ releasedir = releasefile.split("/").slice(0, -1).join("/");
+ if (path.existsSync(target)) {
+ console.log(("" + 'Error'.bold.red + " Folder exists! " + target).red);
+ return;
}
- return buffer.join("\n");
+ fs.mkdirSync(target, 0755);
+ console.log("" + 'Created'.green.bold + " " + target);
+ fs.mkdirSync(srcdir, 0755);
+ console.log("" + 'Created'.green.bold + " " + srcdir);
+ fs.mkdirSync(releasedir, 0755);
+ console.log("" + 'Created'.green.bold + " " + releasedir);
+ name = name;
+ srcdir = srcdir.replace(target, "").substr(1);
+ releasefile = releasefile.replace(target, "").substr(1);
+ return new Config(target).write(name, srcdir, releasefile);
};
- return Builder;
+
+ return Project;
+
})();
+
cs = require("coffee-script");
+
pkg('toaster.core').Module = Module = (function() {
var missing;
+
Module.prototype.pkg_helper = "pkg = ( ns )->\n curr = null\n parts = [].concat = ns.split( \".\" )\n for part, index in parts\n if curr == null\n curr = eval part\n continue\n else\n unless curr[ part ]?\n curr = curr[ part ] = {}\n else\n curr = curr[ part ]\n curr\n";
+
function Module(toaster, config, opts) {
+ var _this = this;
this.toaster = toaster;
this.config = config;
this.opts = opts;
@@ -612,39 +733,39 @@
this.vendors = this.config.vendors;
this.release = this.config.release;
this.files = [];
- FsUtil.find(this.src, "*.coffee", __bind(function(result) {
+ FsUtil.find(this.src, "*.coffee", function(result) {
var file, _i, _len;
for (_i = 0, _len = result.length; _i < _len; _i++) {
file = result[_i];
- this.files.push(new Script(this, file, this.opts));
- }
- this.write();
- if (this.opts.argv.w) {
- return this.watch();
+ _this.files.push(new Script(_this, file, _this.opts));
}
- }, this));
+ _this.write();
+ if (_this.opts.argv.w) return _this.watch();
+ });
}
+
Module.prototype.watch = function() {
- return FsUtil.watch_folder(this.src, /.coffee$/, __bind(function(info) {
+ var _this = this;
+ return FsUtil.watch_folder(this.src, /.coffee$/, function(info) {
var file, msg, relative_path, type;
type = StringUtil.titleize(info.type);
- relative_path = info.path.replace("" + this.src + "/", "");
+ relative_path = info.path.replace("" + _this.src + "/", "");
switch (info.action) {
case "created":
if (info.type === "file") {
- this.files.push(new Script(this, info.path, this.opts));
+ _this.files.push(new Script(_this, info.path, _this.opts));
}
msg = "" + ('New ' + info.type + ' created:').bold.green;
console.log("" + msg + " " + info.path.green);
break;
case "deleted":
- file = ArrayUtil.find(this.files, relative_path, "filepath");
- this.files.splice(file.index, 1);
+ file = ArrayUtil.find(_this.files, relative_path, "filepath");
+ _this.files.splice(file.index, 1);
msg = "" + (type + ' deleted, stop watching: ').bold.red;
console.log("" + msg + " " + info.path.red);
break;
case "updated":
- file = ArrayUtil.find(this.files, relative_path, "filepath");
+ file = ArrayUtil.find(_this.files, relative_path, "filepath");
file.item.getinfo();
msg = "" + (type + ' changed').bold.yellow;
console.log("" + msg + " " + info.path.yellow);
@@ -653,16 +774,13 @@
msg = "" + ('Watching ' + info.type + ':').bold.cyan;
console.log("" + msg + " " + info.path.cyan);
}
- if (info.action !== "watching") {
- return this.write();
- }
- }, this));
+ if (info.action !== "watching") return _this.write();
+ });
};
+
Module.prototype.compile = function(include_vendors) {
var compiled, file, line, msg, output, _i, _j, _len, _len2, _ref;
- if (include_vendors == null) {
- include_vendors = true;
- }
+ if (include_vendors == null) include_vendors = true;
_ref = this.files;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
file = _ref[_i];
@@ -680,7 +798,7 @@
}
return _results;
}).call(this)).join("\n");
- output = output.replace("{root_namespaces}", this.get_root_namespaces());
+ output = output.replace("toaster = {}", this.get_root_namespaces());
try {
compiled = cs.compile(output);
} catch (err) {
@@ -700,6 +818,7 @@
}
return compiled;
};
+
Module.prototype.get_root_namespaces = function() {
var file, k, namespaces_buffer, root_namespaces, v, _i, _len, _ref;
root_namespaces = {};
@@ -718,20 +837,17 @@
}
return namespaces_buffer;
};
+
Module.prototype.write = function() {
var contents, file, filepath, folderpath, index, relative, tmpl, toaster_buffer, toaster_src, vendors, _len, _ref;
- if ((contents = this.compile()) == null) {
- return;
- }
+ if ((contents = this.compile()) == null) return;
vendors = this.toaster.builder.merge_vendors(this.vendors);
fs.writeFileSync(this.release, "" + vendors + "\n" + contents);
console.log("" + '.'.bold.green + " " + this.release);
if (this.opts.argv.debug) {
toaster = this.release.split("/").slice(0, -1).join('/') + "/toaster";
toaster_src = "" + toaster + "/src";
- if (path.existsSync(toaster)) {
- FsUtil.rmdir_rf(toaster);
- }
+ if (path.existsSync(toaster)) FsUtil.rmdir_rf(toaster);
FsUtil.mkdir_p(toaster_src);
tmpl = "document.write('<scri'+'pt src=\"%SRC%\"></scr'+'ipt>')";
toaster_buffer = ("" + vendors + "\n" + (this.get_root_namespaces()) + "\n") + ("" + this.pkg_helper + "\n");
@@ -742,9 +858,7 @@
relative = relative.replace(".coffee", ".js");
filepath = "" + toaster_src + "/" + relative;
folderpath = filepath.split('/').slice(0, -1).join("/");
- if (!path.existsSync(folderpath)) {
- FsUtil.mkdir_p(folderpath);
- }
+ if (!path.existsSync(folderpath)) FsUtil.mkdir_p(folderpath);
relative = "./toaster/src/" + relative;
fs.writeFileSync(filepath, cs.compile(file.raw, {
bare: 1
@@ -756,32 +870,24 @@
}
return this.toaster.builder.build();
};
+
missing = {};
+
Module.prototype.reorder = function(cycling) {
var bc, dependency, dependency_index, file, file_index, filepath, found, i, index, not_found, _len, _len2, _ref, _ref2, _results;
- if (cycling == null) {
- cycling = false;
- }
- if (cycling === false) {
- this.missing = {};
- }
+ if (cycling == null) cycling = false;
+ if (cycling === false) this.missing = {};
_ref = this.files;
_results = [];
for (i = 0, _len = _ref.length; i < _len; i++) {
file = _ref[i];
- if (!file.dependencies.length && !file.baseclasses.length) {
- continue;
- }
+ if (!file.dependencies.length && !file.baseclasses.length) continue;
_ref2 = file.dependencies;
for (index = 0, _len2 = _ref2.length; index < _len2; index++) {
filepath = _ref2[index];
dependency = ArrayUtil.find(this.files, filepath, "filepath");
- if (dependency != null) {
- dependency_index = dependency.index;
- }
- if (dependency_index < i && (dependency != null)) {
- continue;
- }
+ if (dependency != null) dependency_index = dependency.index;
+ if (dependency_index < i && (dependency != null)) continue;
if (dependency != null) {
if (ArrayUtil.has(dependency.item.dependencies, file.filepath, "filepath")) {
file.dependencies.splice(index, 1);
@@ -810,144 +916,125 @@
bc = _ref3[_i];
found = ArrayUtil.find(this.files, bc, "classname");
not_found = (found.index > file_index) || (found === null);
- _results2.push(not_found && !this.missing[bc] ? (this.missing[bc] = true, console.log(("" + 'WARNING!'.bold + " Base class").yellow, bc.bold.white, "not found for class".yellow, file.classname.bold.white, "in file".yellow, file.filepath.yellow)) : void 0);
+ if (not_found && !this.missing[bc]) {
+ this.missing[bc] = true;
+ _results2.push(console.log(("" + 'WARNING!'.bold + " Base class").yellow, bc.bold.white, "not found for class".yellow, file.classname.bold.white, "in file".yellow, file.filepath.yellow));
+ } else {
+ _results2.push(void 0);
+ }
}
return _results2;
}).call(this));
}
return _results;
};
+
return Module;
+
})();
- pkg('toaster.generators').Config = Config = (function() {
- __extends(Config, Question);
- Config.prototype.tpl = "module '%name'\n\tsrc: '%src'\n\trelease: '%release'\n";
- function Config(basepath) {
- this.basepath = basepath;
+
+ fs = require('fs');
+
+ path = require('path');
+
+ pkg('toaster.core').Builder = Builder = (function() {
+
+ function Builder(toaster) {
+ this.toaster = toaster;
}
- Config.prototype.create = function(folderpath) {
- var default_name, question1, question2, question3;
- if ((folderpath != null) && folderpath !== true) {
- this.basepath = "" + this.basepath + "/" + folderpath;
+
+ Builder.prototype.build = function() {
+ var build, builds, modules, name, vendors, _results;
+ builds = this.toaster.config.builds;
+ _results = [];
+ for (name in builds) {
+ build = builds[name];
+ modules = this.merge_modules(build.modules);
+ vendors = this.merge_vendors(this.unify_vendors(build.modules));
+ _results.push(fs.writeFileSync(build.release, "" + vendors + "\n" + modules));
}
- console.log("" + 'Let\'s toast this sly little project! :)'.grey.bold);
- console.log(". With this as your basepath: " + this.basepath.cyan);
- console.log(". Please, tell me:");
- default_name = this.basepath.split("/").pop();
- question1 = "\tWhat's your module name? (" + default_name + ")";
- question2 = "\tWhere's its src folder? (i.e. src)";
- question3 = "\tWhere do you want your release file? " + "(i.e. release/app.js)";
- return this.ask(question1, /.*/, __bind(function(name) {
- return this.ask(question2, /.+/, __bind(function(src) {
- return this.ask(question3, /.+/, __bind(function(release) {
- this.write(name || default_name, src, release);
- return process.exit();
- }, this));
- }, this));
- }, this));
- };
- Config.prototype.write = function(name, src, release) {
- path = pn("" + this.basepath + "/toaster.coffee");
- toaster = this.tpl.replace("%name", name);
- toaster = toaster.replace("%src", src);
- toaster = toaster.replace("%release", release);
- fs.writeFileSync(path, toaster);
- return console.log("" + 'Created'.green.bold + " " + path);
+ return _results;
};
- return Config;
- })();
- exports.Project = pkg('toaster.generators').Project = Project = (function() {
- __extends(Project, Question);
- function Project(basepath) {
- this.basepath = basepath;
- }
- Project.prototype.create = function(folderpath, name, src, release) {
- var default_name, question1, question2, question3, target;
- if (!folderpath || folderpath === true) {
- console.log(("" + 'Error'.bold.red + " You need to inform") + "a target path!");
- return console.log("\ttoaster new myawesomeapp".green);
- }
- if (folderpath.substr(0, 1) !== "/") {
- target = "" + this.basepath + "/" + folderpath;
- } else {
- target = folderpath;
+
+ Builder.prototype.unify_vendors = function(modules) {
+ var module, unique, vendor, _i, _j, _len, _len2, _ref;
+ unique = [];
+ for (_i = 0, _len = modules.length; _i < _len; _i++) {
+ module = modules[_i];
+ if ((module = this.toaster.modules[module]) == null) {
+ return console.log("MODULE NOT FOUND!");
+ }
+ _ref = module.vendors;
+ for (_j = 0, _len2 = _ref.length; _j < _len2; _j++) {
+ vendor = _ref[_j];
+ if (!ArrayUtil.has(unique, vendor)) unique.push(vendor);
+ }
}
- if ((name != null) && (src != null) && (release != null)) {
- return this.scaffold(target, name, src, release);
+ return unique;
+ };
+
+ Builder.prototype.merge_vendors = function(vendors) {
+ var buffer, vendor, vendor_name, _i, _len;
+ buffer = [];
+ for (_i = 0, _len = vendors.length; _i < _len; _i++) {
+ vendor_name = vendors[_i];
+ if ((vendor = this.toaster.config.vendors[vendor_name]) != null) {
+ if (path.existsSync(vendor)) {
+ buffer.push(fs.readFileSync(vendor, 'utf-8'));
+ } else {
+ console.log("WARNING!".bold.yellow, "Vendor".white, vendor_name.yellow.bold, "not found at".white, vendor.yellow.bold);
+ }
+ } else {
+ console.log("WARNING!".bold.yellow, "Vendor".yellow, vendor_name.white.bold, "not found for module".yellow, this.name.white.bold);
+ }
}
- default_name = target.split('/').pop();
- console.log("" + 'Let\'s toast something fresh! :)'.grey.bold);
- console.log(". With this as your basepath: " + target.cyan);
- console.log(". Please tell me:");
- question1 = "\tWhat's your app name? (" + default_name + ")";
- question2 = "\tWhere's its src folder (absolute/relative)? (src)";
- question3 = "\tWhere do you want your release file? (release/app.js)";
- return this.ask(question1, /.*/, __bind(function(name) {
- return this.ask(question2, /.*/, __bind(function(src) {
- return this.ask(question3, /.*/, __bind(function(release) {
- this.scaffold(target, name || default_name, src, release);
- return process.exit();
- }, this));
- }, this));
- }, this));
+ return buffer.join("\n");
};
- Project.prototype.scaffold = function(target, name, src, release) {
- var releasedir, releasefile, srcdir;
- srcdir = ("" + target + "/") + (src || "src");
- releasefile = ("" + target + "/") + (release || "release/app.js");
- releasedir = releasefile.split("/").slice(0, -1).join("/");
- if (path.existsSync(target)) {
- console.log(("" + 'Error'.bold.red + " Folder exists! " + target).red);
- return;
+
+ Builder.prototype.merge_modules = function(modules) {
+ var buffer, module, _i, _len;
+ buffer = [];
+ for (_i = 0, _len = modules.length; _i < _len; _i++) {
+ module = modules[_i];
+ if ((module = this.toaster.modules[module]) != null) {
+ buffer.push(module.compile());
+ } else {
+ console.log("MODULE NOT FOUN!");
+ }
}
- fs.mkdirSync(target, 0755);
- console.log("" + 'Created'.green.bold + " " + target);
- fs.mkdirSync(srcdir, 0755);
- console.log("" + 'Created'.green.bold + " " + srcdir);
- fs.mkdirSync(releasedir, 0755);
- console.log("" + 'Created'.green.bold + " " + releasedir);
- name = name;
- srcdir = srcdir.replace(target, "").substr(1);
- releasefile = releasefile.replace(target, "").substr(1);
- return new Config(target).write(name, srcdir, releasefile);
+ return buffer.join("\n");
};
- return Project;
- })();
- optimist = require('optimist');
- pkg('toaster').Parser = Parser = (function() {
- function Parser() {
- var adendo, usage;
- usage = "" + 'CoffeeToaster'.bold + "\n";
- usage += " Minimalist dependency management for CoffeeScript\n\n".grey;
- usage += "" + 'Usage:' + "\n";
- usage += " toaster [" + 'options'.green + "] [" + 'path'.green + "]\n\n";
- usage += "" + 'Examples:' + "\n";
- usage += " toaster -n myawsomeapp (" + 'required'.red + ")\n";
- usage += " toaster -i [myawsomeapp] (" + 'optional'.green + ")\n";
- usage += " toaster -w [myawsomeapp] (" + 'optional'.green + ")";
- adendo = "";
- this.argv = (this.opts = optimist.usage(usage).alias('n', 'new').describe('n', "Scaffold a very basic new App" + adendo).alias('i', 'init').describe('i', "Create a config (toaster.coffee) file" + adendo).alias('w', 'watch').describe('w', "Start watching/compiling your project" + adendo).alias('c', 'compile').boolean('c').describe('c', "Compile the entire project, without watching it.").alias('d', 'debug').boolean('d')["default"]('d', false).describe('d', 'Debug mode (compile js files individually)').alias('v', 'version').describe('v', '').alias('h', 'help').describe('h', '')).argv;
- }
- return Parser;
+
+ return Builder;
+
})();
+
fs = require("fs");
+
path = require("path");
+
pn = path.normalize;
+
exec = (require("child_process")).exec;
+
colors = require('colors');
+
opts = null;
+
argv = null;
+
exports.run = function() {
return toaster = new Toaster;
};
+
Toaster = (function() {
+
Toaster.prototype.modules = {};
+
function Toaster() {
this.parser = new Parser;
this.builder = new Builder(this);
- if (this.parser.argv.h) {
- return console.log(this.parser.opts.help());
- }
+ if (this.parser.argv.h) return console.log(this.parser.opts.help());
this.basepath = path.resolve(".");
if (this.parser.argv.v) {
path = pn(__dirname + "/../build/VERSION");
@@ -961,6 +1048,7 @@
this.init_modules();
}
}
+
Toaster.prototype.init_modules = function() {
var k, v, _ref, _results;
_ref = this.config.modules;
@@ -971,6 +1059,9 @@
}
return _results;
};
+
return Toaster;
+
})();
+
}).call(this);
4 src/toaster/core/script.coffee
View
@@ -65,7 +65,7 @@ class Script
# colletcts the base classes, in case some class in the file
# extends something
for klass in @raw.match( new RegExp rgx_ext, "gm" ) ? []
- @baseclass = @klass.match( new RegExp(rgx_ext, "m"))[5]
+ baseclass = klass.match( new RegExp(rgx_ext, "m"))[5]
@baseclasses.push baseclass
# then if there's other dependencies
@@ -112,4 +112,4 @@ class Script
continue
# otherwise rewrites found array with filepath info only
- @dependencies.push expanded.item.filepath for expanded in found
+ @dependencies.push expanded.item.filepath for expanded in found

No commit comments for this range

Something went wrong with that request. Please try again.