Created
January 4, 2013 18:17
-
-
Save jcreamer898/4454672 to your computer and use it in GitHub Desktop.
anvil.cdnjs changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require( "request" ); | |
var path = require( "path" ); | |
var pluginFactory = function(_, anvil) { | |
var root = path.resolve( __dirname, "../" ); | |
return anvil.plugin({ | |
// Name your plugin | |
name: "anvil.cdnjs", | |
// Activity list: "identify", "pull", "combine", "pre-process","compile", "post-process", "push", "test" | |
activity: "identify", | |
// Command all the things [ "-s, --somecommand", "Run this plugin for awesomesauce" ] | |
commander: [ | |
["--cdnjs:install [name] [version]", "Install a cdnjs file."], | |
["--cdnjs:search [name]", "Install a cdnjs file."], | |
["--cdnjs:update [name]", "Update a cdnjs file."], | |
["-o, --output [output]", "Output directory."] | |
], | |
url: "http://cdnjs.com/packages.json", | |
packageName: "", | |
config: { | |
output: "ext" | |
}, | |
baseUrl: "http://cdnjs.cloudflare.com/ajax/libs/", | |
// Configure all the things... | |
configure: function( config, command, done ) { | |
if( command["cdnjs:install"] ) { | |
this.packageName = command[ "cdnjs:install" ][0]; | |
} | |
if( command["cdnjs:search"] ) { | |
this.search = command[ "cdnjs:search" ]; | |
} | |
if( command["output"] ) { | |
this.config.output = command[ "output" ]; | |
} | |
done(); | |
}, | |
// Run all the things... | |
run: function( done ) { | |
var libs, pkg, url; | |
if ( !this.search && !this.packageName ) { | |
done(); | |
return; | |
} | |
request( this.url, function( err, response, body ) { | |
if( err || response.statusCode !== 200 ) { | |
anvil.log.error( err ); | |
done(); | |
} | |
libs = JSON.parse( body ).packages; | |
if ( this.packageName ) { | |
pkg = _.find( libs, function(lib) { | |
return lib.name === this.packageName; | |
}.bind(this)); | |
if( typeof pkg === "undefined" ) { | |
anvil.log.error( "anvil.cdnjs: No library with named: " + this.packageName + " exists on cdnjs." ); | |
anvil.raise( "all.stop", 0 ); | |
done(); | |
return; | |
} | |
url = this.baseUrl + pkg.name + "/" + pkg.version + "/" + pkg.filename; | |
this.pkg = pkg; | |
this.pkg.url = url; | |
request( url, function( err, response, body ) { | |
this.getPkg( err, response, body, done ); | |
}.bind( this )); | |
} | |
else if ( this.search ) { | |
pkg = _.filter( libs, function( lib ) { | |
if( !lib.name ) { | |
return; | |
} | |
return lib.name === this.search || ~lib.name.indexOf(this.search); | |
}.bind( this )); | |
_.each( pkg, function( p ) { | |
anvil.log.complete( p.name ); | |
}); | |
anvil.raise( "all.stop", 0 ); | |
done(); | |
} | |
else { | |
done(); | |
} | |
}.bind( this )); | |
}, | |
getPkg: function( err, response, body, done ) { | |
var target; | |
anvil.fs.ensurePath( this.config.output, function( err ) { | |
if( err ) { | |
anvil.log.error( err ); | |
done(); | |
} | |
target = this.config.output + "/" + this.pkg.filename; | |
anvil.fs.write( target, body, function( err) { | |
if( err ) { | |
anvil.log.error( err ); | |
done(); | |
} | |
anvil.log.complete( this.pkg.filename + " has been installed to " + target ); | |
if ( !this.config[ this.pkg.filename ] ) { | |
this.config[ this.pkg.filename ] = {}; | |
} | |
this.config[ this.pkg.filename ][ "version" ] = this.pkg.version; | |
this.config[ this.pkg.filename ][ "url" ] = this.pkg.url; | |
anvil.fs.read( "./build.json", function( contents, err ) { | |
console.log( contents ); | |
done(); | |
}); | |
console.log( this.config ); | |
anvil.raise( "all.stop", 0 ); | |
}.bind( this )); | |
}.bind( this )); | |
} | |
}); | |
}; | |
module.exports = pluginFactory; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment