Skip to content

Instantly share code, notes, and snippets.

@ELLIOTTCABLE
Created November 28, 2009 22:17
Show Gist options
  • Select an option

  • Save ELLIOTTCABLE/244669 to your computer and use it in GitHub Desktop.

Select an option

Save ELLIOTTCABLE/244669 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
(function(){
var posix = require('posix'),
acquire = require('/Users/elliottcable/Code/poopy.js/lib/acquire').acquire,
jess = acquire.absolute('/Users/elliottcable/Code/JESS/lib/jess.js').wait();
var jessc = {};
// Either accepts a list of source files and a destination, or a single
// source file and (optional) destination. E.g.
//
// jessc file.jess # equivalent to jessc file.jess file.js
// jessc file.jess compiled/file.js
// jessc something.jess else.jess an_executable compiled/
// jessc lib/*.jess lib/
//
jessc['compileFile'] = function () {
var compilation = new(process.Promise),
sourcePaths = Array.prototype.slice.apply(arguments, [0]),
destinationPath, me = arguments.callee;
if (sourcePaths.length > 1) { destinationPath = sourcePaths.pop() }
else { destinationPath = process.cwd() };
if (destinationPath[0] !== '/') {
destinationPath = process.cwd() + '/' + destinationPath };
sourcePath = sourcePaths.shift();
posix.stat(sourcePath)
.addCallback(function (source) {
posix.stat(destinationPath)
.addCallback(function (destination) {
if (!destination.isFile()) {
var sourcePathBits = sourcePath.split('/'),
sourceFileName = sourcePathBits[sourcePathBits.length - 1],
destinationFileName;
// Why don’t we have splice() on String? I mean, I wouldn’t
// exactly expect something as easy as Ruby’s #gsub, but… come
// on. We have splice() on Array, even! )-:<
var idx = sourcePath.indexOf(".jess");
if (idx) { destinationFileName = sourceFileName.slice(0, idx) + ".js"
+ sourceFileName.slice(idx + 5, sourceFileName.length) };
var destinationFile = destinationPath + '/' + destinationFileName;
} else { var destinationFile = destinationPath };
posix.open(destinationFile,
process.O_WRONLY |process.O_CREAT |process.O_TRUNC, source.mode)
.addCallback(function (fd) {
// FIXME: WOO!
var data = "woo!";
posix.write(fd, data, null, "utf8")
.addCallback(function (written) {
// FIXME: For some reason, on subsequent files, `written`
// is actually *the name of the file*. This is
// completely contrary to the docs.
// if (written !== data.length) {
// throw new(Error)(
// "Write to destination file partially failed (" +
// written + " of " + data.length + ")")
// } else {
if (sourcePaths.length < 1) {
process.stdio.write("-- No paths left" + '\n');
compilation.emitSuccess("woo");
} else {
process.stdio.write("-- Paths left: " + JSON.stringify(sourcePaths) + '\n');
sourcePaths.push(destinationPath);
me.apply(this, sourcePaths);
}
// }
})
.addErrback(function () {
throw new(Error)("Cannot write to destination file") });
})
.addErrback(function () {
throw new(Error)("Cannot open destination file") });
})
.addErrback(function () {
// This clusterfuck creates the destination as a file, and then
// recurses upon itself to fall into the above callback.
// FIXME: This is really messy; we’re creating the file, and then
// truncating it in the above callback… seems inefficient.
posix.open(destinationPath, process.O_CREAT, source.mode)
.addCallback(function (fd) {
posix.close(fd)
.addCallback(function () {
sourcePaths.unshift(sourcePath);
sourcePaths.push(destinationPath);
me.apply(this, sourcePaths);
})
.addErrback(function () {
throw new(Error)("Cannot close destination file") });
})
.addErrback(function () {
throw new(Error)("Cannot create file at destination") });
});
})
.addErrback(function () {
throw new(Error)("Source file doesn’t exist") });
return compilation;
};
if (process.ARGV[1] === __filename) {
jessc.compileFile.apply(jessc,
Array.prototype.slice.apply(process.ARGV, [0])
.splice(2, process.ARGV.length - 2) ).wait() };
return jessc;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment