88 lines
3.3 KiB
TypeScript
88 lines
3.3 KiB
TypeScript
import { exec } from "child_process";
|
|
import { promisify } from "util";
|
|
|
|
import { sveltekit } from "@sveltejs/kit/vite";
|
|
import { createViteLicensePlugin, type LicenseMeta } from "rollup-license-plugin";
|
|
import { defineConfig } from "vitest/config";
|
|
|
|
// Get current tag/commit and last commit date from git
|
|
const pexec = promisify(exec);
|
|
const [version, lastmod] = (
|
|
await Promise.allSettled([
|
|
pexec("git describe --tags || git rev-parse --short HEAD"),
|
|
pexec('git log -1 --format=%cd --date=format:"%Y-%m-%d %H:%M"'),
|
|
])
|
|
).map((v) => {
|
|
if (v.status !== "fulfilled") throw Error("could not get version info from git");
|
|
return JSON.stringify(v.value.stdout.trim());
|
|
});
|
|
|
|
export default defineConfig({
|
|
plugins: [
|
|
sveltekit(),
|
|
createViteLicensePlugin({
|
|
additionalFiles: {
|
|
"oss-licenses.html": (packages) => {
|
|
let res = `<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<title>Visitenbuch - Lizenzen</title>
|
|
</head>
|
|
<body>
|
|
<h1>Open-Source-Lizenzen</h1>
|
|
<a href="./oss-licenses.json">JSON-formatted license list</a>
|
|
`;
|
|
const escapeHTML = (s: string | null) => s ? s.replaceAll("&", "&")
|
|
.replaceAll("<", "<")
|
|
.replaceAll(">", ">")
|
|
.replaceAll('"', """)
|
|
.replaceAll("'", "'") : "";
|
|
|
|
for (const _p of packages) {
|
|
type LicenseMetaExt = LicenseMeta & {
|
|
repository: string | null,
|
|
author: string | { name: string } | null
|
|
};
|
|
const p = _p as LicenseMetaExt;
|
|
const rp = p.repository;
|
|
let aut = null;
|
|
if (p.author) {
|
|
if (typeof p.author === "object" && p.author.name) aut = p.author.name;
|
|
else if (typeof p.author === "string") aut = p.author;
|
|
}
|
|
|
|
let repoUrl = null;
|
|
if (typeof rp === "string") {
|
|
if (rp.startsWith("http")) repoUrl = rp;
|
|
else if (rp.startsWith("git+http")) repoUrl = rp.substring(4);
|
|
else if (rp.startsWith("git://")) repoUrl = "https://" + rp.substring(6);
|
|
else if (rp.startsWith("github:")) repoUrl = "https://github.com/" + rp.substring(7);
|
|
else if (rp.match(/^[\w-]+\/[\w-]+$/)) repoUrl = "https://github.com/" + rp;
|
|
}
|
|
|
|
res += `<div class="package">\n`;
|
|
res += `<h3><a href="https://www.npmjs.com/package/${escapeHTML(p.name)}" target="_blank" rel="noopener noreferrer">${escapeHTML(p.name)}</a></h3>\n`;
|
|
res += `<table>\n`;
|
|
res += `<tr><td>Version:</td><td>${escapeHTML(p.version)}</td></tr>\n`;
|
|
if (aut) res += `<tr><td>Author:</td><td>${escapeHTML(aut)}</td></tr>\n`;
|
|
res += `<tr><td>License:</td><td>${escapeHTML(p.license)}</td></tr>\n`;
|
|
if (repoUrl) res += `<tr><td>Repository:</td><td><a href="${escapeHTML(repoUrl)}" target="_blank" rel="noopener noreferrer">${escapeHTML(repoUrl)}</a></td></tr>\n`;
|
|
else if (rp) res += `<tr><td>Repository:</td><td>${escapeHTML(rp)}</td></tr>\n`;
|
|
res += `</table>\n`;
|
|
res += "</div>";
|
|
}
|
|
|
|
res += "</body>\n</html>\n";
|
|
return res;
|
|
},
|
|
},
|
|
}),
|
|
],
|
|
test: {
|
|
include: ["src/**/*.{test,spec}.{js,ts}"],
|
|
},
|
|
define: {
|
|
__VERSION__: version,
|
|
__LASTMOD__: lastmod,
|
|
},
|
|
});
|