:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { readdirSync, readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import type { Plugin } from "vite";
/**
* Vite plugin that automatically generates service barrel files
*/
export function autoServicesPlugin(servicesDir: string): Plugin {
function generateBarrelFiles(logChange = false) {
const files = readdirSync(servicesDir);
// Filter for service implementation files
const serviceFiles = files.filter(
(f) =>
f.endsWith(".ts") &&
!f.endsWith(".types.ts") &&
!f.endsWith(".test.ts") &&
!f.endsWith(".spec.ts") &&
f !== "index.ts" &&
f !== "types.ts",
);
// Filter for service type files
const typeFiles = files.filter((f) => f.endsWith(".types.ts"));
// Generate index.ts (imports implementations for auto-registration)
const indexContent = `/**
* Auto-register all IPC services
* This file is auto-generated by vite-plugin-auto-services.ts
*/
${serviceFiles.map((f) => `import "./${f.replace(".ts", ".js")}";`).join("\n")}
`;
// Generate types.ts (imports type files for declaration merging)
const typesImports = typeFiles
.map((f) => `import "./${f.replace(".ts", ".js")}";`)
.join("\n");
const typesContent = `/**
* Auto-import all IPC service types for declaration merging
* This file is auto-generated by vite-plugin-auto-services.ts
*/
${typesImports || "export {};"}
`;
// Check if content actually changed before writing
const indexPath = join(servicesDir, "index.ts");
const typesPath = join(servicesDir, "types.ts");
let hasChanges = false;
try {
const existingIndex = readFileSync(indexPath, "utf-8");
const existingTypes = readFileSync(typesPath, "utf-8");
if (existingIndex !== indexContent || existingTypes !== typesContent) {
hasChanges = true;
}
} catch {
// Files don't exist, so we have changes
hasChanges = true;
}
if (hasChanges) {
writeFileSync(indexPath, indexContent);
writeFileSync(typesPath, typesContent);
if (logChange) {
console.log(
`✓ Generated service imports: ${serviceFiles.length} services, ${typeFiles.length} type files`,
);
}
}
}
return {
name: "auto-services",
buildStart() {
generateBarrelFiles(false);
},
configureServer(server) {
// Watch the services directory for changes
server.watcher.add(servicesDir);
server.watcher.on("add", (file) => {
if (file.startsWith(servicesDir) && file.endsWith(".ts")) {
console.log(`New service detected: ${file}`);
generateBarrelFiles(true);
}
});
server.watcher.on("unlink", (file) => {
if (file.startsWith(servicesDir) && file.endsWith(".ts")) {
console.log(`Service removed: ${file}`);
generateBarrelFiles(true);
}
});
},
};
}