diff --git a/services/collector/.swc/plugins/linux_x86_64_23.0.0/ba28a2b8397ad27b179a5e64e0ebece60ef3e459eae37c2b570efd3ee4bc29fa.wasmer-v7 b/services/collector/.swc/plugins/linux_x86_64_23.0.0/ba28a2b8397ad27b179a5e64e0ebece60ef3e459eae37c2b570efd3ee4bc29fa.wasmer-v7 new file mode 100644 index 0000000..215c86c Binary files /dev/null and b/services/collector/.swc/plugins/linux_x86_64_23.0.0/ba28a2b8397ad27b179a5e64e0ebece60ef3e459eae37c2b570efd3ee4bc29fa.wasmer-v7 differ diff --git a/services/collector/nest-cli.json b/services/collector/nest-cli.json index 165a3b6..0196212 100644 --- a/services/collector/nest-cli.json +++ b/services/collector/nest-cli.json @@ -3,12 +3,7 @@ "collection": "@nestjs/schematics", "sourceRoot": "src", "compilerOptions": { - "deleteOutDir": true, - "builder": { - "type": "swc", - "options": { - "swcrcPath": ".swcrc" - } - } + "builder": "swc", + "deleteOutDir": true } } diff --git a/services/collector/scripts/esm-loader.mjs b/services/collector/scripts/esm-loader.mjs deleted file mode 100644 index 2032e43..0000000 --- a/services/collector/scripts/esm-loader.mjs +++ /dev/null @@ -1,78 +0,0 @@ -/** - * Custom ESM Loader that adds .js extensions to imports without extensions - */ - -import { resolve as pathResolve, dirname, extname, isAbsolute } from 'node:path'; -import { fileURLToPath, pathToFileURL } from 'node:url'; -import { existsSync } from 'node:fs'; - -/** - * Resolve hook - adds .js extension to imports without extensions - */ -export async function resolve(specifier, context, nextResolve) { - // Log everything unconditionally - console.log(`[loader] resolve called: ${specifier}`); - - // Skip node built-ins and package imports - if (specifier.startsWith('node:') || specifier.startsWith('@') || !specifier.includes('/')) { - return nextResolve(specifier, context); - } - - // Check if specifier needs .js extension added - if (!extname(specifier)) { - // Calculate the absolute path - let absolutePath; - - if (isAbsolute(specifier)) { - absolutePath = specifier; - } else if (specifier.startsWith('./') || specifier.startsWith('../')) { - // For relative imports, resolve from parent directory - const { parentURL } = context; - let parentDir; - if (parentURL) { - try { - parentDir = dirname(fileURLToPath(parentURL)); - } catch { - parentDir = process.cwd(); - } - } else { - parentDir = process.cwd(); - } - absolutePath = pathResolve(parentDir, specifier); - } else { - // Not a path we handle - return nextResolve(specifier, context); - } - - console.log(`[loader] absolute path: ${absolutePath}`); - - // Try adding .js extension - const pathWithJs = absolutePath + '.js'; - console.log(`[loader] trying: ${pathWithJs}, exists: ${existsSync(pathWithJs)}`); - - if (existsSync(pathWithJs)) { - const url = pathToFileURL(pathWithJs).href; - console.log(`[loader] resolved to: ${url}`); - return { - shortCircuit: true, - url, - }; - } - - // Try as directory with index.js - const pathAsIndex = pathResolve(absolutePath, 'index.js'); - console.log(`[loader] trying index: ${pathAsIndex}, exists: ${existsSync(pathAsIndex)}`); - - if (existsSync(pathAsIndex)) { - const url = pathToFileURL(pathAsIndex).href; - console.log(`[loader] resolved to: ${url}`); - return { - shortCircuit: true, - url, - }; - } - } - - // Default resolution - return nextResolve(specifier, context); -} diff --git a/services/collector/scripts/fix-esm-imports.mjs b/services/collector/scripts/fix-esm-imports.mjs new file mode 100644 index 0000000..cbf0890 --- /dev/null +++ b/services/collector/scripts/fix-esm-imports.mjs @@ -0,0 +1,67 @@ +#!/usr/bin/env node +/** + * Fix ESM Imports + * + * Adds .js extensions to relative imports in compiled JavaScript files. + * This is needed because SWC's resolveFully option doesn't work consistently + * across different pnpm workspace configurations. + */ + +import { readdir, readFile, writeFile, stat } from 'node:fs/promises'; +import { join, extname } from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __dirname = fileURLToPath(new URL('.', import.meta.url)); +const distDir = join(__dirname, '..', 'dist'); + +// Regex to match relative imports without .js extension +// Matches: from './path' or from '../path' but not from './path.js' +const importRegex = /from\s+(['"])(\.\.?\/[^'"]+)(? { + // Don't add .js if it's already there or if it's a JSON import + if (importPath.endsWith('.js') || importPath.endsWith('.json')) { + return match; + } + return `from ${quote}${importPath}.js${quote}`; + }); + + if (content !== newContent) { + await writeFile(filePath, newContent, 'utf8'); + return true; + } + return false; +} + +async function processDirectory(dir) { + const entries = await readdir(dir); + let modified = 0; + + for (const entry of entries) { + const fullPath = join(dir, entry); + const stats = await stat(fullPath); + + if (stats.isDirectory()) { + modified += await processDirectory(fullPath); + } else if (extname(entry) === '.js') { + if (await processFile(fullPath)) { + modified++; + } + } + } + + return modified; +} + +console.log('šŸ”§ Fixing ESM imports in dist/...'); + +try { + const modified = await processDirectory(distDir); + console.log(`āœ… Fixed ${modified} files\n`); +} catch (error) { + console.error('āŒ Error fixing imports:', error.message); + process.exit(1); +} diff --git a/services/collector/scripts/register-loader.mjs b/services/collector/scripts/register-loader.mjs deleted file mode 100644 index 6100e61..0000000 --- a/services/collector/scripts/register-loader.mjs +++ /dev/null @@ -1,13 +0,0 @@ -/** - * Register the ESM loader using the modern API - */ - -import { register } from 'node:module'; -import { pathToFileURL } from 'node:url'; -import { dirname, join } from 'node:path'; -import { fileURLToPath } from 'node:url'; - -const __dirname = dirname(fileURLToPath(import.meta.url)); -const loaderPath = join(__dirname, 'esm-loader.mjs'); - -register(pathToFileURL(loaderPath).href, import.meta.url); diff --git a/services/collector/scripts/verify-circular-deps.mjs b/services/collector/scripts/verify-circular-deps.mjs index 3244e48..b3a6556 100644 --- a/services/collector/scripts/verify-circular-deps.mjs +++ b/services/collector/scripts/verify-circular-deps.mjs @@ -8,7 +8,6 @@ * Usage: node scripts/verify-circular-deps.mjs */ -import { spawn } from 'node:child_process'; import { existsSync } from 'node:fs'; import { join, dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; @@ -33,43 +32,23 @@ if (!existsSync(appModulePath)) { process.exit(1); } -// Spawn a child process with the custom ESM loader registered via --import -const registerPath = join(__dirname, 'register-loader.mjs'); -const child = spawn( - 'node', - [ - '--import', - registerPath, - '-e', - ` - process.env.NODE_ENV = 'test'; - process.env.SKIP_BOOTSTRAP = 'true'; - import('${appModulePath}').then(() => { - console.log('āœ… No circular dependency issues detected'); - console.log(' All modules and entities loaded successfully\\n'); - process.exit(0); - }).catch((error) => { - console.error('āŒ Circular dependency detected!\\n'); - console.error('Error:', error.message); - console.error('\\nStack trace:'); - console.error(error.stack); - console.error('\\nšŸ’” Hint: Look for entities with bidirectional relations.'); - console.error(' Use string references in decorators: @ManyToOne(\\'EntityName\\', ...)\\n'); - process.exit(1); - }); - `, - ], - { - stdio: 'inherit', - cwd: projectRoot, - env: { - ...process.env, - NODE_ENV: 'test', - SKIP_BOOTSTRAP: 'true', - }, - } -); +// Set environment to avoid side effects +process.env.NODE_ENV = 'test'; +process.env.SKIP_BOOTSTRAP = 'true'; -child.on('exit', (code) => { - process.exit(code ?? 1); -}); +try { + // Dynamically import the AppModule to check for circular dependencies + await import(appModulePath); + + console.log('āœ… No circular dependency issues detected'); + console.log(' All modules and entities loaded successfully\n'); + process.exit(0); +} catch (error) { + console.error('āŒ Circular dependency detected!\n'); + console.error('Error:', error.message); + console.error('\nStack trace:'); + console.error(error.stack); + console.error('\nšŸ’” Hint: Look for entities with bidirectional relations.'); + console.error(" Use string references in decorators: @ManyToOne('EntityName', ...)\n"); + process.exit(1); +}