Skip to content

Commit

Permalink
fix(files): support licenses attribute
Browse files Browse the repository at this point in the history
and refactor license parse to separate file
  • Loading branch information
msrcodes committed Oct 15, 2021
1 parent 6695043 commit c70e817
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,40 @@ import {readFileSync} from 'fs';
import {sync} from 'glob';
import {LicenseMap, NO_LICENSE} from './consts';

interface OldLicense {
type: string;
url: string;
}

interface PackageFile {
license?: string | OldLicense;
licenses?: [OldLicense];
}

const parseLicense = ({license, licenses}: PackageFile): string => {
let parsedLicense = NO_LICENSE;

if (typeof license === 'string') {
parsedLicense = license;
} else if (license?.type) {
parsedLicense = license.type;
} else if (licenses) {
const types = licenses.map(({type}) => type);
if (types.length === 1) {
parsedLicense = types[0];
} else {
parsedLicense = `(${types.join(' OR ')})`;
}
}

return parsedLicense;
};

const parsePackageFile = (path: string) => {
const fileContent = readFileSync(path).toString();
let {license} = JSON.parse(fileContent);
const pkgFile = JSON.parse(fileContent) as PackageFile;

license = license?.type ?? license ?? NO_LICENSE;
const license = parseLicense(pkgFile);

return {name: `./${path}`, license};
};
Expand Down

0 comments on commit c70e817

Please sign in to comment.