import { exec } from "child_process";
import { promisify } from "util";
import { sveltekit } from "@sveltejs/kit/vite";
import { createViteLicensePlugin } from "rollup-license-plugin";
import { defineConfig } from "vitest/config";
// Get current tag/commit and last commit date from git
const pexec = promisify(exec);
let [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
`;
for (const p of packages) {
// @ts-expect-error repo not present in type definition
let rp = p.repository;
// @ts-expect-error author not present in type definition
let aut = p.author;
if (typeof aut === "object") {
aut = aut.name;
}
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: | ${p.version} |
\n`;
if (aut) res += `Author: | ${aut} |
\n`;
res += `License: | ${p.license} |
\n`;
if (repoUrl) res += `Repository: | ${repoUrl} |
\n`;
else if (rp) res += `Repository: | ${rp} |
\n`;
res += `
\n`;
res += "
";
}
res += "\n\n";
return res;
},
},
}),
],
test: {
include: ["src/**/*.{test,spec}.{js,ts}"],
},
define: {
__VERSION__: version,
__LASTMOD__: lastmod,
},
});