Skip to content

Instantly share code, notes, and snippets.

@epowers
Last active January 15, 2016 13:14
Show Gist options
  • Save epowers/6182098 to your computer and use it in GitHub Desktop.
Save epowers/6182098 to your computer and use it in GitHub Desktop.
hello is a self-executing shell script header for node.js; starts as a unix shell script (bash, etc.), checks for node or installs it, runs itself in node, checks for npm or installs it, installs dependencies (i.e. async), then prints a nice hello to standard output and exits.
#!/bin/sh
dummy=0<<0000
/**
* hello is a self-executing node shell script header.
*
* hello starts as a unix shell script (bash, etc.),
* checks for node or installs it, runs itself in node,
* checks for npm or installs it, installs dependencies,
* then prints a nice hello to standard output and exits.
*/
/* Start shell script, Javascript comments out this section.
*
*
0000
# check for node executable.
dummy=`which node`
STATUS=$?
# if not found, install it.
if [ $STATUS -ne 0 ]; then
cat 1>&2 <<EOC
Error: node not found in executable path.
Attempting to install node with package managers...
EOC
# attempt package installers prioritized
# of course, have not tried all of these so please tell me if the command is incorrect
sudo port install nodejs 1>&2 || sudo brew install node 1>&2 || sudo yum --enablerepo=epel install nodejs 1>&2 || sudo apt-get install nodejs 1>&2 || sudo emerge nodejs 1>&2 || sudo zypper in nodejs 1>&2 || pacman -S nodejs 1>&2 || pkg_add -r node-devel 1>&2 || cinst nodejs 1>&2
STATUS=$?
if [ $STATUS -ne 0 ]; then
cat 1>&2 <<EOC
Error: no package manager succeeded... exiting.
EOC
exit $STATUS
fi
fi
# execute node with this script and arguments.
exec node $0 $*
exit $?
# End shell script, should never get here
dummy=<<EOFEOF
*
* Start Javascript
*/
/**** If you do not want to automatically install packages, replace this script to the end with your script. ****/
// install a package with a callback, ala requirejs
function install( requests, cb ) {
function exit( code ) {
try {
process.exit( code );
} catch(e) {
console.error( e.message + "... exit failed." );
throw e;
}
}
if( typeof require === 'undefined' ) {
console.error( "ReferenceError: require is not defined... exiting." );
exit(1);
} else if( typeof require !== 'function' ) {
console.error( "TypeError: require is not a function... exiting." );
exit(1);
} else if( typeof cb !== 'function' ) {
console.error( "Error: No install callback specified for [" + requests.join(', ') + "]... exiting." );
exit(1);
}
function _install( request, _cb ) {
try {
var obj = require( request );
_cb( obj );
} catch( e ) {
if( request === 'child_process' ) {
console.error( e.message + "... exiting." );
exit(1);
} else if( request === 'npm' ) {
console.error( e.message + ". Attempting to install npm with package managers..." );
var cmd = "sudo port install npm 1>&2 || sudo brew install npm 1>&2 || sudo yum --enablerepo=epel install npm 1>&2 || sudo apt-get install npm 1>&2 || sudo emerge npm 1>&2 || sudo zypper in npm 1>&2 || pacman -S npm 1>&2 || pkg_add -r npm 1>&2 || cinst nodejs.install 1>&2";
try {
require( 'child_process' )
.spawn( "sh", ["-c", cmd], {
stdio: 'inherit',
}).on( 'close', function( code ) {
if( code !== 0 ) {
console.error( "Error: Installing " + request + " failed... exiting." );
exit(1);
}
try {
var obj = require( request );
} catch( e ) {
console.error( "Error: Installing " + request + " failed... exiting." );
exit(1);
}
_cb( obj );
});
} catch(e) {
console.error( "Error: Installing " + request + " failed... exiting." );
exit(1);
}
} else {
console.error( e.message + ". Attempting to install with npm..." );
_install( 'npm', function( npm ){
try {
npm.load({}, function( e ) {
try {
npm.commands.install( [request], function( e ) {
if( e ) {
console.error( e.message + "... exiting." );
exit(1);
}
try {
var obj = require( request );
} catch( e ) {
console.error( "Error: Installing " + request + " failed... exiting." );
exit(1);
}
_cb( obj );
});
} catch(e) {
console.error( e.message + "... exiting." );
exit(1);
}
});
} catch(e) {
console.error( e.message + "... exiting." );
exit(1);
}
});
}
}
}
var i = 0,
n = requests.length,
objs = [];
objs.length = n;
function request_install() {
_install( requests[i], function( obj ) {
objs[i] = obj;
if( ++i < n ) {
request_install();
} else {
cb.apply( cb, objs );
}
});
}
if( n ) {
request_install(0);
} else {
cb.apply( cb, objs );
}
}
/* Print hello and exit. */
install(['async'], function(async) {
async.series([
function(done) {
console.log( "Hello, World!" );
done();
},
]);
});
@0x4007
Copy link

0x4007 commented Jan 15, 2016

I appreciate this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment