-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
540 additions
and
21 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
const generateLicenses = function generateLicenses(file) { | ||
const checker = require('license-checker'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
checker.init( | ||
{ | ||
start: ".", | ||
production: true, | ||
excludePrivatePackages: true, | ||
}, | ||
function (err, json) { | ||
if (err) { | ||
console.log(err); //Handle error | ||
} else { | ||
copyLicenseToText(json); //The sorted json data | ||
} | ||
} | ||
); | ||
function copyLicenseToText(json) { | ||
const licenses = Object.keys(json).map((key) => { | ||
let obj = json[key]; | ||
obj.module = key; | ||
return obj; | ||
}) | ||
.filter(item => !isMarkDown(item.licenseFile)) | ||
.reduce( | ||
(r, v) => ((r[v.licenses] || (r[v.licenses] = [])).push(v), r), | ||
{} | ||
); | ||
printLicenseSummary(licenses); | ||
printLicenseDetails(licenses); | ||
} | ||
function printLicenseSummary(licenses) { | ||
Object.keys(licenses).forEach((key) => { | ||
const items = licenses[key]; | ||
fs.appendFileSync(file, "\n\n-------------------\n"); | ||
fs.appendFileSync(file, `${key} - License Summary\n`); | ||
fs.appendFileSync(file, "--------------------\n\n"); | ||
items.forEach(item => { | ||
fs.appendFileSync(file, `${item.module}\n`); | ||
}); | ||
}); | ||
} | ||
function printLicenseDetails(licenses) { | ||
Object.keys(licenses).forEach(function (key) { | ||
const items = licenses[key]; | ||
fs.appendFileSync(file, "\n\n----------------------\n"); | ||
fs.appendFileSync(file, `${key} - License Details \n`); | ||
fs.appendFileSync(file, "------------------------\n\n"); | ||
items.forEach(copyLicense); | ||
}); | ||
} | ||
function copyLicense(item) { | ||
try { | ||
if (item.licenseFile && !isMarkDown(item.licenseFile)) { | ||
const licenseFile = fs.readFileSync(`${item.licenseFile}`, "utf8"); | ||
fs.appendFileSync(file, "\n\n----------------------\n"); | ||
fs.appendFileSync(file, `${item.module} - ${item.licenses}\n`); | ||
fs.appendFileSync(file, "-----------------------\n\n"); | ||
fs.appendFileSync(file, licenseFile); | ||
} | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
} | ||
function isMarkDown(filepath) { | ||
const filename = path.basename(filepath).toUpperCase(); | ||
return filename == "README.MD" || filename == "README.MARKDOWN"; | ||
} | ||
}; | ||
module.exports.generateLicenses = generateLicenses; |
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
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
Oops, something went wrong.