Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
230 changes: 0 additions & 230 deletions bin/ngbp

This file was deleted.

129 changes: 129 additions & 0 deletions bin/warlock
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#!/usr/bin/env node

process.title = 'warlock';

// Node.js libs
var READLINE = require( 'readline' );
var SPAWN = require( 'child_process' ).spawn;

// 3rd Party Modules
var Q = require( 'q' );
var COLORS = require( 'colors' );
var FINDUP = require('findup-sync');
var RESOLVE = require('resolve').sync;

// Local libs
var PKG = require( './../package.json' );

function log ( msg ) {
console.log( "[warlock] " + msg );
}

function writeln ( msg ) {
console.log( msg );
}

function error ( msg, fatal ) {
msg = "[warlock] " + msg;
console.log( msg.red );

if ( fatal ) {
process.exit( 1 );
}
}

function header ( msg ) {
msg = "[warlock] " + msg;
console.log( msg.underline.magenta );
}

function installWarlockIfNeeded () {
var deferred = Q.defer();

/**
* Ensure there is a local installation of warlock.
*/
try {
warlockPath = RESOLVE( 'warlock', { basedir: process.cwd() } );
deferred.resolve( warlockPath );
} catch ( e ) {
error( "Encountered error: " + e );
warlockPath = FINDUP( 'lib/warlock.js' );

// No warlock install found!
if ( warlockPath ) {
deferred.resolve( warlockPath );
} else {
header( "Local warlock not installed." );

// Ensure the user is okay with installing it here.
var rl = READLINE.createInterface({
input: process.stdin,
output: process.stdout
});

rl.question( "Install warlock in the current directory? [Yn] ", function ( answer ) {
rl.close();

if ( answer && [ 'y', 'Y', 'yes', 'Yes', 'YES' ].indexOf( answer ) === -1 ) {
deferred.reject( "I can't function without a local warlock installation." );
return;
}

/**
* This will install warlock and its dependencies locally, and if there is a `package.json`
* file it will save the requirement to the `devDependencies` array.
*/
var npm = SPAWN( 'npm', [ 'link', '--save-dev', 'warlock' ], {
// var npm = SPAWN( 'npm', [ 'install', '--save-dev', 'warlock' ], {
cwd: process.cwd()
});

npm.stderr.on( 'data', function ( data ) {
if (/^execvp\(\)/.test( data ) ) {
deferred.reject( "Could not start NPM." );
}
});

npm.stdout.pipe( process.stdout );
npm.stderr.pipe( process.stderr );

npm.on( 'close', function ( code ) {
if ( code === 0 ) {
deferred.resolve( RESOLVE( 'warlock', { basedir: process.cwd() } ) );
} else {
deferred.reject( "NPM exited with status " + code + "." );
}
});
});
}
}

return deferred.promise;
}

/**
* Print out the version, if necessary.
*/
if( process.argv.indexOf( '--version' ) !== -1 || process.argv.indexOf( '-V' ) !== -1 ) {
writeln( "warlock-cli v" + PKG.version );
}

/**
* Ensure that warlock is installed, then load the CLI.
*/
installWarlockIfNeeded()
.then( function ( warlockPath ) {
return require( warlockPath ).cli();
})
.done( function () {
// FIXME(jdm): I'm almost certain this shouldn't be necessary...
process.exit();
}, function ( err ) {
if ( err.stack ) {
console.log( err.stack );
}

error( "Well, shoot-darn. " + err + " Exiting...", true );
});

Loading