Skip to content

Commit

Permalink
Globby3 (#93)
Browse files Browse the repository at this point in the history
Use file globbing
  • Loading branch information
Hypercubed committed Oct 10, 2016
1 parent 5b8a253 commit 79c5824
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 138 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module.exports = function( projectPath ) {
try {
var credits = readDirectory( projectPath, analyzers );
} catch( e ) {
/* istanbul ignore next */
return reject( e );
}

Expand Down
16 changes: 6 additions & 10 deletions lib/analyzers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var fs = require( 'fs' );
var path = require( 'path' );
var globby = require( 'globby' );

/**
* Get all available analyzers
Expand All @@ -15,16 +16,11 @@ function getAnalyzers( config ) {
var methods = {};
var basePath = config.filePaths.analyzers;

try {
var analyzers = fs.readdirSync( basePath );
} catch( error ) {
console.log( error );
throw new Error( 'filePaths \'analyzers\' does not exist' );
}

analyzers.forEach( function( analyzer ) {
methods[ analyzer ] = require( path.join( basePath, analyzer, 'index' ) );
} );
globby.sync( `${basePath}/*` )
.forEach( function( analyzer ) {
var name = path.basename( analyzer );
methods[ name ] = require( analyzer );
} );

return methods;
}
Expand Down
37 changes: 12 additions & 25 deletions lib/analyzers/bower/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var fs = require( 'fs' );
var path = require( 'path' );
var globby = require( 'globby' );

var creditUtil = require( '../../credit' );
var packageUtil = require( '../../package' );
Expand All @@ -19,33 +20,19 @@ function getCredits( projectPath ) {

var depPath = path.join( projectPath, 'bower_components' );

// projects without dependencies won't have
// a bower_components folder
try {
var deps = fs.readdirSync( depPath );
} catch( error ) {
return credits;
}
globby.sync( [ `${depPath}/*/bower.json` ] )
.forEach( function( bowerJsonPath ) {
var name = path.basename( path.dirname( bowerJsonPath ) );
var bowerJson = require( bowerJsonPath );

deps.forEach( function( name ) {
var bowerJsonPath = path.join( depPath, name, 'bower.json' );
var authors = packageUtil.getAuthor( bowerJson );

// it might possible that some modules are missing
// the bower.json file
try {
var bowerJson = require( bowerJsonPath );
} catch( error ) {
return credits;
}

var authors = packageUtil.getAuthor( bowerJson );

if ( authors ) {
authors.forEach( function( author ) {
credits = creditUtil.addCreditToCredits( credits, author, name );
} );
}
} );
if ( authors ) {
authors.forEach( function( author ) {
credits = creditUtil.addCreditToCredits( credits, author, name );
} );
}
} );

return credits;
}
Expand Down
71 changes: 23 additions & 48 deletions lib/analyzers/jspm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,11 @@

var fs = require( 'fs' );
var path = require( 'path' );
var globby = require( 'globby' );

var creditUtil = require( '../../credit' );
var packageUtil = require( '../../package' );


/**
* Helper function for reading a specific jspm category
*
* @param {String} projectPath absolute path to project root
* @param {String} category category name
*
* @return {Array} list of credits
*/
function getCreditsPerCategory( projectPath, category ) {
var credits = [];

var depPath = path.join( projectPath, 'jspm_packages/' + category );

// projects without dependencies won't have
// a jspm_packages folder
try {
var deps = fs.readdirSync( depPath );
} catch( error ) {
return credits;
}

deps.forEach( function( name ) {
if ( name !== '.bin' && name.indexOf( '.js' ) === -1 ) {
var packageJson = require( path.join( depPath, name, 'package.json' ) );
var author = packageUtil.getAuthor( packageJson );
var maintainers = packageUtil.getMaintainers( packageJson );

if ( author ) {
credits = creditUtil.addCreditToCredits( credits, author, name );
}

if ( maintainers ) {
maintainers.forEach( function( maintainer ) {
credits = creditUtil.addCreditToCredits( credits, maintainer, name );
} );
}
}
} );

return credits;
}

/**
* Read project root and evaluate dependency credits for jspm modules
*
Expand All @@ -58,11 +16,28 @@ function getCreditsPerCategory( projectPath, category ) {
*/
function getCredits( projectPath ) {
var credits = [];
var categories = [ 'npm', 'github/components' ];

categories.forEach( function( category ) {
credits = credits.concat( getCreditsPerCategory( projectPath, category ) );
} );
var jspmPath = path.join( projectPath, 'jspm_packages' );

globby.sync( [ `${jspmPath}/npm/*/package.json`, `${jspmPath}/github/*/*/package.json` ] )
.forEach( function( packagePath ) {
var name = path.basename( path.dirname( packagePath ) );

if ( name[ 0 ] !== '.' && name.indexOf( '.js' ) === -1 ) {
var packageJson = require( path.join( packagePath ) );
var author = packageUtil.getAuthor( packageJson );
var maintainers = packageUtil.getMaintainers( packageJson );

if ( author ) {
credits = creditUtil.addCreditToCredits( credits, author, name );
}

if ( maintainers ) {
maintainers.forEach( function( maintainer ) {
credits = creditUtil.addCreditToCredits( credits, maintainer, name );
} );
}
}
} );

return credits;
}
Expand Down
72 changes: 28 additions & 44 deletions lib/analyzers/npm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,52 @@

var fs = require( 'fs' );
var path = require( 'path' );
var globby = require( 'globby' );

var creditUtil = require( '../../credit' );
var packageUtil = require( '../../package' );


/**
* Read project root and evaluate dependency credits for node modules
* -> this will be run recursively
*
* @param {String} projectPath absolute path to project root
* @param {Array|undefined} credits list of credits
* @param {Array|undefined} seen list of already iterated packages
*
* @return {Array} list of credits
*/
function getCredits( projectPath, credits, seen ) {
function getCredits( projectPath, credits ) {
credits = credits || [];
seen = seen || [];

if ( seen[ projectPath ] ) {
return credits;
}

seen[ projectPath ] = true;

var depPath = path.join( projectPath, 'node_modules' );

// projects without dependencies won't have
// a node_modules folder
try {
var deps = fs.readdirSync( depPath );
} catch( error ) {
return credits;
}

deps.forEach( function( name ) {
var directoryPath = path.join( depPath, name );

if (
name[ 0 ] !== '.' &&
(
fs.lstatSync( directoryPath ).isDirectory() ||
fs.lstatSync( directoryPath ).isSymbolicLink()
)
) {
var packageJson = require( path.join( directoryPath, 'package.json' ) );
var author = packageUtil.getAuthor( packageJson );
var maintainers = packageUtil.getMaintainers( packageJson );

if ( author ) {
credits = creditUtil.addCreditToCredits( credits, author, name );
}

if ( maintainers ) {
maintainers.forEach( function( maintainer ) {
credits = creditUtil.addCreditToCredits( credits, maintainer, name );
} );
globby.sync( `${depPath}/**/package.json` )
.forEach( function( packagePath ) {
var directoryPath = path.dirname( packagePath );
var name = path.basename( path.dirname( packagePath ) );

if (
name[ 0 ] !== '.' &&
(
fs.lstatSync( directoryPath ).isDirectory() ||
fs.lstatSync( directoryPath ).isSymbolicLink()
)
) {
var packageJson = require( packagePath );
var author = packageUtil.getAuthor( packageJson );
var maintainers = packageUtil.getMaintainers( packageJson );

if ( author ) {
credits = creditUtil.addCreditToCredits( credits, author, name );
}

if ( maintainers ) {
maintainers.forEach( function( maintainer ) {
credits = creditUtil.addCreditToCredits( credits, maintainer, name );
} );
}
}

getCredits( fs.realpathSync( directoryPath ), credits, seen );
}
} );
} );

return credits;
}
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"scripts": {
"lint": "eslint lib/*.js lib/**/*.js",
"test": "npm run lint && nyc ava",
"coverage": "nyc ava && nyc report --reporter=html",
"coveralls": "nyc report --reporter=text-lcov | coveralls"
},
"repository": {
Expand All @@ -34,6 +35,7 @@
"dependencies": {
"all-stars": "^1.1.0",
"es6-promise": "^4.0.5",
"globby": "^6.0.0",
"object-assign": "^4.0.1"
}
}
2 changes: 1 addition & 1 deletion test/bower.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import getCredits from '../lib/analyzers/bower';

const fixtures = path.resolve( './fixtures' );

test( 'getCredits - get availalbe bower credits', t => {
test( 'getCredits - get available bower credits', t => {
const credits = getCredits( fixtures );

t.deepEqual( credits[ 0 ].name, 'Alice Bobson' );
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/bower_components/boom/bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"note":"no authors"}
1 change: 1 addition & 0 deletions test/fixtures/jspm_packages/github/calsow/foo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"author":"Don Calsow"}
1 change: 1 addition & 0 deletions test/fixtures/jspm_packages/npm/.bin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"note": "this is ignored", "author":"Alice Bobson"}
1 change: 1 addition & 0 deletions test/fixtures/node_modules/.bin/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test( 'credits - folder exists', t => {
return credits( fixtures )
.then( credits => {
t.deepEqual( credits.npm[ 0 ].name, 'Alice Bobson' );
t.deepEqual( credits.npm[ 0 ].packages, ['bar', 'boom', 'baz'] );
t.deepEqual( credits.npm[ 0 ].packages.sort(), ['bar', 'baz', 'boom'] );
} );
} );

Expand Down
2 changes: 1 addition & 1 deletion test/jspm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import getCredits from '../lib/analyzers/jspm';

const fixtures = path.resolve( './fixtures' );

test( 'getCredits - get availalbe jspm credits', t => {
test( 'getCredits - get available jspm credits', t => {
const credits = getCredits( fixtures );

t.deepEqual( credits[ 0 ].name, 'Alice Bobson' );
Expand Down
16 changes: 8 additions & 8 deletions test/npm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ import getCredits from '../lib/analyzers/npm';

const fixtures = path.resolve( './fixtures' );

test( 'getCredits - get availalbe npm credits', t => {
test( 'getCredits - get available npm credits', t => {
fs.ensureSymlinkSync( `${fixtures}/linked`, `${fixtures}/node_modules/linked` );
fs.ensureSymlinkSync( `${fixtures}/node_modules/cycle`, `${fixtures}/node_modules/cycle/node_modules/cycle` );

const credits = getCredits( fixtures );

t.deepEqual( credits[ 0 ].name, 'Alice Bobson' );
t.deepEqual( credits[ 0 ].packages, [ 'bar', 'boom', 'baz' ] );
t.deepEqual( credits[ 0 ].packages.sort(), [ 'bar', 'baz', 'boom' ] );

t.deepEqual( credits[ 1 ].name, 'Randy Ran' );
t.deepEqual( credits[ 1 ].packages, [ 'baz' ] );
t.deepEqual( credits[ 1 ].name, 'Bob Calsow' );
t.deepEqual( credits[ 1 ].packages.sort(), [ 'boing', 'foo' ] );

t.deepEqual( credits[ 2 ].name, 'Bobby Bob' );
t.deepEqual( credits[ 2 ].name, 'Randy Ran' );
t.deepEqual( credits[ 2 ].packages, [ 'baz' ] );

t.deepEqual( credits[ 3 ].name, 'Bob Calsow' );
t.deepEqual( credits[ 3 ].packages, [ 'boing', 'foo' ] );
t.deepEqual( credits[ 3 ].name, 'Bobby Bob' );
t.deepEqual( credits[ 3 ].packages, [ 'baz' ] );

t.deepEqual( credits[ 4 ].name, 'Bob Loblaw' );
t.deepEqual( credits[ 4 ].packages, [ 'cycle', 'linked' ] );
t.deepEqual( credits[ 4 ].packages.sort(), [ 'cycle', 'linked' ] );
} );

0 comments on commit 79c5824

Please sign in to comment.