smartpnpm/ts/smartpnpm.classes.smartpnpm.ts
2023-10-11 19:09:18 +02:00

76 lines
2.1 KiB
TypeScript

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 class SmartPnpm {
public projectDir: string;
constructor(projectDirArg: string) {
this.projectDir = projectDirArg;
}
private smartshellInstance = new plugins.smartshell.Smartshell({
executor: 'bash',
})
async getDependencyLicenseJson() {
try {
// Get store directory
const execResult = await this.smartshellInstance.execSilent(`(cd ${this.projectDir} && pnpm licenses list --json)`);
const licenseJson: {[key: string]: Array<IPnpmLicenseResult>} = JSON.parse(execResult.stdout);
return licenseJson;
} catch (error) {
console.error('Error:', error);
}
}
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);
}
}