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

Conversation

F-Kublin
Copy link
Collaborator

No description provided.

Comment on lines +4 to +10
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",
),
};
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:

Comment on lines 6 to 15
const licenseFiles = [
"LICENSE",
"LICENCE",
"LICENSE.md",
"LICENCE.md",
"LICENSE.txt",
"LICENSE-MIT",
"LICENSE.BSD",
] as const;

Copy link
Collaborator

Choose a reason for hiding this comment

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

this array was originally located in packages/core/src/license-finder/find-license-in-license-file.ts and that's where it should have stayed
I don't understand why you decided to ignore the separation of concerns that's been laid out
in 90% of the cases the license will be found in package.json
so I don't understand why this file has to know about the existence of various licenseFiles formats, when they most likely won't even be used

Comment on lines 28 to 33
for (const licenseFile of licenseFiles) {
const basicPath = path.join(packagePath, licenseFile);
const licenseFromLicenseFile = findLicenseInLicenseFile(basicPath);
if (licenseFromLicenseFile) {
return licenseFromLicenseFile;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

same here, it was an architectural choice to offload the developer's cognitive load by having 3 simple code paths
this for loop should have been placed in findLicenseInLicenseFile - the file dedicated to retrieving licenses from license files
not here, where the specified strategies should only be called, not implemented

Comment on lines 35 to 38
if (!fs.existsSync(filename)) {
return;
}
const content = fs.readFileSync(filename).toString();
Copy link
Collaborator

Choose a reason for hiding this comment

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

let's make it async please because this code is executed in a loop


export function findLicenseInLicenseFile(
filename: string,
): LicenseWithPath | undefined {
Copy link
Collaborator

Choose a reason for hiding this comment

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

let's make the return type just LicenseWithPath please
if it's not found return

license: undefined,
licensePath: undefined

Comment on lines 42 to 45
license:
Array.isArray(foundLicenses) && foundLicenses.length === 1
? foundLicenses[0]
: foundLicenses,
Copy link
Collaborator

Choose a reason for hiding this comment

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

the needsVerification case which we discussed is not handled
also please move the condition out of the return statement for better readability

@angjez angjez closed this Oct 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants