@wearesage/schema
Version:
A flexible schema definition and validation system for TypeScript with multi-database support
71 lines (57 loc) • 2.31 kB
JavaScript
const fs = require('fs');
const path = require('path');
const entitiesDir = '/Users/zach/dev/@wearesage/core/schema/test-entities';
// Get all entity files
const entityFiles = fs.readdirSync(entitiesDir).filter(f => f.endsWith('.ts'));
console.log('Found entity files:', entityFiles);
// For each entity file, we need to:
// 1. Find all import('./EntityName').EntityName patterns
// 2. Add proper imports at the top
// 3. Replace the dynamic imports with direct references
entityFiles.forEach(file => {
const filePath = path.join(entitiesDir, file);
let content = fs.readFileSync(filePath, 'utf8');
console.log(`\nProcessing ${file}...`);
// Find all dynamic imports
const importMatches = content.match(/import\('\.\/\w+'\)\.\w+/g);
if (importMatches) {
console.log('Found dynamic imports:', importMatches);
// Extract entity names
const entityImports = new Set();
importMatches.forEach(match => {
const entityMatch = match.match(/import\('\.\/(\w+)'\)\.(\w+)/);
if (entityMatch) {
const [, fileName, entityName] = entityMatch;
entityImports.add({fileName, entityName});
}
});
// Add imports at the top (after existing imports)
const importStatements = Array.from(entityImports).map(({fileName, entityName}) => {
return `import { ${entityName} } from './${fileName}';`;
}).join('\n');
// Find the last import line
const lines = content.split('\n');
let lastImportLine = -1;
for (let i = 0; i < lines.length; i++) {
if (lines[i].startsWith('import ')) {
lastImportLine = i;
}
}
if (lastImportLine !== -1) {
// Insert new imports after the last import
lines.splice(lastImportLine + 1, 0, importStatements);
content = lines.join('\n');
}
// Replace dynamic imports with direct references
entityImports.forEach(({fileName, entityName}) => {
const pattern = new RegExp(`import\\('\\.\\/${fileName}'\\)\\.${entityName}`, 'g');
content = content.replace(pattern, entityName);
});
// Write the fixed content back
fs.writeFileSync(filePath, content, 'utf8');
console.log(`Fixed ${file}`);
} else {
console.log('No dynamic imports found');
}
});
console.log('\nDone! All imports fixed.');