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 = `
Visitenbuch - Lizenzen
Open-Source-Lizenzen
JSON-formatted license list
`;
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 += `\n`;
res += `
\n`;
res += `
\n`;
res += `Version: | ${escapeHTML(p.version)} |
\n`;
if (aut) res += `Author: | ${escapeHTML(aut)} |
\n`;
res += `License: | ${escapeHTML(p.license)} |
\n`;
if (repoUrl) res += `Repository: | ${escapeHTML(repoUrl)} |
\n`;
else if (rp) res += `Repository: | ${escapeHTML(rp)} |
\n`;
res += `
\n`;
res += "
";
}
res += "\n\n";
return res;
},
},
}),
],
test: {
include: ["src/**/*.{test,spec}.{js,ts}"],
},
define: {
__VERSION__: version,
__LASTMOD__: lastmod,
},
});