Skip to content

Commit

Permalink
Added ability to output individual license files
Browse files Browse the repository at this point in the history
  • Loading branch information
aweber1 committed Jun 24, 2016
1 parent 7101c77 commit 1265cb3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
5 changes: 4 additions & 1 deletion bin/license-checker
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ var nopt = require('nopt'),
relativeLicensePath: Boolean,
exclude: String,
customPath: require('path'),
customFormat: { }
customFormat: { },
files: require('path')
},
shorts = {
"v" : ["--version"],
Expand Down
22 changes: 22 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
};

0 comments on commit 1265cb3

Please sign in to comment.