UNPKG

license-kit

Version:

Aggregate license notes of OSS libraries used in your Node.js project, analyze & visualize OSS licenses with AI-turbocharged tooling

72 lines (71 loc) 3.82 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = copyleftCommandSetup; const node_fs_1 = __importDefault(require("node:fs")); const node_path_1 = __importDefault(require("node:path")); const node_process_1 = __importDefault(require("node:process")); const licenses_1 = require("@callstack/licenses"); const constants_1 = require("../constants"); const scanOptionsUtils_1 = require("../scanOptionsUtils"); const commandUtils_1 = require("../utils/commandUtils"); function copyleftCommandSetup(program) { return (0, commandUtils_1.curryCommonScanOptions)(program .command('copyleft') .description('Check for copyleft licenses. Exits with error if strong copyleft licenses are found.' + '\nExit codes:' + `\n${constants_1.NON_TAB_HELP_LISTING_SUBLIST_OFFSET}- 0 - no copyleft licenses found` + `\n${constants_1.NON_TAB_HELP_LISTING_SUBLIST_OFFSET}- 1 - strong copyleft licenses found` + `\n${constants_1.NON_TAB_HELP_LISTING_SUBLIST_OFFSET}- 2 - weak copyleft licenses found (if --error-on-weak is set)`) .option('--error-on-weak', 'Exit with error if weak copyleft licenses are found', false) .option('--root [path]', 'Path to the root of your project', '.')).action((options) => { (0, commandUtils_1.validateCommonScanOptions)(options); const repoRootPath = node_path_1.default.resolve(node_process_1.default.cwd(), options.root); const packageJsonPath = node_path_1.default.join(repoRootPath, 'package.json'); if (!node_fs_1.default.existsSync(packageJsonPath)) { console.error(`package.json not found at ${packageJsonPath}`); node_process_1.default.exit(1); } const licenses = (0, licenses_1.scanDependencies)(packageJsonPath, (0, scanOptionsUtils_1.createScanOptionsFactory)(options)); const strongCopyleftLicensesFound = []; const weakCopyleftLicensesFound = []; for (const value of Object.values(licenses)) { if (!value.type) { continue; } if (licenses_1.STRONG_COPYLEFT_LICENSES_LOWERCASE.has(value.type.toLowerCase())) { strongCopyleftLicensesFound.push(`- ${value.name}: ${value.type} (${value.file || value.url})`); } if (licenses_1.WEAK_COPYLEFT_LICENSES_LOWERCASE.has(value.type.toLowerCase())) { weakCopyleftLicensesFound.push(`- ${value.name}: ${value.type} (${value.file || value.url})`); } } let exitCode = 0, noCopyleftLicensesFound = true; if (strongCopyleftLicensesFound.length > 0) { console.error(`${constants_1.ERROR_EMOJI} Copyleft licenses found in the following dependencies:`); strongCopyleftLicensesFound.forEach((entry) => { console.error(entry); }); exitCode = 1; noCopyleftLicensesFound = false; } if (weakCopyleftLicensesFound.length > 0) { console.error(`${options.errorOnWeak ? constants_1.ERROR_EMOJI : constants_1.WARNING_EMOJI} Weak copyleft licenses found in the following dependencies:`); weakCopyleftLicensesFound.forEach((entry) => { (options.errorOnWeak ? console.error : console.warn)(entry); }); if (options.errorOnWeak) { exitCode = 2; } noCopyleftLicensesFound = false; } if (noCopyleftLicensesFound) { console.log('✅ No copyleft licenses found'); } if (exitCode != 0) { node_process_1.default.exit(exitCode); } }); }