import * as plugins from './smartpnpm.plugins.js'; export interface IPnpmLicenseResult { "name": string; "version": string; "path": string; "license": string; "author": string; "homepage": string; "description": string; } export type TPnpmLicenseMap = Record; export class SmartPnpm { public projectDir: string; constructor(projectDirArg: string) { this.projectDir = projectDirArg; } private smartshellInstance = new plugins.smartshell.Smartshell({ executor: 'bash', }); async getDependencyLicenseJson(): Promise { const projectDir = this.projectDir.replace(/(["\\$`])/g, '\\$1'); const execResult = await this.smartshellInstance.execSilent(`(cd "${projectDir}" && pnpm licenses list --json)`); return JSON.parse(execResult.stdout) as TPnpmLicenseMap; } public async printDependencyLicenseSummary() { const result = await this.getDependencyLicenseJson(); for (const licenseType of Object.keys(result)) { const refArray = result[licenseType]; console.log(`${licenseType}: ${refArray.length} packages.`); if (licenseType !== 'MIT' && licenseType !== 'ISC') { for (const ref of refArray) { console.log(`|-- ${ref.name} (${ref.version})`); } } else { console.log(`|-- ${refArray.length} packages. Not detailing this license type.`); } } } public async getDependencyLicenseFlatArray() { const result = await this.getDependencyLicenseJson(); const licenseArray: Array<{ "name": string; "version": string; "path": string; "license": string; "author": string; "homepage": string; "description": string; }> = []; for (const licenseType of Object.keys(result)) { const refArray = result[licenseType]; for (const ref of refArray) { licenseArray.push(ref); } } return licenseArray; } public async getDependencyLicenseArray() { const result = await this.getDependencyLicenseJson(); return Object.keys(result); } }