Public
HTTP
Forked from kylem/gitReleaseNotes
kylem-dependencylicenses.web.val.run
July 24, 2024
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/** @jsxImportSource npm:hono@3/jsx */
import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo?v=29";
import { Hono } from "npm:hono";
import { marked } from "npm:marked";
interface PackageJson {
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
}
interface PackageLicense {
name: string;
version: string;
license: string;
repoUrl: string;
}
// Function to fetch package info from NPM registry
async function fetchPackageInfo(packageName: string) {
const url = `https://registry.npmjs.org/${packageName}`;
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch package info for ${packageName}`);
}
return response.json();
}
// Function to extract license information from package info
function extractLicenseInfo(packageInfo: any): string {
if (typeof packageInfo.license === "string") {
return packageInfo.license;
} else if (packageInfo.license && packageInfo.license.type) {
return packageInfo.license.type;
} else {
return "Unknown";
}
}
// Function to extract and normalize GitHub repo URL from package info
function extractGitHubRepoUrl(packageInfo: any): string {
const repository = packageInfo.repository;
if (repository) {
let repoUrl = typeof repository === "string" ? repository : repository.url;
if (repoUrl) {
// Remove git+ prefix and .git suffix
repoUrl = repoUrl.replace(/^git\+/, "").replace(/\.git$/, "");
// Convert git:// URLs to https://
repoUrl = repoUrl.replace(/^git:\/\/github.com\//, "https://github.com/");
// Convert ssh URLs to https://
repoUrl = repoUrl.replace(/^git@github.com:/, "https://github.com/");
// Ensure the URL starts with https://
if (!repoUrl.startsWith("http")) {
repoUrl = `https://github.com/${repoUrl}`;
}
return repoUrl;
}
}
return "";
}
function extractRepoNameFromUrl(url: string): string {
const match = url.match(/githubusercontent\.com\/[^\/]+\/([^\/]+)/);
return match ? match[1] : "";
}
async function checkPackageLicenses(packageJsonUrl: string): Promise<{ licenses: PackageLicense[]; repoName: string }> {
try {
const response = await fetch(packageJsonUrl);
if (!response.ok) {
throw new Error(`Failed to fetch package.json from ${packageJsonUrl}`);
}
const packageJson: PackageJson = await response.json();
const dependencies = { ...packageJson.dependencies, ...packageJson.devDependencies };
const licenseInfo: PackageLicense[] = [];
const repoName = extractRepoNameFromUrl(packageJsonUrl);
for (const [name, version] of Object.entries(dependencies)) {
try {
const packageInfo = await fetchPackageInfo(name);
const license = extractLicenseInfo(packageInfo);
const repoUrl = extractGitHubRepoUrl(packageInfo);
licenseInfo.push({ name, version, license, repoUrl });
} catch (error) {
licenseInfo.push({ name, version, license: `Error: ${error.message}`, repoUrl: "" });
}
}
return { licenses: licenseInfo, repoName };
} catch (error) {
throw new Error(`Error checking package licenses: ${error.message}`);
}
}
function generateLicenseTable(licenses: PackageLicense[]): string {
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
July 24, 2024