refactor(api): streamline agent script directory resolution
- 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:
@@ -1,6 +1,5 @@
|
|||||||
import { readFileSync } from 'node:fs'
|
import { readFileSync } from 'node:fs'
|
||||||
import { dirname, join } from 'node:path'
|
import { join } from 'node:path'
|
||||||
import { fileURLToPath } from 'node:url'
|
|
||||||
import type { FastifyPluginAsync } from 'fastify'
|
import type { FastifyPluginAsync } from 'fastify'
|
||||||
import { repos } from '@evofw/db'
|
import { repos } from '@evofw/db'
|
||||||
import { enrollBodySchema, applyReportBodySchema } from '@evofw/shared'
|
import { enrollBodySchema, applyReportBodySchema } from '@evofw/shared'
|
||||||
@@ -9,10 +8,10 @@ import { hashToken } from '../plugins/auth.js'
|
|||||||
import { evaluateAgentPolicy } from '../services/policy/evaluate.js'
|
import { evaluateAgentPolicy } from '../services/policy/evaluate.js'
|
||||||
import { renderMikrotikPolicyRsc } from '../services/policy/mikrotik-rsc.js'
|
import { renderMikrotikPolicyRsc } from '../services/policy/mikrotik-rsc.js'
|
||||||
import { AppError } from '../plugins/error-handler.js'
|
import { AppError } from '../plugins/error-handler.js'
|
||||||
|
import { resolveAgentScriptsDir } from '../services/agent-scripts-path.js'
|
||||||
import { resolveAndRenderInstall } from '../services/install-links.js'
|
import { resolveAndRenderInstall } from '../services/install-links.js'
|
||||||
|
|
||||||
const __dirname = dirname(fileURLToPath(import.meta.url))
|
const scriptsDir = resolveAgentScriptsDir()
|
||||||
const scriptsDir = join(__dirname, '../agent-scripts')
|
|
||||||
|
|
||||||
export const agentRoutes: FastifyPluginAsync<{ config: AppConfig }> = async (
|
export const agentRoutes: FastifyPluginAsync<{ config: AppConfig }> = async (
|
||||||
app,
|
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(', ')})`,
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,12 +1,11 @@
|
|||||||
import { readFileSync } from 'node:fs'
|
import { readFileSync } from 'node:fs'
|
||||||
import { dirname, join } from 'node:path'
|
import { join } from 'node:path'
|
||||||
import { fileURLToPath } from 'node:url'
|
|
||||||
import type { Db } from '@evofw/db'
|
import type { Db } from '@evofw/db'
|
||||||
import { repos } from '@evofw/db'
|
import { repos } from '@evofw/db'
|
||||||
import { AppError } from '../plugins/error-handler.js'
|
import { AppError } from '../plugins/error-handler.js'
|
||||||
|
import { resolveAgentScriptsDir } from './agent-scripts-path.js'
|
||||||
|
|
||||||
const __dirname = dirname(fileURLToPath(import.meta.url))
|
const scriptsDir = resolveAgentScriptsDir()
|
||||||
const scriptsDir = join(__dirname, '../agent-scripts')
|
|
||||||
|
|
||||||
const ID_ALPHABET =
|
const ID_ALPHABET =
|
||||||
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
|
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
|
||||||
|
|||||||
Reference in New Issue
Block a user