Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Retrieve license from license file #53

Closed
wants to merge 10 commits into from
1 change: 1 addition & 0 deletions packages/core/license.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ declare global {
interface LicenseWithPath {
license: LicenseResult;
licensePath: string | undefined;
needsVerification?: boolean;
}
}
296 changes: 7 additions & 289 deletions packages/core/src/audit-licenses.ts

Large diffs are not rendered by default.

97 changes: 82 additions & 15 deletions packages/core/src/license-finder/find-license-in-license-file.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,83 @@
// import * as fs from "node:fs";

// const licenseFiles = [
// "LICENSE",
// "LICENCE",
// "LICENSE.md",
// "LICENCE.md",
// "LICENSE.txt",
// "LICENSE-MIT",
// "LICENSE.BSD",
// ] as const;

// todo: re-implement logic responsible for searching for license in license files
export function findLicenseInLicenseFile(): LicenseWithPath | undefined {
return undefined;
import * as fs from "node:fs";
import * as path from "node:path";
import { type License, licenses } from "@license-auditor/licenses";

const licenseFiles = [
"LICENSE",
"LICENCE",
"LICENSE.md",
"LICENCE.md",
"LICENSE.txt",
"LICENSE-MIT",
"LICENSE.BSD",
] as const;

const templates = {
[fs.readFileSync(`${__dirname}/templates/BSD-2-Clause.txt`).toString()]:
licenses.find((license) => license.licenseId === "BSD-2-Clause"),
[fs.readFileSync(`${__dirname}/templates/MIT.txt`).toString()]: licenses.find(
(license) => license.licenseId === "MIT",
),
};
Comment on lines +15 to +21
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please prove the code is actually functional by creating a sample directory with actual installed modules containing license files

you can find what @matt-jb used to query the npm registry for packages which do not contain licenses in package.json

we want to prove the code on actual data and actual use cases, not mocks which can prove to be unreliable

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing, here's the crawler's code: https://github.com/matt-jb/npm-registry-crawler

Just pop in some popular word to query packages for (like node, http, etc.) to get actual semi-random results.

The crawl is rather slow, though. It takes <20 mins for a full run of 1000 packages. This is due to the NPM's crawling policy, which rate-limits requests to 1 per second or less.

Here are some packages that I found yesterday that you can test against:


function retrieveLicenseFromLicenseFileContent(
content: string,
): License | License[] {
const lines = content.split("\n");

const contentTokens = content.split(/[ ,]+/);

const licenseArr = licenses.filter(
(license) =>
contentTokens.includes(license.licenseId) ||
contentTokens.includes(license.name),
);

const withoutFirstLine = lines
.slice(1)
.filter((line) => line.length)
.join("\n");
return templates[withoutFirstLine] || licenseArr;
}

export async function findLicenseInLicenseFile(
filename: string,
): Promise<LicenseWithPath> {
if (!fs.existsSync(filename)) {
return { license: undefined, licensePath: undefined };
}

const content = await fs.promises.readFile(filename, "utf-8");

if (!content) {
return { license: undefined, licensePath: undefined };
}
const foundLicenses = retrieveLicenseFromLicenseFileContent(content);

const license =
Array.isArray(foundLicenses) && foundLicenses.length === 1
? foundLicenses[0]
: foundLicenses;

if (!license) {
return { license: undefined, licensePath: undefined };
}

return {
license,
licensePath: filename,
needsVerification: Array.isArray(license),
};
}

export function parseLicenseFiles(
packagePath: string,
): Promise<LicenseWithPath> | undefined {
for (const licenseFile of licenseFiles) {
const basicPath = path.join(packagePath, licenseFile);
const licenseFromLicenseFile = findLicenseInLicenseFile(basicPath);
if (licenseFromLicenseFile) {
return licenseFromLicenseFile;
}
}
}
8 changes: 4 additions & 4 deletions packages/core/src/license-finder/find-license.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as path from "node:path";
import { findLicenseInLicenseFile } from "./find-license-in-license-file";
import { parseLicenseFiles } from "./find-license-in-license-file";
import { findLicenseInPackageJson } from "./find-license-in-package-json";
import { findLicenseInReadme } from "./find-license-in-readme";

export function findLicense(
export async function findLicense(
packageJson: object,
packagePath: string,
): LicenseWithPath {
): Promise<LicenseWithPath> {
const licenseFromPackageJson = findLicenseInPackageJson(packageJson);
if (licenseFromPackageJson) {
return {
Expand All @@ -15,7 +15,7 @@ export function findLicense(
};
}

const licenseFromLicenseFile = findLicenseInLicenseFile();
const licenseFromLicenseFile = parseLicenseFiles(packagePath);
if (licenseFromLicenseFile) {
return licenseFromLicenseFile;
}
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/license-finder/templates/BSD-2-clause.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
18 changes: 18 additions & 0 deletions packages/core/src/license-finder/templates/MIT.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.