refactor(api): streamline agent script directory resolution
Build and Push EvoFirewall Docker Image / build-and-push (push) Successful in 1m53s
Build and Push EvoFirewall Docker Image / create-release (push) Skipped

- Removed the use of `dirname` and `fileURLToPath` for determining the agent scripts directory.
- Introduced `resolveAgentScriptsDir` function to simplify and centralize the path resolution logic in both `agent.ts` and `install-links.ts`.

Co-authored-by: Cursor <[email protected]>
This commit is contained in:
Denozordec
2026-07-21 18:28:50 +07:00
co-authored by Cursor
parent 39e4856caa
commit 08e7c4d755
3 changed files with 28 additions and 8 deletions
+3 -4
View File
@@ -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,
@@ -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(', ')})`,
)
}
+3 -4
View File
@@ -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'