From 1265cb38d186ab1609dbdfa4124f237f8aa15846 Mon Sep 17 00:00:00 2001 From: Adam Weber Date: Fri, 24 Jun 2016 16:48:42 -0500 Subject: [PATCH] Added ability to output individual license files --- bin/license-checker | 5 ++++- lib/args.js | 3 ++- lib/index.js | 22 ++++++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/bin/license-checker b/bin/license-checker index a88209f..28faa9b 100755 --- a/bin/license-checker +++ b/bin/license-checker @@ -65,7 +65,10 @@ checker.init(args, function(json, err) { formattedOutput = checker.asTree(json); } - if (args.out) { + if (args.files) { + checker.asFiles(json, args.files); + } + else if (args.out) { var dir = path.dirname(args.out); mkdirp.sync(dir); //Remove the color tags diff --git a/lib/args.js b/lib/args.js index f0c6a08..17cd62d 100644 --- a/lib/args.js +++ b/lib/args.js @@ -22,7 +22,8 @@ var nopt = require('nopt'), relativeLicensePath: Boolean, exclude: String, customPath: require('path'), - customFormat: { } + customFormat: { }, + files: require('path') }, shorts = { "v" : ["--version"], diff --git a/lib/index.js b/lib/index.js index d0252d6..e2d5b27 100644 --- a/lib/index.js +++ b/lib/index.js @@ -12,6 +12,7 @@ var chalk = require('chalk'); var treeify = require('treeify'); var license = require('./license'); var debug = require('debug'); +var mkdirp = require('mkdirp'); // Set up debug logging // https://www.npmjs.com/package/debug#stderr-vs-stdout @@ -345,3 +346,24 @@ exports.parseJson = function(jsonPath) { return result; } }; + +exports.asFiles = function(json, outDir) { + mkdirp.sync(outDir); + try { + Object.keys(json).forEach(function (moduleName) { + var licenseFile = json[moduleName].licenseFile; + if (licenseFile && fs.existsSync(licenseFile)) { + var fileContents = fs.readFileSync(licenseFile), + outFileName = chalk.stripColor(moduleName).replace(/(\s+|@)/g, "") + "-LICENSE.txt", + outPath = path.join(outDir, outFileName); + fs.writeFileSync(outPath, fileContents, "utf8"); + } + else { + console.warn("no license file found for: " + moduleName); + } + }); + } + catch(ex) { + console.log(ex); + } +};