Permalink
Browse files

Merge pull request #18 from nodejitsu/coffee

Coffee
  • Loading branch information...
2 parents fb2094a + 315abce commit 23e50de01e2382693d67c20df3a5d4edfd0b29a4 @bmeck bmeck committed Nov 28, 2011
Showing with 40 additions and 20 deletions.
  1. +7 −7 bin/carapace
  2. +9 −9 lib/carapace.js
  3. +21 −0 lib/plugins/coffee.js
  4. +3 −4 test/helper/macros.js
View
@@ -8,8 +8,8 @@ var path = require('path'),
// Extract the carapace CLI arguments for this
// process.
//
-var argv = carapace.cli.argv(),
- script = argv._[0];
+var argv = carapace.cli.argv();
+carapace.script = argv._[0];
function onPluginError (info) {
//
@@ -25,7 +25,7 @@ function onPluginError (info) {
//
// Setup the passthru arguments.
//
-var passthru = carapace.cli.extract(null, script);
+carapace.argv = carapace.cli.extract(null, carapace.script);
//
// Remark: Calling `carapace.listen()` with no
@@ -43,7 +43,7 @@ carapace.start(argv, function () {
//
carapace.on('carapace::plugin::error', onPluginError);
- var plugins = argv.plugin.map(function (plugin) {
+ var plugins = argv.plugin.map(function (plugin) {
return carapace.load(plugin);
});
@@ -71,16 +71,16 @@ carapace.start(argv, function () {
}
function runAndReport () {
- carapace.run(script, passthru, true, function () {
- console.log('carapace has wrapped: ' + script);
+ carapace.run(carapace.script, true, function () {
+ console.log('carapace has wrapped: ' + carapace.script);
function logArray (array, msg, delim) {
return array && array.length
? console.log(msg + array.join(delim))
: null;
}
- logArray(passthru, 'with arguments: ', ' ');
+ logArray(carapace.argv, 'with arguments: ', ' ');
logArray(Object.keys(carapace._module.exports), ' and exports: ', ', ');
});
}
View
@@ -40,6 +40,7 @@ var overrideNet = require('./net');
//
carapace.cli = require('./cli');
carapace.bin = path.join(__dirname, '..', 'bin', 'carapace');
+carapace.argv = [];
//
// Plugins list exposed through path names so that they
@@ -165,24 +166,23 @@ carapace.use = function (plugins, callback) {
// Runs the script in `argv[0]` with the rest of the arguments specified
// in `argv` by transparently rewriting the current `process.argv`.
//
-carapace.run = function (script, argv, override, callback) {
+carapace.run = function (override, callback) {
var error;
Array.prototype.slice.call(arguments).forEach(function (a) {
switch (typeof(a)) {
case 'function': callback = a; break;
- case 'string': script = a; break;
- case 'object': argv = a; break;
case 'boolean': override = a; break;
}
});
- if (!script) {
+ if (!carapace.script) {
error = new Error('Cannot spawn a script with no path.');
}
else if (carapace.running) {
error = new Error('Cannot spawn a new script, one is already running.');
}
+
if (error) {
return callback
@@ -195,7 +195,7 @@ carapace.run = function (script, argv, override, callback) {
// will transparently locate and run the target script
// and it will be completely unaware of the carapace.
//
- carapace.cli.rewrite(script, argv, override);
+ carapace.cli.rewrite(carapace.script, carapace.argv, override);
//
// Start up the hooks for the net module
@@ -213,8 +213,8 @@ carapace.run = function (script, argv, override, callback) {
// the wrapped script, then
//
carapace.wrapped = {
- script: script,
- argv: argv
+ script: carapace.script,
+ argv: carapace.argv
};
process.nextTick(function () {
@@ -251,7 +251,7 @@ carapace.load = function (script) {
return carapace.plugins[name];
}
- if (!~['/'].indexOf(script[0])) {
+ if ('/' !== script[0]) {
//
// If it is not a relative or absolute path, make it absolute
// from the current `process.cwd()`.
@@ -261,7 +261,7 @@ carapace.load = function (script) {
carapace.plugins[name] = script;
carapace.emit('carapace::plugin::loaded', script);
-
+
return script;
};
View
@@ -0,0 +1,21 @@
+var path = require('path'),
+ coffeeBin = '/usr/local/bin/coffee';
+
+module.exports = function coffeePlugin(carapace) {
+ if (!carapace.coffee) {
+ carapace.coffee = function (value, done) {
+ //
+ // This will be immediately called when this plugin is passed to `.use()`
+ //
+ // Should change `process.argv[1]` to coffee and then rewrite the CLI arguments
+ // as necessary so that the .coffee is handled correctly by the `coffee` binary.
+ //
+ var script = carapace.script;
+ if (script.match(/\.coffee$/)) {
+ carapace.script = coffeeBin;
+ carapace.argv.unshift(script);
+ }
+ done();
+ };
+ }
+};
View
@@ -46,9 +46,6 @@ macros.assertUse = function (plugins, vows) {
"should have load the plugin(s)": function () {
assert.isArray(plugins);
names.forEach(function (name) {
- //
- // Remark (drjackal): Hopefully nothing malicious in plugins...
- //
assert.isFunction(carapace[name]);
});
}
@@ -61,7 +58,9 @@ macros.assertRun = function (script, argv, vows) {
var context = {
topic: function () {
carapace.on('carapace::running', this.callback.bind(carapace, null));
- carapace.run(script, argv || []);
+ carapace.argv = argv || [];
+ carapace.script = script;
+ carapace.run();
},
"should fire the `carapace::running` event": function () {
assert.equal(carapace.event, 'carapace::running');

0 comments on commit 23e50de

Please sign in to comment.