Check license info for a package and its dependencies
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
License file extraction and JSON parsing utilities for advanced license management workflows.
Extracts individual license files from scanned packages to a specified output directory for compliance archiving and legal review.
/**
* Extract license files from packages to output directory
* @param data - License data from init() callback
* @param outDir - Output directory path for extracted files
*/
function asFiles(data: LicenseData, outDir: string): void;Usage Example:
const checker = require("license-checker");
const path = require("path");
checker.init({ start: process.cwd() }, function(err, packages) {
if (err) throw err;
// Extract all license files to ./licenses directory
checker.asFiles(packages, "./licenses");
console.log("License files extracted to ./licenses");
});Behavior:
{package-name}@{version}-LICENSE.txtlicenseFile path and the file existsExample Output Structure:
licenses/
├── chalk@2.4.2-LICENSE.txt
├── debug@3.2.7-LICENSE.txt
├── nopt@4.0.3-LICENSE.txt
├── mkdirp@0.5.5-LICENSE.txt
└── semver@5.7.1-LICENSE.txtFile Naming:
@angular/core@12.0.0 → @angular/core@12.0.0-LICENSE.txt-LICENSE.txt suffix for all extracted filesError Handling:
Utility function for parsing JSON files with error handling, commonly used for custom format specifications.
/**
* Parse JSON file from filesystem path
* @param jsonPath - Absolute or relative path to JSON file
* @returns Parsed JSON object or Error instance on failure
*/
function parseJson(jsonPath: string): object | Error;Usage Examples:
const checker = require("license-checker");
// Parse custom format file
const customFormat = checker.parseJson("./custom-format.json");
if (customFormat instanceof Error) {
console.error("Failed to parse custom format:", customFormat.message);
} else {
// Use parsed format with init
checker.init({
start: process.cwd(),
customFormat: customFormat
}, callback);
}
// Alternative usage with path validation
const formatPath = "./custom-format.json";
if (typeof formatPath === "string") {
const format = checker.parseJson(formatPath);
if (!(format instanceof Error)) {
console.log("Loaded custom format:", format);
}
}Return Values:
Error Types:
// File system errors
const result = checker.parseJson("./nonexistent.json");
// Returns: Error("ENOENT: no such file or directory...")
// JSON parsing errors
const result = checker.parseJson("./invalid.json");
// Returns: Error("Unexpected token } in JSON at position 15")
// Type validation
const result = checker.parseJson(123);
// Returns: Error("did not specify a path")Custom Format File Example:
{
"name": "",
"version": "",
"licenses": "",
"repository": "",
"publisher": "",
"description": "No description available",
"licenseText": false,
"copyright": false
}Both file operations integrate with the CLI tool:
# Extract license files to directory
license-checker --files ./extracted-licenses
# Use custom format from JSON file
license-checker --customPath ./my-format.json --csv
# Combine file extraction with other outputs
license-checker --files ./licenses --json --out report.jsonCLI File Extraction:
--files flag specifies output directory for license file extractionCLI Custom Format:
--customPath flag loads JSON format specificationCompliance Archiving:
const checker = require("license-checker");
const fs = require("fs");
const path = require("path");
function createComplianceReport(projectPath, outputDir) {
checker.init({
start: projectPath,
production: true // Only production dependencies for deployment
}, function(err, packages) {
if (err) throw err;
// Extract license files
checker.asFiles(packages, path.join(outputDir, "licenses"));
// Create summary report
const summary = checker.asSummary(packages);
fs.writeFileSync(path.join(outputDir, "license-summary.txt"), summary);
// Create detailed CSV
const csv = checker.asCSV(packages);
fs.writeFileSync(path.join(outputDir, "license-details.csv"), csv);
console.log(`Compliance report created in ${outputDir}`);
});
}Custom Analysis:
// Load analysis configuration
const analysisConfig = checker.parseJson("./analysis-config.json");
if (analysisConfig instanceof Error) {
console.error("Configuration error:", analysisConfig.message);
process.exit(1);
}
checker.init({
start: process.cwd(),
customFormat: analysisConfig.format,
exclude: analysisConfig.excludeLicenses,
production: analysisConfig.productionOnly
}, function(err, packages) {
// Process results with custom configuration
const report = checker.asMarkDown(packages, analysisConfig.format);
fs.writeFileSync("analysis-report.md", report);
});