Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit of swake 0.1.0

  • Loading branch information...
commit 82ccf71e8b242b4422d7e7950fe31df86437ad16 0 parents
@tristanls authored
27 README.md
@@ -0,0 +1,27 @@
+swake
+====
+
+`swake` is a simple task execution tool that executes a `swake task`.
+
+## Installation
+
+ npm install swake -g
+
+## Usage
+
+to execute the example `swake` task run the following:
+
+ swake echoer 'print this task' --swake-path /path/to/example/folder
+
+`swake` will treat `--swake-path` as a root folder and will attempt to find a
+file called `echoer.swake.js`. If it is found, `swake` will `require` it and
+run it with the provided arguments ( See: `example/echoer.swake.js` ).
+
+if environment variable `SWAKE_PATH` exists, `swake` will use that as the root
+folder.
+
+ env SWAKE_PATH=/path/to/example/folder echoer 'print this task'
+
+Additionally, it is easy to embed `swake` tasks in a foldier hierarchy. For example:
+
+ swake deeper:echoer 'print this task' --swake-path /path/to/example/folder
79 bin/swake
@@ -0,0 +1,79 @@
+#!/usr/bin/env node
+
+var argv = process.argv.slice( 2 ),
+ arg,
+ args = [],
+ lib_path = __dirname + '/../lib/',
+ logly = require( 'logly' ),
+ options = {},
+ path = require( 'path' ),
+ program = path.basename( __filename ),
+ swake = require( lib_path + 'swake.js' );
+
+logly.name( program );
+
+var usage = [
+ "usage",
+ " " + program + " ( -e SWAKETASKFILE | SWAKETASK ) [OPTIONS] [ARG]*",
+ ""
+ ].join( '\n' ),
+ help = [
+ usage,
+ "options:",
+ " --debug Debug mode",
+ " -e Executes SWAKETASKFILE using provided ARGs",
+ " -h, --help Displays this information",
+ " --swake-path SWAKE_PATH Specifies SWAKE_PATH to use",
+ ""
+ ].join( '\n' );
+
+if ( argv.length == 0 ) {
+ logly.stdout( help );
+ process.exit( 0 );
+}
+
+while ( arg = argv.shift() ) {
+ if ( arg === program ) continue;
+
+ if ( arg[ 0 ] !== '-' ) {
+ args.push( arg );
+ } else {
+ arg = arg.match( /--?(.+)/ )[ 1 ];
+
+ switch ( arg ) {
+ case 'debug':
+ logly.mode( 'debug' );
+ break;
+ case 'e':
+ options.swakeTaskFile = argv.shift();
+ if ( ! options.swakeTaskFile ) {
+ logly.stdout( usage );
+ process.exit( 0 );
+ }
+ options.executionMode = true;
+ break
+ case 'help':
+ case 'h':
+ logly.stdout( help );
+ process.exit( 0 );
+ break;
+ case 'swake-path':
+ options.swakePath = argv.shift();
+ if ( ! options.swakePath ) {
+ logly.stdout( usage );
+ process.exit( 0 );
+ }
+ break;
+ }
+ }
+}
+
+if ( options.executionMode ) {
+ swake.executeSwakeFile( options.swakeTaskFile, args, options );
+} else {
+ if ( args.length < 1 ) {
+ logly.stdout( usage );
+ process.exit( 0 );
+ }
+ swake.swake( args.shift(), args, options );
+}
7 example/deeper/echoer.swake.js
@@ -0,0 +1,7 @@
+var exec = require( 'child_process' ).exec,
+ logly = require( 'logly' );
+
+module.exports = function( text ) {
+ exec( 'echo "deeper echoer: ' + text + ' - ' + new Date().getTime()
+ + '" >> /tmp/echoer.swake.js');
+};
6 example/echoer.swake.js
@@ -0,0 +1,6 @@
+var exec = require( 'child_process' ).exec,
+ logly = require( 'logly' );
+
+module.exports = function( text ) {
+ exec( 'echo "' + text + new Date().getTime() + '" >> /tmp/echoer.swake.js');
+};
62 lib/swake.js
@@ -0,0 +1,62 @@
+var fs = require( 'fs' )
+ , logly = require( 'logly' )
+ , path = require( 'path' )
+ , spawn = require( 'child_process' ).spawn;
+
+exports.version =
+ JSON.parse( fs.readFileSync( __dirname + '/../package.json' ) ).version;
+
+exports.executeSwakeFile = function( swakeTaskFile, args, options ) {
+
+ logly.log( 'swakeTaskFile - ' + swakeTaskFile );
+
+ var swakeTaskModule;
+
+ try {
+ swakeTaskModule = require( swakeTaskFile );
+ swakeTaskModule.apply( swakeTaskModule, args );
+ } catch ( err ) {
+ logly.error( err.message );
+ logly.error( 'Unable to locate swake task file ' + swakeTaskFile );
+ return;
+ }
+
+};
+
+exports.swake = function( task, args, options ) {
+
+ // set swake path from environment or options
+ var swakePath = process.env.SWAKE_PATH;
+
+ if ( options && options.swakePath ) {
+ logly.debug( 'setting SWAKE_PATH from options to ' + options.swakePath );
+ swakePath = options.swakePath;
+ }
+
+ if ( ! swakePath ) {
+ logly.debug( 'SWAKE_PATH not defined, using process.cwd()' );
+ swakePath = process.cwd();
+ }
+
+ // parse task name ( ex: seed:some:task )
+ var swakeTask = task.split( ':' ).join( '/' );
+
+ swakeTask = path.join( swakePath, '/', swakeTask + '.swake.js' );
+
+ logly.debug( 'loading swakeTaskModule - ' + swakeTask );
+
+ try {
+ require( swakeTask );
+ } catch ( err ) {
+ logly.error( 'Unable to locate swake task ' + task );
+ logly.error( err.message );
+ return;
+ }
+
+ // we loaded swakeTask via 'require'
+ // run swake with -e ( execute task ) option in a separate process
+ args.unshift( swakeTask );
+ args.unshift( '-e' );
+ logly.debug( 'launching new swake process with ' + args );
+ spawn( 'swake', args );
+};
13 package.json
@@ -0,0 +1,13 @@
+{ "name" : "swake"
+, "description" : "Task execution tool used by swarm"
+, "version" : "0.1.0"
+, "homepage" : "https://github.com/tristanls/swake"
+, "author" : "Tristan Slominski <tristan.slominski@gmail.com> (http://github.com/tristanls)"
+, "bin" : "./bin/swake"
+, "repository" :
+ { "type" : "git"
+ , "url" : "http://github.com/tristanls/swake.git" }
+, "dependencies" :
+ { "logly" : "1.x.x" }
+, "engines" : { "node" : "0.4.x" }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.