Skip to content

Commit

Permalink
[api dist] Now hashbangifies in place; supports arbitrary interpreter…
Browse files Browse the repository at this point in the history
…s; v0.1.0.
  • Loading branch information
AvianFlu committed Aug 23, 2011
1 parent bdd233a commit 95bed30
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
7 changes: 4 additions & 3 deletions bin/hashbangify
Expand Up @@ -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');

35 changes: 23 additions & 12 deletions lib/hashbangify.js
@@ -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);
});

}
Expand Down
4 changes: 2 additions & 2 deletions package.json
@@ -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": {
Expand All @@ -19,4 +19,4 @@
"engine" : {
"node" : ">=0.4"
}
}
}

0 comments on commit 95bed30

Please sign in to comment.