Socket
Socket
Sign inDemoInstall

anvil.cdnjs

Package Overview
Dependencies
47
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.3 to 0.1.0

242

lib/plugin.js

@@ -13,2 +13,4 @@ var request = require( "request" );

["--cdnjs:search [name]", "Install a cdnjs file."],
["--cdnjs:update [name]", "Update a cdnjs file."],
["-v [version]", "The version of the cdnjs package to install" ],
["-o, --output [output]", "Output directory."]

@@ -18,3 +20,5 @@ ],

packageName: "",
output: "ext",
config: {
output: "src/vendor"
},
baseUrl: "http://cdnjs.cloudflare.com/ajax/libs/",

@@ -24,12 +28,14 @@ // Configure all the things...

if( command["cdnjs:install"] ) {
this.packageName = command[ "cdnjs:install" ];
if( command[ "cdnjs:install" ] ) {
this.config.packageName = command[ "cdnjs:install" ];
this.command = "install";
}
if( command["cdnjs:search"] ) {
this.search = command[ "cdnjs:search" ];
else if( command[ "cdnjs:search" ] ) {
this.query = command[ "cdnjs:search" ];
this.command = "search";
}
if( command["output"] ) {
this.output = command[ "output" ];
if( command[ "output" ] ) {
this.config.output = command[ "output" ];
}

@@ -41,12 +47,8 @@

run: function( done ) {
var libs, pkg, url;
var pkg, url;
if ( !this.search && !this.packageName ) {
if ( !this.query && !this.config.packageName ) {
done();
return;
}
if ( _.isArray(this.packageName) ) {
this.packageName = this.packageName.pop();
}

@@ -59,86 +61,180 @@ request( this.url, function( err, response, body ) {

libs = JSON.parse( body ).packages;
this.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;
if ( this[ this.command ] ) {
this[ this.command ].call( this, done );
}
else {
done();
}
request( url, function( err, response, body ) {
this.getPkg( err, response, body, done );
}.bind( this ));
}.bind( this ));
},
install: function( done ) {
var pkg, url, packages = [];
}
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 ));
// Needs to be explicitly true, basically means they left out the package name.
if ( this.config.packageName === true ) {
_.each( this.config.libs, function( pkg, libName ) {
var cdnjsPackage = _.find( this.libs, function( cdnjsLib ) {
return cdnjsLib.name === libName;
});
if ( cdnjsPackage ) {
packages.push( cdnjsPackage );
}
}.bind( this ));
_.each( pkg, function( p ) {
anvil.log.complete( p.name );
if ( packages.length ) {
anvil.scheduler.parallel( packages, this.installPackage, function() {
anvil.log.complete( "anvil.cdnjs: libraries installed" );
anvil.raise( "all.stop", 0 );
done();
});
anvil.raise( "all.stop", 0 );
done();
}
else {
anvil.log.error( "anvil.cdnjs: No libaries present in config are available on cdnjs" );
anvil.raise( "all.stop", 0 );
done();
}
}
else if ( this.config.packageName ) {
pkg = _.find( this.libs, function(lib) {
return lib.name === this.config.packageName;
}.bind(this));
this.installPackage( pkg, function() {
anvil.raise( "all.stop", 0 );
done();
});
}
},
installPackage: function( pkg, done ) {
var url;
if( typeof pkg === "undefined" ) {
anvil.log.error( "anvil.cdnjs: No library with named: " + this.config.packageName + " exists on cdnjs." );
anvil.raise( "all.stop", 0 );
done();
return;
}
url = this.baseUrl + pkg.name + "/" + pkg.version + "/" + pkg.filename;
pkg.url = url;
request( url, function( err, response, body ) {
this.getPkg( err, body, pkg, done );
}.bind( this ));
},
getPkg: function( err, response, body, done ) {
var target,
self = this;
anvil.fs.ensurePath( this.output, function( err ) {
search: function( done ) {
var pkg;
pkg = _.filter( this.libs, function( lib ) {
if( !lib.name ) {
return;
}
return lib.name === this.query || ~lib.name.indexOf(this.query);
}.bind( this ));
_.each( pkg, function( p ) {
anvil.log.complete( p.name );
});
anvil.raise( "all.stop", 0 );
done();
},
getPkg: function( err, body, pkg, done ) {
var target;
anvil.fs.ensurePath( this.config.output, function( err ) {
if( err ) {
anvil.log.error( err );
anvil.log.error( "anvil.cdnjs:" + err );
done();
}
target = this.output + "/" + this.pkg.filename;
target = anvil.fs.buildPath( [ this.config.output, pkg.filename ] );
anvil.fs.write( target, body, function( err) {
if( err ) {
anvil.log.error( err );
anvil.log.error( "anvil.cdnjs:" + err );
done();
}
anvil.log.complete( this.pkg.filename + " has been installed to " + target );
//this.updatePackageList( done );
}.bind( this ));
anvil.log.complete( pkg.filename + " has been installed to " + target );
}.bind( self ));
if ( !anvil.fs.pathExists( "./build.json" ) ) {
anvil.fs.write( "./build.json", "{}", function() {
this.updateBuild( pkg, done );
}.bind(this));
}
this.updateBuild( pkg, done );
}.bind( this )); // anvil.fs.write
}.bind( this )); // anvil.fs.ensurePath
},
updatePackageList: function( done ) {
anvil.fs.ensurePath( ".anvil", function( err ) {
this._error( err, done );
anvil.fs.read( ".anvil/cdnjs.json", function( content, err ) {
this._error( err, done );
anvil.raise( "all.stop", 0 );
done();
}.bind( this ));
}.bind( this ));
compareVersion: function( a, b ) {
var a_components = a.split("."),
b_components = b.split("."),
len, i;
if ( a === b ) {
return 0;
}
len = Math.min( a_components.length, b_components.length );
// loop while the components are equal
for ( i = 0; i < len; i++ ) {
// A bigger than B
if ( +a_components[ i ] > +b_components[ i ] ) {
return 1;
}
// B bigger than A
if ( +a_components[ i ] < +b_components[ i ] )
{
return -1;
}
}
// If one's a prefix of the other, the longer one is greater.
if ( a_components.length > b_components.length ) {
return 1;
}
if ( a_components.length < b_components.length ) {
return -1;
}
// Otherwise they are the same.
return 0;
},
_error: function( err, done ) {
if( err ) {
anvil.log.error( err );
updateBuild: function( pkg, done ) {
anvil.fs.transform( "./build.json", function( contents, transform ) {
var build = JSON.parse( contents ),
libs, buildPkg;
libs = (build[ "anvil.cdnjs" ] = build[ "anvil.cdnjs" ] || {
libs: {}
}).libs;
buildPkg = libs[ pkg.name ] = libs[ pkg.name ] || {};
// If the current version is older than the version being installed.
if ( _.isEmpty( buildPkg ) ||
( buildPkg.version && this.compareVersion( pkg.version, buildPkg.version ) === 1 ) ) {
if ( !_.isEmpty( buildPkg ) ) {
buildPkg.history = buildPkg.history || [];
buildPkg.history.push({
version: pkg.version,
url: pkg.url
});
}
buildPkg.version = pkg.version;
buildPkg.url = pkg.url;
}
transform( JSON.stringify( build, null, 4 ) );
}.bind( this ), "./build.json", function() {
// All done...
done();
}
}.bind( this )); // anvil.fs.transform
}

@@ -145,0 +241,0 @@ });

{
"name": "anvil.cdnjs",
"version": "0.0.3",
"version": "0.1.0",
"description": "Install packages from cdnjs.",

@@ -5,0 +5,0 @@ "repository": {

@@ -13,2 +13,4 @@ var request = require( "request" );

["--cdnjs:search [name]", "Install a cdnjs file."],
["--cdnjs:update [name]", "Update a cdnjs file."],
["-v [version]", "The version of the cdnjs package to install" ],
["-o, --output [output]", "Output directory."]

@@ -18,3 +20,5 @@ ],

packageName: "",
output: "ext",
config: {
output: "src/vendor"
},
baseUrl: "http://cdnjs.cloudflare.com/ajax/libs/",

@@ -24,12 +28,14 @@ // Configure all the things...

if( command["cdnjs:install"] ) {
this.packageName = command[ "cdnjs:install" ];
if( command[ "cdnjs:install" ] ) {
this.config.packageName = command[ "cdnjs:install" ];
this.command = "install";
}
if( command["cdnjs:search"] ) {
this.search = command[ "cdnjs:search" ];
else if( command[ "cdnjs:search" ] ) {
this.query = command[ "cdnjs:search" ];
this.command = "search";
}
if( command["output"] ) {
this.output = command[ "output" ];
if( command[ "output" ] ) {
this.config.output = command[ "output" ];
}

@@ -41,12 +47,8 @@

run: function( done ) {
var libs, pkg, url;
var pkg, url;
if ( !this.search && !this.packageName ) {
if ( !this.query && !this.config.packageName ) {
done();
return;
}
if ( _.isArray(this.packageName) ) {
this.packageName = this.packageName.pop();
}

@@ -59,86 +61,180 @@ request( this.url, function( err, response, body ) {

libs = JSON.parse( body ).packages;
this.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;
if ( this[ this.command ] ) {
this[ this.command ].call( this, done );
}
else {
done();
}
request( url, function( err, response, body ) {
this.getPkg( err, response, body, done );
}.bind( this ));
}.bind( this ));
},
install: function( done ) {
var pkg, url, packages = [];
}
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 ));
// Needs to be explicitly true, basically means they left out the package name.
if ( this.config.packageName === true ) {
_.each( this.config.libs, function( pkg, libName ) {
var cdnjsPackage = _.find( this.libs, function( cdnjsLib ) {
return cdnjsLib.name === libName;
});
if ( cdnjsPackage ) {
packages.push( cdnjsPackage );
}
}.bind( this ));
_.each( pkg, function( p ) {
anvil.log.complete( p.name );
if ( packages.length ) {
anvil.scheduler.parallel( packages, this.installPackage, function() {
anvil.log.complete( "anvil.cdnjs: libraries installed" );
anvil.raise( "all.stop", 0 );
done();
});
anvil.raise( "all.stop", 0 );
done();
}
else {
anvil.log.error( "anvil.cdnjs: No libaries present in config are available on cdnjs" );
anvil.raise( "all.stop", 0 );
done();
}
}
else if ( this.config.packageName ) {
pkg = _.find( this.libs, function(lib) {
return lib.name === this.config.packageName;
}.bind(this));
this.installPackage( pkg, function() {
anvil.raise( "all.stop", 0 );
done();
});
}
},
installPackage: function( pkg, done ) {
var url;
if( typeof pkg === "undefined" ) {
anvil.log.error( "anvil.cdnjs: No library with named: " + this.config.packageName + " exists on cdnjs." );
anvil.raise( "all.stop", 0 );
done();
return;
}
url = this.baseUrl + pkg.name + "/" + pkg.version + "/" + pkg.filename;
pkg.url = url;
request( url, function( err, response, body ) {
this.getPkg( err, body, pkg, done );
}.bind( this ));
},
getPkg: function( err, response, body, done ) {
var target,
self = this;
anvil.fs.ensurePath( this.output, function( err ) {
search: function( done ) {
var pkg;
pkg = _.filter( this.libs, function( lib ) {
if( !lib.name ) {
return;
}
return lib.name === this.query || ~lib.name.indexOf(this.query);
}.bind( this ));
_.each( pkg, function( p ) {
anvil.log.complete( p.name );
});
anvil.raise( "all.stop", 0 );
done();
},
getPkg: function( err, body, pkg, done ) {
var target;
anvil.fs.ensurePath( this.config.output, function( err ) {
if( err ) {
anvil.log.error( err );
anvil.log.error( "anvil.cdnjs:" + err );
done();
}
target = this.output + "/" + this.pkg.filename;
target = anvil.fs.buildPath( [ this.config.output, pkg.filename ] );
anvil.fs.write( target, body, function( err) {
if( err ) {
anvil.log.error( err );
anvil.log.error( "anvil.cdnjs:" + err );
done();
}
anvil.log.complete( this.pkg.filename + " has been installed to " + target );
//this.updatePackageList( done );
}.bind( this ));
anvil.log.complete( pkg.filename + " has been installed to " + target );
}.bind( self ));
if ( !anvil.fs.pathExists( "./build.json" ) ) {
anvil.fs.write( "./build.json", "{}", function() {
this.updateBuild( pkg, done );
}.bind(this));
}
this.updateBuild( pkg, done );
}.bind( this )); // anvil.fs.write
}.bind( this )); // anvil.fs.ensurePath
},
updatePackageList: function( done ) {
anvil.fs.ensurePath( ".anvil", function( err ) {
this._error( err, done );
anvil.fs.read( ".anvil/cdnjs.json", function( content, err ) {
this._error( err, done );
anvil.raise( "all.stop", 0 );
done();
}.bind( this ));
}.bind( this ));
compareVersion: function( a, b ) {
var a_components = a.split("."),
b_components = b.split("."),
len, i;
if ( a === b ) {
return 0;
}
len = Math.min( a_components.length, b_components.length );
// loop while the components are equal
for ( i = 0; i < len; i++ ) {
// A bigger than B
if ( +a_components[ i ] > +b_components[ i ] ) {
return 1;
}
// B bigger than A
if ( +a_components[ i ] < +b_components[ i ] )
{
return -1;
}
}
// If one's a prefix of the other, the longer one is greater.
if ( a_components.length > b_components.length ) {
return 1;
}
if ( a_components.length < b_components.length ) {
return -1;
}
// Otherwise they are the same.
return 0;
},
_error: function( err, done ) {
if( err ) {
anvil.log.error( err );
updateBuild: function( pkg, done ) {
anvil.fs.transform( "./build.json", function( contents, transform ) {
var build = JSON.parse( contents ),
libs, buildPkg;
libs = (build[ "anvil.cdnjs" ] = build[ "anvil.cdnjs" ] || {
libs: {}
}).libs;
buildPkg = libs[ pkg.name ] = libs[ pkg.name ] || {};
// If the current version is older than the version being installed.
if ( _.isEmpty( buildPkg ) ||
( buildPkg.version && this.compareVersion( pkg.version, buildPkg.version ) === 1 ) ) {
if ( !_.isEmpty( buildPkg ) ) {
buildPkg.history = buildPkg.history || [];
buildPkg.history.push({
version: pkg.version,
url: pkg.url
});
}
buildPkg.version = pkg.version;
buildPkg.url = pkg.url;
}
transform( JSON.stringify( build, null, 4 ) );
}.bind( this ), "./build.json", function() {
// All done...
done();
}
}.bind( this )); // anvil.fs.transform
}

@@ -145,0 +241,0 @@ });

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc