diff --git a/apps/api/src/routes/agent.ts b/apps/api/src/routes/agent.ts index 706052c..e4c5569 100644 --- a/apps/api/src/routes/agent.ts +++ b/apps/api/src/routes/agent.ts @@ -1,6 +1,5 @@ import { readFileSync } from 'node:fs' -import { dirname, join } from 'node:path' -import { fileURLToPath } from 'node:url' +import { join } from 'node:path' import type { FastifyPluginAsync } from 'fastify' import { repos } from '@evofw/db' import { enrollBodySchema, applyReportBodySchema } from '@evofw/shared' @@ -9,10 +8,10 @@ import { hashToken } from '../plugins/auth.js' import { evaluateAgentPolicy } from '../services/policy/evaluate.js' import { renderMikrotikPolicyRsc } from '../services/policy/mikrotik-rsc.js' import { AppError } from '../plugins/error-handler.js' +import { resolveAgentScriptsDir } from '../services/agent-scripts-path.js' import { resolveAndRenderInstall } from '../services/install-links.js' -const __dirname = dirname(fileURLToPath(import.meta.url)) -const scriptsDir = join(__dirname, '../agent-scripts') +const scriptsDir = resolveAgentScriptsDir() export const agentRoutes: FastifyPluginAsync<{ config: AppConfig }> = async ( app, diff --git a/apps/api/src/services/agent-scripts-path.ts b/apps/api/src/services/agent-scripts-path.ts new file mode 100644 index 0000000..a52412a --- /dev/null +++ b/apps/api/src/services/agent-scripts-path.ts @@ -0,0 +1,22 @@ +import { existsSync } from 'node:fs' +import { dirname, join } from 'node:path' +import { fileURLToPath } from 'node:url' + +/** + * Resolve agent-scripts directory for both: + * - prod bundle: dist/server.js → dist/agent-scripts + * - dev (tsx): src/services|routes → src/agent-scripts + */ +export function resolveAgentScriptsDir(fromUrl: string = import.meta.url): string { + const here = dirname(fileURLToPath(fromUrl)) + const candidates = [ + join(here, 'agent-scripts'), + join(here, '../agent-scripts'), + ] + for (const dir of candidates) { + if (existsSync(join(dir, 'install.sh'))) return dir + } + throw new Error( + `agent-scripts not found (looked for install.sh in: ${candidates.join(', ')})`, + ) +} diff --git a/apps/api/src/services/install-links.ts b/apps/api/src/services/install-links.ts index 9456b0f..d6ff41d 100644 --- a/apps/api/src/services/install-links.ts +++ b/apps/api/src/services/install-links.ts @@ -1,12 +1,11 @@ import { readFileSync } from 'node:fs' -import { dirname, join } from 'node:path' -import { fileURLToPath } from 'node:url' +import { join } from 'node:path' import type { Db } from '@evofw/db' import { repos } from '@evofw/db' import { AppError } from '../plugins/error-handler.js' +import { resolveAgentScriptsDir } from './agent-scripts-path.js' -const __dirname = dirname(fileURLToPath(import.meta.url)) -const scriptsDir = join(__dirname, '../agent-scripts') +const scriptsDir = resolveAgentScriptsDir() const ID_ALPHABET = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'