Created
June 14, 2015 02:03
-
-
Save frenchie4111/ccc0d74ef75b9c3b31fe to your computer and use it in GitHub Desktop.
Get licenses from package.json
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
/* | |
* Note: The code in this gist is in no way meant to be pretty, please do not judge my coding abilities | |
* based on it | |
*/ | |
var package = require( './package.json' ), | |
_ = require( 'underscore' ), | |
q = require( 'q' ), | |
exec = require( 'child_process' ).exec; | |
var license_text = { | |
'MIT': '[MIT License](http://opensource.org/licenses/MIT)', | |
'Apache 2.0': '[Apache License](https://www.apache.org/licenses/LICENSE-2.0)', | |
'Apache-2.0': '[Apache License](https://www.apache.org/licenses/LICENSE-2.0)', | |
'BSD': '[BSD](http://en.wikipedia.org/wiki/BSD_licenses)' | |
} | |
var runCommand = function( command ) { | |
return q | |
.Promise( function( resolve, reject ) { | |
exec( command, function( error, stdout, stderr ) { | |
resolve( stdout ); | |
} ); | |
} ); | |
}; | |
q | |
.async( function *() { | |
var list = yield runCommand( 'npm ls' ) | |
var lines = list.split( '\n' ); | |
var licenses = _ | |
.chain( lines ) | |
.map( ( line ) => { | |
return ( line.match( /^.{4}([\w-]+)/g ) ); | |
} ) | |
.filter( ( match ) => { | |
return match !== null; | |
} ) | |
.map( ( match ) => match[ 0 ] ) | |
.filter( ( line ) => line.charAt( 0 ) === '├' ) | |
.map( ( line ) => line.substring( 4 ) ) | |
.map( ( name ) => [ name, require( './node_modules/' + name + '/package.json' ) ] ) | |
.map( ( dep_package_array ) => { | |
var dep_package = dep_package_array[ 1 ]; | |
var license; | |
if( dep_package.hasOwnProperty( 'license' ) ){ | |
license = dep_package.license; | |
} | |
if( dep_package.hasOwnProperty( 'licenses' ) ) { | |
license = dep_package.licenses; | |
} | |
dep_package_array.push( license ); | |
return dep_package_array; | |
} ) | |
.filter( ( arr ) => arr[ 2 ] !== null && arr[ 2 ] !== undefined ) | |
.map( ( arr ) => { | |
while( arr[ 2 ] instanceof Array ) { | |
arr[ 2 ] = arr[ 2 ][ 0 ]; | |
} | |
return arr; | |
} ) | |
.map( ( arr ) => { | |
if( arr[ 2 ].hasOwnProperty( 'type' ) ) { | |
arr[ 2 ] = arr[ 2 ].type; | |
} | |
return arr; | |
} ) | |
.map( ( arr ) => { | |
if( license_text.hasOwnProperty( arr[ 2 ] ) ) { | |
arr[ 2 ] = license_text[ arr[ 2 ] ]; | |
} | |
return arr; | |
} ) | |
.map( ( arr ) => { | |
var author; | |
if( arr[ 1 ].hasOwnProperty( 'author' ) ) { | |
author = ( arr[ 1 ].author ); | |
} | |
if( arr[ 1 ].hasOwnProperty( 'authors' ) ) { | |
author = ( arr[ 1 ].authors ); | |
} | |
arr.push( author ); | |
return arr; | |
} ) | |
.map( ( arr ) => { | |
if( arr[ 3 ] instanceof Object ) { | |
arr[ 3 ] = _.map( arr[ 3 ], ( val ) => val ).join( ' ' ); | |
} | |
return arr; | |
} ) | |
.map( ( arr ) => { | |
return '### [' + arr[ 1 ].name + '](' + arr[ 1 ].homepage + ')\n' + | |
arr[ 1 ].description + '\n' + | |
'\n' + | |
'>' + arr[ 2 ]; | |
} ) | |
.value(); | |
console.log( licenses.join( '\n\n' ) ); | |
} )() | |
.then( process.exit ) | |
.catch( ( error ) => { | |
console.error( error ); | |
console.error( error.stack ); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment