@wearesage/schema
Version:
A flexible schema definition and validation system for TypeScript with multi-database support
80 lines (66 loc) • 2.75 kB
JavaScript
const fs = require('fs');
const path = require('path');
// Find all .ts files in the project
function findTSFiles(dir) {
const files = [];
const items = fs.readdirSync(dir);
for (const item of items) {
const fullPath = path.join(dir, item);
const stat = fs.statSync(fullPath);
if (stat.isDirectory() && item !== 'node_modules' && item !== 'dist') {
files.push(...findTSFiles(fullPath));
} else if (item.endsWith('.ts') || item.endsWith('.js')) {
files.push(fullPath);
}
}
return files;
}
const projectRoot = '/Users/zach/dev/@wearesage/core/schema';
const files = findTSFiles(projectRoot);
files.forEach(file => {
let content = fs.readFileSync(file, 'utf8');
let modified = false;
// Replace imports of Labels from neo4j to core/decorators
const labelsFromNeo4jPattern = /import\s*{\s*([^}]*?)Labels([^}]*?)}\s*from\s*['"]([^'"]*neo4j[^'"]*)['"]/g;
content = content.replace(labelsFromNeo4jPattern, (match, before, after, importPath) => {
console.log(`Fixing Labels import in ${file}`);
modified = true;
// Remove Labels from the import
let otherImports = (before + after).replace(/,\s*,/g, ',').replace(/^\s*,\s*/, '').replace(/\s*,\s*$/, '').trim();
if (otherImports) {
// Keep other imports from neo4j
return `import { ${otherImports} } from '${importPath}'`;
} else {
// Remove the entire import line if only Labels was imported
return '';
}
});
// Add Labels import from core/decorators if it was used
if (modified && content.includes('Labels')) {
// Check if there's already an import from core/decorators
const coreImportPattern = /import\s*{\s*([^}]*?)}\s*from\s*['"]([^'"]*core\/decorators[^'"]*)['"]/;
const coreImportMatch = content.match(coreImportPattern);
if (coreImportMatch) {
// Add Labels to existing core/decorators import
const existingImports = coreImportMatch[1];
if (!existingImports.includes('Labels')) {
const newImports = existingImports + ', Labels';
content = content.replace(coreImportPattern, `import { ${newImports} } from '${coreImportMatch[2]}'`);
}
} else {
// Add new import for Labels from core/decorators
const firstImport = content.match(/^import\s+.*$/m);
if (firstImport) {
const insertPos = content.indexOf(firstImport[0]);
content = content.slice(0, insertPos) +
`import { Labels } from '../core/decorators';\n` +
content.slice(insertPos);
}
}
}
if (modified) {
fs.writeFileSync(file, content, 'utf8');
console.log(`Fixed Labels import in ${file}`);
}
});
console.log('Done! Fixed all Labels imports.');