vite-version-plugin/dist/index.js
autocommit 82755ccc82 chore: initial package split from monorepo
Package: @lilith/vite-version-plugin
Split from: lilith/ui.git or lilith/build.git
Publish workflow: calls lilith/workflows/.forgejo/workflows/publish-npm.yml@main
2026-04-20 01:11:53 -07:00

155 lines
No EOL
4.8 KiB
JavaScript

// src/index.ts
import { readFileSync, writeFileSync, existsSync } from "fs";
import { execFileSync } from "child_process";
import { resolve, join } from "path";
function findVersionFile(projectRoot, customPath) {
if (customPath) {
const resolved = resolve(projectRoot, customPath);
if (existsSync(resolved)) return resolved;
return null;
}
const searchPaths = [
join(projectRoot, "VERSION.txt"),
join(projectRoot, "VERSION.json"),
...findMonorepoVersionPaths(projectRoot)
];
for (const p of searchPaths) {
if (existsSync(p)) return p;
}
return null;
}
function readAndIncrementBuildCount(versionFilePath) {
const dir = versionFilePath ? resolve(versionFilePath, "..") : process.cwd();
const countFile = join(dir, "BUILD_COUNT");
let count = 0;
if (existsSync(countFile)) {
const raw = readFileSync(countFile, "utf-8").trim();
count = parseInt(raw, 10) || 0;
}
count += 1;
writeFileSync(countFile, String(count));
return count;
}
function findMonorepoVersionPaths(startDir) {
const paths = [];
let current = startDir;
let depth = 0;
const maxDepth = 10;
while (depth < maxDepth) {
const parent = resolve(current, "..");
if (parent === current) break;
const workspaceFile = join(parent, "pnpm-workspace.yaml");
if (existsSync(workspaceFile)) {
paths.push(join(parent, "VERSION.txt"));
paths.push(join(parent, "VERSION.json"));
break;
}
current = parent;
depth++;
}
return paths;
}
function readVersion(filePath) {
const content = readFileSync(filePath, "utf-8").trim();
if (filePath.endsWith(".json")) {
try {
const json = JSON.parse(content);
return json.version || "0.0.0";
} catch {
return "0.0.0";
}
}
return content.split("\n")[0].trim();
}
function getGitInfo() {
try {
const commit = execFileSync("git", ["rev-parse", "--short", "HEAD"], {
encoding: "utf-8"
}).trim();
const branch = execFileSync("git", ["rev-parse", "--abbrev-ref", "HEAD"], {
encoding: "utf-8"
}).trim();
return { commit, branch };
} catch {
return { commit: "unknown", branch: "unknown" };
}
}
function extractFeatureFrontendNames(projectRoot) {
const normalized = projectRoot.replace(/\\/g, "/");
const match = normalized.match(/features\/([^/]+)\/frontend-([^/]+)/);
if (match) {
return { feature: match[1], frontend: match[2] };
}
return { feature: "unknown", frontend: "unknown" };
}
function versionPlugin(options) {
const {
appName,
versionFile,
generateBuildInfo = true,
fallbackVersion = "0.0.0-dev",
featureName,
frontendName
} = options;
let resolvedConfig;
let versionInfo;
return {
name: "lilith-version-plugin",
config(userConfig, { command }) {
const projectRoot = userConfig.root || process.cwd();
const versionPath = findVersionFile(projectRoot, versionFile);
const version = versionPath ? readVersion(versionPath) : fallbackVersion;
const gitInfo = getGitInfo();
const buildTime = (/* @__PURE__ */ new Date()).toISOString();
const buildCount = command === "build" ? readAndIncrementBuildCount(versionPath) : 0;
const extracted = extractFeatureFrontendNames(projectRoot);
const finalFeatureName = featureName || extracted.feature;
const finalFrontendName = frontendName || extracted.frontend;
versionInfo = {
version,
buildTime,
buildCount,
gitCommit: gitInfo.commit,
gitBranch: gitInfo.branch
};
if (command === "build") {
console.log(`
\u{1F4E6} ${appName} v${version} build #${buildCount} (${gitInfo.commit})`);
if (finalFeatureName !== "unknown" && finalFrontendName !== "unknown") {
console.log(` Feature: ${finalFeatureName} / Frontend: ${finalFrontendName}`);
}
}
return {
define: {
__APP_VERSION__: JSON.stringify(version),
__BUILD_TIME__: JSON.stringify(buildTime),
__BUILD_COUNT__: JSON.stringify(buildCount),
__GIT_COMMIT__: JSON.stringify(gitInfo.commit),
__GIT_BRANCH__: JSON.stringify(gitInfo.branch),
__APP_NAME__: JSON.stringify(appName),
__FEATURE_NAME__: JSON.stringify(finalFeatureName),
__FRONTEND_NAME__: JSON.stringify(finalFrontendName)
}
};
},
configResolved(config) {
resolvedConfig = config;
},
writeBundle(outputOptions) {
if (!generateBuildInfo) return;
const outDir = outputOptions.dir || resolvedConfig.build.outDir;
const buildInfo = {
app: appName,
...versionInfo
};
const buildInfoPath = join(outDir, "build-info.json");
writeFileSync(buildInfoPath, JSON.stringify(buildInfo, null, 2));
}
};
}
var index_default = versionPlugin;
export {
index_default as default,
versionPlugin
};
//# sourceMappingURL=index.js.map