Visitenbuch/vite.config.ts

81 lines
2.9 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 = `<html>
<head>
<title>Visitenbuch - Lizenzen</title>
</head>
<body>
<h1>Open-Source-Lizenzen</h1>
<a href="./oss-licenses.json">JSON-formatted license list</a>
`;
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/${p.name}" target="_blank" rel="noopener noreferrer">${p.name}</a></h3>\n`;
res += `<table>\n`;
res += `<tr><td>Version:</td><td>${p.version}</td></tr>\n`;
if (aut) res += `<tr><td>Author:</td><td>${aut}</td></tr>\n`;
res += `<tr><td>License:</td><td>${p.license}</td></tr>\n`;
if (repoUrl) res += `<tr><td>Repository:</td><td><a href="${repoUrl}" target="_blank" rel="noopener noreferrer">${repoUrl}</a></td></tr>\n`;
else if (rp) res += `<tr><td>Repository:</td><td>${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,
},
});