-
Notifications
You must be signed in to change notification settings - Fork 1
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
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
518971e
feat(core): reimplement finding licenses from LICENSE file
F-Kublin 68aa20c
refactor(core): compare license content against available licenses
F-Kublin 774e8e8
feat(core): iterate over license files
F-Kublin a6abb58
fix(core): fix formatting
F-Kublin 4bb19b3
fix(core): iterate over licenseFiles correctly
F-Kublin 1eeb86f
refactor(core): return a single license code if one was detected
F-Kublin da73768
Merge branch 'release-v2.0.0' of https://github.com/brainhubeu/licens…
F-Kublin 6d4db85
refactor(core): adjust retrieving license from licenseFile after chan…
F-Kublin d9d43e5
refactor(core): move iteration over license files to appropriate scope
F-Kublin 7843675
refactor(core): use async readFile function
F-Kublin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
97 changes: 82 additions & 15 deletions
97
packages/core/src/license-finder/find-license-in-license-file.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
), | ||
}; | ||
|
||
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
packages/core/src/license-finder/templates/BSD-2-clause.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 filesyou 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
There was a problem hiding this comment.
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: