Permalink
Browse files

[api dist] Now hashbangifies in place; supports arbitrary interpreter…

…s; v0.1.0.
  • Loading branch information...
1 parent bdd233a commit 95bed30489d3a60056d2e0e6fb60077ae89008fc @AvianFlu committed Aug 23, 2011
Showing with 29 additions and 17 deletions.
  1. +4 −3 bin/hashbangify
  2. +23 −12 lib/hashbangify.js
  3. +2 −2 package.json
View
@@ -2,10 +2,11 @@
var hashbangify = require('../lib/hashbangify');
-if (!(process.argv[2] && process.argv[3])) {
- console.error('Usage: hashbangify oldfile newfile');
+if (!process.argv[2]) {
+ console.error('Usage: hashbangify [file] [interpreter]');
+ console.error('[interpreter] Defaults to node.');
process.exit(1);
}
-hashbangify.hashbangify(process.argv[2], process.argv[3]);
+hashbangify.hashbangify(process.argv[2], process.argv[3] || 'node');
View
@@ -1,30 +1,41 @@
var fs = require('fs'),
childProcess = require('child_process');
-var hashbangify = exports.hashbangify = function (oldfile, newfile) {
+var hashbangify = exports.hashbangify = function (file, interpreter) {
getNodePath();
+ var path = '';
+
function getNodePath() {
- childProcess.exec('which node', function (err, stdout) {
+ childProcess.exec('which ' + interpreter, function (err, stdout) {
if (err) {
console.error(err.stack);
- console.error('Could not obtain node location: exiting.');
- return;
+ console.error('Could not obtain interpreter location: exiting.');
+ process.exit(1);
+ }
+ hashBang = '#!' + stdout;
+ fs.readFile(file, gotScript);
+ });
+ }
+
+ function gotScript(err, data) {
+ fs.unlink(file, function (err) {
+ if(err) {
+ console.error(err.stack);
+ process.exit(1);
}
- addHashBang(stdout);
+ addHashBang(file, data);
});
}
- function addHashBang(path) {
- var writeStream = fs.createWriteStream(newfile),
- readStream = fs.createReadStream(oldfile);
- path = '#!' + path;
- writeStream.write(path + '\n');
- readStream.pipe(writeStream);
+ function addHashBang(file, data) {
+ var writeStream = fs.createWriteStream(file);
+ writeStream.write(hashBang + '\n');
+ writeStream.end(data);
}
process.on('exit', function () {
- fs.chmodSync(newfile, 0755);
+ fs.chmodSync(file, 0755);
});
}
View
@@ -1,7 +1,7 @@
{
"name" : "hashbangify",
"preferGlobal": "true",
- "version" : "0.0.0",
+ "version" : "0.1.0",
"author": "AvianFlu <charlie@charlieistheman.com>",
"description" : "Make your node scripts executable!",
"bin": {
@@ -19,4 +19,4 @@
"engine" : {
"node" : ">=0.4"
}
-}
+}

0 comments on commit 95bed30

Please sign in to comment.