kaffee-coffeemaker
Version:
The kaffee-coffeemaker plugin compiles Coffeescript files into Javascript.
193 lines (173 loc) • 5.61 kB
JavaScript
(function() {
var CoffeeScript, Fs, Path;
Fs = require("fs");
Path = require("path");
CoffeeScript = require("coffee-script");
/*
The kaffee-coffeemaker plugins compiles Coffeescript files into Javascript files.
@version 0.0.1
@author Fabian M. <mail.fabianm@gmail.com>
*/
module.exports = function() {
/*
Utility to create a directory.
@since 0.0.1
@param path The path to the directory to create.
*/
var compile, getFiles, mkdir, test;
mkdir = function(path) {
try {
return Fs.mkdirSync(path);
} catch (e) {
if (e.errno === 34) {
mkdir(Path.dirname(path));
return mkdir(path);
}
}
};
/*
Returns an array of Coffeescript files in the given directory
and its childs.
@since 0.0.1
@param path The path to the directory.
@return An array of Coffeescript files in the given directory
and its childs.
*/
getFiles = function(path) {
var file, files, _i, _len, _ref;
if (!Fs.existsSync(path)) {
return [];
}
files = [];
_ref = Fs.readdirSync(path);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
file = _ref[_i];
files.push(Path.join(path, file));
}
return files;
};
/*
Tests all Coffeescript files in the given directory.
@since 0.0.1
@param path The path to the directory to check.
@return <code>true</code> if no errors occured, <code>false</code> otherwise.
*/
test = function(path) {
var file, ok, stats, _i, _len, _ref;
ok = true;
_ref = getFiles(path);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
file = _ref[_i];
try {
stats = Fs.lstatSync(file);
if (stats.isDirectory()) {
if (!test.call(this, file)) {
ok = false;
}
continue;
}
if (Path.extname(file) === ".coffee") {
CoffeeScript.compile(Fs.readFileSync(file, 'UTF-8'));
}
} catch (e) {
e.message += " in " + file;
this.logger.error(e);
return false;
}
}
return ok;
};
/*
Compiles all Coffeescript files in the given directory.
@since 0.0.1
@param goal The goal instance.
@param path The path to the input directory.
@param output The path to the output directory.
*/
compile = function(path, output) {
var fd, file, files, i, out, stats, _i, _len, _ref, _results;
if (!test.call(this, path)) {
return;
}
files = getFiles(path);
if (!(Fs.existsSync(output) && files.length > 0)) {
mkdir(output);
}
_ref = getFiles(path);
_results = [];
for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
file = _ref[i];
try {
stats = Fs.lstatSync(file);
if (stats.isDirectory()) {
compile.call(this, file, Path.join(output, Path.basename(file)));
}
if (Path.extname(file) === ".coffee") {
this.logger.info(file);
fd = Fs.openSync(Path.join(output, Path.basename(file, ".coffee") + ".js"), "w");
out = CoffeeScript.compile(Fs.readFileSync(file, 'UTF-8'));
Fs.writeSync(fd, out, 0, out.length);
_results.push(Fs.closeSync(fd));
} else if (Path.basename(file) === "package.json") {
this.logger.info(file);
fd = Fs.openSync(Path.join(output, Path.basename(file)), "w");
out = Fs.readFileSync(file, 'UTF-8');
Fs.writeSync(fd, out, 0, out.length);
_results.push(Fs.closeSync(fd));
} else {
_results.push(void 0);
}
} catch (e) {
e.message += " in " + file;
_results.push(this.logger.error(e));
}
}
return _results;
};
/*
Compiles Coffeescript files into Javascript files.
*/
this.register("compile", function(request) {
var structure;
this.dependsOn("clean", request);
structure = this.getProject().getConfiguration().getKaffeeConfiguration().getStructure();
if (!structure) {
return this.logger.warn("No structure");
}
this.logger.info("Compiling files for project " + (this.getProject().getConfiguration().getName()));
compile.call(this, structure.get('src'), structure.get('bin'));
return compile.call(this, structure.get('src-test'), structure.get('bin-test'));
});
/*
Tests Coffeescript files.
*/
this.register("test", function() {
var ok, structure;
structure = this.getProject().getConfiguration().getKaffeeConfiguration().getStructure();
if (!structure) {
return this.logger.warn("No structure");
}
this.logger.info("Testing files for project " + (this.getProject().getConfiguration().getName()));
ok = test.call(this, structure.get('src')) && test.call(this, structure.get('src-test'));
if (ok) {
return this.logger.info("Test passed!");
} else {
return this.logger.warn("Test failed!");
}
});
/*
Registers the archtype.
*/
return this.archtype({
kaffee: {
plugins: {
"kaffee-coffeemaker": {}
},
lifecycles: {
"compile": ["kaffee-coffeemaker:compile"],
"test": ["kaffee-coffeemaker:test"]
}
}
});
};
}).call(this);