Skip to content

Commit 40b348b

Browse files
committed
Add tool to find license
1 parent 2b7fed2 commit 40b348b

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

tools/find-license.mjs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function convertToRawURL(url, suffix = null) {
2+
url = url.replace('https://github.com/', '');
3+
const parts = url.split('/');
4+
const owner = parts[0];
5+
const repoName = parts[1];
6+
const branch = 'HEAD';
7+
let path = parts[2] ? "/" + parts[2] : '';
8+
if (suffix) {
9+
path += "/" + suffix;
10+
}
11+
return `https://raw.githubusercontent.com/${owner}/${repoName}/${branch}${path}`;
12+
}
13+
14+
async function findLicenseURL(url) {
15+
const licenseFileURL = convertToRawURL(url, 'LICENSE');
16+
// Fetch the license file
17+
const licenseResponse = await fetch(licenseFileURL);
18+
if (licenseResponse.ok) {
19+
const licenseText = await licenseResponse.text();
20+
// Get the first line of the license file
21+
const license = licenseText.split('\n')[0];
22+
return license;
23+
}
24+
return null;
25+
}
26+
27+
if(process.argv.length < 3) {
28+
console.error('Usage: node find-license.mjs <GitHub URL>');
29+
process.exit(1);
30+
}
31+
32+
const license = await findLicenseURL(process.argv[2]);
33+
if (license) console.log(license);

0 commit comments

Comments
 (0)