Add back file watch for declarative config

This commit is contained in:
bkellam 2025-04-01 14:51:02 -07:00
parent 1080e6a5c8
commit f8adf5974d

View file

@ -3,6 +3,7 @@ import { env } from './env.mjs';
import { prisma } from "@/prisma";
import { SINGLE_TENANT_USER_ID, SINGLE_TENANT_ORG_ID, SINGLE_TENANT_ORG_DOMAIN, SINGLE_TENANT_ORG_NAME, SINGLE_TENANT_USER_EMAIL } from './lib/constants';
import { readFile } from 'fs/promises';
import { watch } from 'fs';
import stripJsonComments from 'strip-json-comments';
import { SourcebotConfig } from "@sourcebot/schemas/v3/index.type";
import { ConnectionConfig } from '@sourcebot/schemas/v3/connection.type';
@ -21,62 +22,7 @@ const isRemotePath = (path: string) => {
return path.startsWith('https://') || path.startsWith('http://');
}
const initSingleTenancy = async () => {
await prisma.org.upsert({
where: {
id: SINGLE_TENANT_ORG_ID,
},
update: {},
create: {
name: SINGLE_TENANT_ORG_NAME,
domain: SINGLE_TENANT_ORG_DOMAIN,
id: SINGLE_TENANT_ORG_ID,
isOnboarded: env.SOURCEBOT_AUTH_ENABLED === 'false',
}
});
if (env.SOURCEBOT_AUTH_ENABLED === 'false') {
// Default user for single tenancy unauthed access
await prisma.user.upsert({
where: {
id: SINGLE_TENANT_USER_ID,
},
update: {},
create: {
id: SINGLE_TENANT_USER_ID,
email: SINGLE_TENANT_USER_EMAIL,
},
});
await prisma.org.update({
where: {
id: SINGLE_TENANT_ORG_ID,
},
data: {
members: {
upsert: {
where: {
orgId_userId: {
orgId: SINGLE_TENANT_ORG_ID,
userId: SINGLE_TENANT_USER_ID,
}
},
update: {},
create: {
role: OrgRole.MEMBER,
user: {
connect: { id: SINGLE_TENANT_USER_ID }
}
}
}
}
}
});
}
// Load any connections defined declaratively in the config file.
const configPath = env.CONFIG_PATH;
if (configPath) {
const scheduleDeclarativeConfigSync = async (configPath: string) => {
const configContent = await (async () => {
if (isRemotePath(configPath)) {
const response = await fetch(configPath);
@ -183,6 +129,73 @@ const initSingleTenancy = async () => {
}
}
}
const initSingleTenancy = async () => {
await prisma.org.upsert({
where: {
id: SINGLE_TENANT_ORG_ID,
},
update: {},
create: {
name: SINGLE_TENANT_ORG_NAME,
domain: SINGLE_TENANT_ORG_DOMAIN,
id: SINGLE_TENANT_ORG_ID,
isOnboarded: env.SOURCEBOT_AUTH_ENABLED === 'false',
}
});
if (env.SOURCEBOT_AUTH_ENABLED === 'false') {
// Default user for single tenancy unauthed access
await prisma.user.upsert({
where: {
id: SINGLE_TENANT_USER_ID,
},
update: {},
create: {
id: SINGLE_TENANT_USER_ID,
email: SINGLE_TENANT_USER_EMAIL,
},
});
await prisma.org.update({
where: {
id: SINGLE_TENANT_ORG_ID,
},
data: {
members: {
upsert: {
where: {
orgId_userId: {
orgId: SINGLE_TENANT_ORG_ID,
userId: SINGLE_TENANT_USER_ID,
}
},
update: {},
create: {
role: OrgRole.MEMBER,
user: {
connect: { id: SINGLE_TENANT_USER_ID }
}
}
}
}
}
});
}
// Load any connections defined declaratively in the config file.
const configPath = env.CONFIG_PATH;
if (configPath) {
await scheduleDeclarativeConfigSync(configPath);
// watch for changes assuming it is a local file
if (!isRemotePath(configPath)) {
watch(configPath, () => {
console.log(`Config file ${configPath} changed. Re-syncing...`);
scheduleDeclarativeConfigSync(configPath);
});
}
}
}
(async () => {