@memberjunction/metadata-sync 2.50.0 → 2.52.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/README.md +423 -2
  2. package/dist/commands/file-reset/index.d.ts +15 -0
  3. package/dist/commands/file-reset/index.js +221 -0
  4. package/dist/commands/file-reset/index.js.map +1 -0
  5. package/dist/commands/pull/index.d.ts +1 -0
  6. package/dist/commands/pull/index.js +82 -10
  7. package/dist/commands/pull/index.js.map +1 -1
  8. package/dist/commands/push/index.d.ts +21 -0
  9. package/dist/commands/push/index.js +589 -45
  10. package/dist/commands/push/index.js.map +1 -1
  11. package/dist/commands/validate/index.d.ts +15 -0
  12. package/dist/commands/validate/index.js +149 -0
  13. package/dist/commands/validate/index.js.map +1 -0
  14. package/dist/commands/watch/index.js +39 -1
  15. package/dist/commands/watch/index.js.map +1 -1
  16. package/dist/config.d.ts +7 -0
  17. package/dist/config.js.map +1 -1
  18. package/dist/index.d.ts +3 -0
  19. package/dist/index.js +5 -1
  20. package/dist/index.js.map +1 -1
  21. package/dist/lib/file-backup-manager.d.ts +90 -0
  22. package/dist/lib/file-backup-manager.js +186 -0
  23. package/dist/lib/file-backup-manager.js.map +1 -0
  24. package/dist/lib/provider-utils.d.ts +2 -2
  25. package/dist/lib/provider-utils.js +3 -4
  26. package/dist/lib/provider-utils.js.map +1 -1
  27. package/dist/lib/sync-engine.js +29 -3
  28. package/dist/lib/sync-engine.js.map +1 -1
  29. package/dist/services/FormattingService.d.ts +45 -0
  30. package/dist/services/FormattingService.js +564 -0
  31. package/dist/services/FormattingService.js.map +1 -0
  32. package/dist/services/ValidationService.d.ts +110 -0
  33. package/dist/services/ValidationService.js +737 -0
  34. package/dist/services/ValidationService.js.map +1 -0
  35. package/dist/types/validation.d.ts +98 -0
  36. package/dist/types/validation.js +97 -0
  37. package/dist/types/validation.js.map +1 -0
  38. package/oclif.manifest.json +205 -39
  39. package/package.json +7 -7
@@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E,CAAC;gBACtC,yDAAyD;gBACzD,MAAM,UAAU,GAAe,MAAM,kBAAE,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC;gBAC/D,UAAU,CAAC,IAAI,GAAG;oBAChB,YAAY,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;oBACtC,QAAQ,EAAE,UAAU,CAAC,IAAI,EAAE,QAAQ,IAAI,EAAE;iBAC1C,CAAC;gBACF,MAAM,kBAAE,CAAC,SAAS,CAAC,YAAY,EAAE,UAAU,EAAE,EAAE,MAAM,EAAE,CAAC,EAAE,CAAC,CAAC;gBAE5D,IAAI,CAAC,GAAG,CAAC,6BAA6B,YAAY,8BAA8B,CAAC,CAAC;YACpF,CAAC;QACH,CAAC;IACH,CAAC;;AA/SH,wBAgTC","sourcesContent":["import { Command, Flags } from '@oclif/core';\nimport fs from 'fs-extra';\nimport path from 'path';\nimport chokidar from 'chokidar';\nimport ora from 'ora-classic';\nimport { loadMJConfig, loadSyncConfig, loadEntityConfig } from '../../config';\nimport { SyncEngine, RecordData } from '../../lib/sync-engine';\nimport { initializeProvider, findEntityDirectories, getSystemUser } from '../../lib/provider-utils';\nimport { BaseEntity } from '@memberjunction/core';\nimport { getSyncEngine, resetSyncEngine } from '../../lib/singleton-manager';\nimport { cleanupProvider } from '../../lib/provider-utils';\n\nexport default class Watch extends Command {\n static description = 'Watch for file changes and automatically push to database';\n \n static examples = [\n `<%= config.bin %> <%= command.id %>`,\n `<%= config.bin %> <%= command.id %> --dir=\"ai-prompts\"`,\n ];\n \n static flags = {\n dir: Flags.string({ description: 'Specific entity directory to watch' }),\n };\n \n private syncEngine!: SyncEngine;\n private syncConfig: any;\n private debounceTimers: Map<string, NodeJS.Timeout> = new Map();\n \n async run(): Promise<void> {\n const { flags } = await this.parse(Watch);\n const spinner = ora();\n \n try {\n // Load configurations\n spinner.start('Loading configuration');\n const mjConfig = loadMJConfig();\n if (!mjConfig) {\n this.error('No mj.config.cjs found in current directory or parent directories');\n }\n \n this.syncConfig = await loadSyncConfig(process.cwd());\n \n // Stop spinner before provider initialization (which logs to console)\n spinner.stop();\n \n // Initialize data provider\n const provider = await initializeProvider(mjConfig);\n \n // Initialize sync engine using singleton pattern\n this.syncEngine = await getSyncEngine(getSystemUser());\n \n // Show success after all initialization is complete\n spinner.succeed('Configuration and metadata loaded');\n \n // Find entity directories to watch\n const entityDirs = findEntityDirectories(process.cwd(), flags.dir);\n \n if (entityDirs.length === 0) {\n this.error('No entity directories found');\n }\n \n this.log(`Watching ${entityDirs.length} entity ${entityDirs.length === 1 ? 'directory' : 'directories'} for changes`);\n \n // Set up watchers\n const watchers: chokidar.FSWatcher[] = [];\n \n for (const entityDir of entityDirs) {\n const entityConfig = await loadEntityConfig(entityDir);\n if (!entityConfig) {\n this.warn(`Skipping ${entityDir} - no valid entity configuration`);\n continue;\n }\n \n this.log(`Watching ${entityConfig.entity} in ${entityDir}`);\n \n // Watch for JSON files and external files\n // All JSON files will be watched, but only dot-prefixed ones will be processed\n const patterns = [\n path.join(entityDir, entityConfig.filePattern || '**/*.json'),\n path.join(entityDir, '**/*.md'),\n path.join(entityDir, '**/*.txt'),\n path.join(entityDir, '**/*.html'),\n path.join(entityDir, '**/*.liquid'),\n path.join(entityDir, '**/*.sql')\n ];\n \n const ignored = [\n '**/node_modules/**',\n '**/.git/**',\n '**/.mj-sync.json',\n '**/.mj-folder.json',\n '**/*.backup',\n ...(this.syncConfig?.watch?.ignorePatterns || [])\n ];\n \n const watcher = chokidar.watch(patterns, {\n ignored,\n persistent: true,\n ignoreInitial: true\n });\n \n watcher\n .on('add', (filePath) => this.handleFileChange(filePath, 'added', entityDir, entityConfig))\n .on('change', (filePath) => this.handleFileChange(filePath, 'changed', entityDir, entityConfig))\n .on('unlink', (filePath) => this.handleFileChange(filePath, 'deleted', entityDir, entityConfig));\n \n watchers.push(watcher);\n }\n \n this.log('\\nPress Ctrl+C to stop watching');\n \n // Keep process alive\n process.stdin.resume();\n \n // Cleanup on exit\n process.on('SIGINT', async () => {\n this.log('\\nStopping watchers...');\n watchers.forEach(w => w.close());\n // Reset sync engine singleton\n resetSyncEngine();\n // Clean up database connection\n await cleanupProvider();\n process.exit(0);\n });\n \n } catch (error) {\n spinner.fail('Watch failed');\n \n // Enhanced error logging for debugging\n this.log('\\n=== Watch Error Details ===');\n this.log(`Error type: ${error?.constructor?.name || 'Unknown'}`);\n this.log(`Error message: ${error instanceof Error ? error.message : String(error)}`);\n \n if (error instanceof Error && error.stack) {\n this.log(`\\nStack trace:`);\n this.log(error.stack);\n }\n \n // Log context information\n this.log(`\\nContext:`);\n this.log(`- Working directory: ${process.cwd()}`);\n this.log(`- Specific directory: ${flags.dir || 'none (watching all directories)'}`);\n this.log(`- Flags: ${JSON.stringify(flags, null, 2)}`);\n \n // Check if error is related to common issues\n const errorMessage = error instanceof Error ? error.message : String(error);\n if (errorMessage.includes('No entity directories found')) {\n this.log(`\\nHint: This appears to be an entity directory configuration issue.`);\n this.log(`Make sure directories have .mj-sync.json files or run \"mj-sync init\".`);\n } else if (errorMessage.includes('database') || errorMessage.includes('connection')) {\n this.log(`\\nHint: This appears to be a database connectivity issue.`);\n this.log(`Check your mj.config.cjs configuration and database connectivity.`);\n } else if (errorMessage.includes('config') || errorMessage.includes('mj.config.cjs')) {\n this.log(`\\nHint: This appears to be a configuration file issue.`);\n this.log(`Make sure mj.config.cjs exists and is properly configured.`);\n } else if (errorMessage.includes('ENOENT') || errorMessage.includes('no such file')) {\n this.log(`\\nHint: File or directory access issue.`);\n this.log(`Check that the directories exist and are accessible.`);\n } else if (errorMessage.includes('chokidar') || errorMessage.includes('watch')) {\n this.log(`\\nHint: File watching system issue.`);\n this.log(`Check file system permissions and available file descriptors.`);\n }\n \n // Reset sync engine singleton\n resetSyncEngine();\n // Clean up database connection\n await cleanupProvider();\n this.error(error as Error);\n }\n }\n \n private async handleFileChange(\n filePath: string,\n event: string,\n entityDir: string,\n entityConfig: any\n ): Promise<void> {\n // Clear existing debounce timer\n const existingTimer = this.debounceTimers.get(filePath);\n if (existingTimer) {\n clearTimeout(existingTimer);\n }\n \n // Set new debounce timer\n const debounceMs = this.syncConfig?.watch?.debounceMs || 1000;\n const timer = setTimeout(async () => {\n this.debounceTimers.delete(filePath);\n \n try {\n const relativePath = path.relative(entityDir, filePath);\n this.log(`\\nFile ${event}: ${relativePath}`);\n \n if (event === 'deleted') {\n // Handle deletion\n this.log('File deletion detected - manual database cleanup may be required');\n } else if (filePath.endsWith('.json')) {\n // Handle JSON file change\n await this.syncJsonFile(filePath, entityDir, entityConfig);\n } else {\n // Handle external file change\n await this.syncExternalFile(filePath, entityDir, entityConfig);\n }\n } catch (error) {\n this.warn(`Failed to sync ${filePath}: ${(error as any).message || error}`);\n }\n }, debounceMs);\n \n this.debounceTimers.set(filePath, timer);\n }\n \n private async syncJsonFile(\n filePath: string,\n entityDir: string,\n entityConfig: any\n ): Promise<void> {\n const recordData: RecordData = await fs.readJson(filePath);\n \n // Build defaults\n const defaults = await this.syncEngine.buildDefaults(filePath, entityConfig);\n \n // Load or create entity\n let entity: BaseEntity | null = null;\n let isNew = false;\n \n if (recordData.primaryKey) {\n entity = await this.syncEngine.loadEntity(entityConfig.entity, recordData.primaryKey);\n }\n \n if (!entity) {\n // New record\n entity = await this.syncEngine.createEntityObject(entityConfig.entity);\n entity.NewRecord();\n isNew = true;\n }\n \n // Apply defaults first\n for (const [field, value] of Object.entries(defaults)) {\n if (field in entity) {\n (entity as any)[field] = value;\n }\n }\n \n // Apply record fields\n for (const [field, value] of Object.entries(recordData.fields)) {\n if (field in entity) {\n const processedValue = await this.syncEngine.processFieldValue(value, path.dirname(filePath));\n (entity as any)[field] = processedValue;\n }\n }\n \n // Save the record\n const saved = await entity.Save();\n if (!saved) {\n const message = entity.LatestResult?.Message;\n if (message) {\n throw new Error(`Failed to save record: ${message}`);\n }\n \n const errors = entity.LatestResult?.Errors?.map(err => \n typeof err === 'string' ? err : (err?.message || JSON.stringify(err))\n )?.join(', ') || 'Unknown error';\n throw new Error(`Failed to save record: ${errors}`);\n }\n \n this.log(`Successfully ${isNew ? 'created' : 'updated'} ${entityConfig.entity} record`);\n \n // Update the local file with new primary key if created\n if (isNew) {\n const entityInfo = this.syncEngine.getEntityInfo(entityConfig.entity);\n if (entityInfo) {\n const newPrimaryKey: Record<string, any> = {};\n for (const pk of entityInfo.PrimaryKeys) {\n newPrimaryKey[pk.Name] = entity.Get(pk.Name);\n }\n recordData.primaryKey = newPrimaryKey;\n \n // Update sync metadata\n recordData.sync = {\n lastModified: new Date().toISOString(),\n checksum: this.syncEngine.calculateChecksum(recordData.fields)\n };\n \n // Write back to file\n await fs.writeJson(filePath, recordData, { spaces: 2 });\n }\n }\n }\n \n private async syncExternalFile(\n filePath: string,\n entityDir: string,\n entityConfig: any\n ): Promise<void> {\n // Find the corresponding JSON file\n const fileName = path.basename(filePath);\n const parts = fileName.split('.');\n \n if (parts.length >= 3) {\n // Format: uuid.fieldname.ext\n const jsonFileName = `${parts[0]}.json`;\n const fieldName = parts[1];\n const jsonFilePath = path.join(path.dirname(filePath), jsonFileName);\n \n if (await fs.pathExists(jsonFilePath)) {\n // Update the JSON file's sync metadata to trigger a sync\n const recordData: RecordData = await fs.readJson(jsonFilePath);\n recordData.sync = {\n lastModified: new Date().toISOString(),\n checksum: recordData.sync?.checksum || ''\n };\n await fs.writeJson(jsonFilePath, recordData, { spaces: 2 });\n \n this.log(`Updated sync metadata for ${jsonFileName} due to external file change`);\n }\n }\n }\n}"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/commands/watch/index.ts"],"names":[],"mappings":";;;;;AAAA,sCAA6C;AAC7C,wDAA0B;AAC1B,gDAAwB;AACxB,wDAAgC;AAChC,8DAA8B;AAC9B,yCAA8E;AAE9E,6DAAoG;AAEpG,mEAA6E;AAC7E,6DAA2D;AAE3D,MAAqB,KAAM,SAAQ,cAAO;IACxC,MAAM,CAAC,WAAW,GAAG,2DAA2D,CAAC;IAEjF,MAAM,CAAC,QAAQ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Y,GAAG,cAAI,CAAC,IAAI,CAAC,cAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,YAAY,CAAC,CAAC;YAErE,IAAI,MAAM,kBAAE,CAAC,UAAU,CAAC,YAAY,CAAC,EAAE,CAAC;gBACtC,yDAAyD;gBACzD,MAAM,UAAU,GAAe,MAAM,kBAAE,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC;gBAC/D,UAAU,CAAC,IAAI,GAAG;oBAChB,YAAY,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;oBACtC,QAAQ,EAAE,UAAU,CAAC,IAAI,EAAE,QAAQ,IAAI,EAAE;iBAC1C,CAAC;gBACF,MAAM,kBAAE,CAAC,SAAS,CAAC,YAAY,EAAE,UAAU,EAAE,EAAE,MAAM,EAAE,CAAC,EAAE,CAAC,CAAC;gBAE5D,IAAI,CAAC,GAAG,CAAC,6BAA6B,YAAY,8BAA8B,CAAC,CAAC;YACpF,CAAC;QACH,CAAC;IACH,CAAC;;AAtVH,wBAuVC","sourcesContent":["import { Command, Flags } from '@oclif/core';\nimport fs from 'fs-extra';\nimport path from 'path';\nimport chokidar from 'chokidar';\nimport ora from 'ora-classic';\nimport { loadMJConfig, loadSyncConfig, loadEntityConfig } from '../../config';\nimport { SyncEngine, RecordData } from '../../lib/sync-engine';\nimport { initializeProvider, findEntityDirectories, getSystemUser } from '../../lib/provider-utils';\nimport { BaseEntity } from '@memberjunction/core';\nimport { getSyncEngine, resetSyncEngine } from '../../lib/singleton-manager';\nimport { cleanupProvider } from '../../lib/provider-utils';\n\nexport default class Watch extends Command {\n static description = 'Watch for file changes and automatically push to database';\n \n static examples = [\n `<%= config.bin %> <%= command.id %>`,\n `<%= config.bin %> <%= command.id %> --dir=\"ai-prompts\"`,\n ];\n \n static flags = {\n dir: Flags.string({ description: 'Specific entity directory to watch' }),\n };\n \n private syncEngine!: SyncEngine;\n private syncConfig: any;\n private debounceTimers: Map<string, NodeJS.Timeout> = new Map();\n \n async run(): Promise<void> {\n const { flags } = await this.parse(Watch);\n const spinner = ora();\n \n try {\n // Load configurations\n spinner.start('Loading configuration');\n const mjConfig = loadMJConfig();\n if (!mjConfig) {\n this.error('No mj.config.cjs found in current directory or parent directories');\n }\n \n this.syncConfig = await loadSyncConfig(process.cwd());\n \n // Stop spinner before provider initialization (which logs to console)\n spinner.stop();\n \n // Initialize data provider\n const provider = await initializeProvider(mjConfig);\n \n // Initialize sync engine using singleton pattern\n this.syncEngine = await getSyncEngine(getSystemUser());\n \n // Show success after all initialization is complete\n spinner.succeed('Configuration and metadata loaded');\n \n // Find entity directories to watch\n const entityDirs = findEntityDirectories(process.cwd(), flags.dir);\n \n if (entityDirs.length === 0) {\n this.error('No entity directories found');\n }\n \n this.log(`Watching ${entityDirs.length} entity ${entityDirs.length === 1 ? 'directory' : 'directories'} for changes`);\n \n // Set up watchers\n const watchers: chokidar.FSWatcher[] = [];\n \n for (const entityDir of entityDirs) {\n const entityConfig = await loadEntityConfig(entityDir);\n if (!entityConfig) {\n this.warn(`Skipping ${entityDir} - no valid entity configuration`);\n continue;\n }\n \n this.log(`Watching ${entityConfig.entity} in ${entityDir}`);\n \n // Watch for JSON files and external files\n // All JSON files will be watched, but only dot-prefixed ones will be processed\n const patterns = [\n path.join(entityDir, entityConfig.filePattern || '**/*.json'),\n path.join(entityDir, '**/*.md'),\n path.join(entityDir, '**/*.txt'),\n path.join(entityDir, '**/*.html'),\n path.join(entityDir, '**/*.liquid'),\n path.join(entityDir, '**/*.sql')\n ];\n \n const ignored = [\n '**/node_modules/**',\n '**/.git/**',\n '**/.mj-sync.json',\n '**/.mj-folder.json',\n '**/*.backup',\n ...(this.syncConfig?.watch?.ignorePatterns || [])\n ];\n \n const watcher = chokidar.watch(patterns, {\n ignored,\n persistent: true,\n ignoreInitial: true\n });\n \n watcher\n .on('add', (filePath) => this.handleFileChange(filePath, 'added', entityDir, entityConfig))\n .on('change', (filePath) => this.handleFileChange(filePath, 'changed', entityDir, entityConfig))\n .on('unlink', (filePath) => this.handleFileChange(filePath, 'deleted', entityDir, entityConfig));\n \n watchers.push(watcher);\n }\n \n this.log('\\nPress Ctrl+C to stop watching');\n \n // Keep process alive\n process.stdin.resume();\n \n // Cleanup on exit\n process.on('SIGINT', async () => {\n this.log('\\nStopping watchers...');\n watchers.forEach(w => w.close());\n // Reset sync engine singleton\n resetSyncEngine();\n // Clean up database connection\n await cleanupProvider();\n process.exit(0);\n });\n \n } catch (error) {\n spinner.fail('Watch failed');\n \n // Enhanced error logging for debugging\n this.log('\\n=== Watch Error Details ===');\n this.log(`Error type: ${error?.constructor?.name || 'Unknown'}`);\n this.log(`Error message: ${error instanceof Error ? error.message : String(error)}`);\n \n if (error instanceof Error && error.stack) {\n this.log(`\\nStack trace:`);\n this.log(error.stack);\n }\n \n // Log context information\n this.log(`\\nContext:`);\n this.log(`- Working directory: ${process.cwd()}`);\n this.log(`- Specific directory: ${flags.dir || 'none (watching all directories)'}`);\n this.log(`- Flags: ${JSON.stringify(flags, null, 2)}`);\n \n // Check if error is related to common issues\n const errorMessage = error instanceof Error ? error.message : String(error);\n if (errorMessage.includes('No entity directories found')) {\n this.log(`\\nHint: This appears to be an entity directory configuration issue.`);\n this.log(`Make sure directories have .mj-sync.json files or run \"mj-sync init\".`);\n } else if (errorMessage.includes('database') || errorMessage.includes('connection')) {\n this.log(`\\nHint: This appears to be a database connectivity issue.`);\n this.log(`Check your mj.config.cjs configuration and database connectivity.`);\n } else if (errorMessage.includes('config') || errorMessage.includes('mj.config.cjs')) {\n this.log(`\\nHint: This appears to be a configuration file issue.`);\n this.log(`Make sure mj.config.cjs exists and is properly configured.`);\n } else if (errorMessage.includes('ENOENT') || errorMessage.includes('no such file')) {\n this.log(`\\nHint: File or directory access issue.`);\n this.log(`Check that the directories exist and are accessible.`);\n } else if (errorMessage.includes('chokidar') || errorMessage.includes('watch')) {\n this.log(`\\nHint: File watching system issue.`);\n this.log(`Check file system permissions and available file descriptors.`);\n }\n \n // Reset sync engine singleton\n resetSyncEngine();\n // Clean up database connection\n await cleanupProvider();\n this.error(error as Error);\n }\n }\n \n private async handleFileChange(\n filePath: string,\n event: string,\n entityDir: string,\n entityConfig: any\n ): Promise<void> {\n // Clear existing debounce timer\n const existingTimer = this.debounceTimers.get(filePath);\n if (existingTimer) {\n clearTimeout(existingTimer);\n }\n \n // Set new debounce timer\n const debounceMs = this.syncConfig?.watch?.debounceMs || 1000;\n const timer = setTimeout(async () => {\n this.debounceTimers.delete(filePath);\n \n try {\n const relativePath = path.relative(entityDir, filePath);\n this.log(`\\nFile ${event}: ${relativePath}`);\n \n if (event === 'deleted') {\n // Handle deletion\n this.log('File deletion detected - manual database cleanup may be required');\n } else if (filePath.endsWith('.json')) {\n // Handle JSON file change\n await this.syncJsonFile(filePath, entityDir, entityConfig);\n } else {\n // Handle external file change\n await this.syncExternalFile(filePath, entityDir, entityConfig);\n }\n } catch (error) {\n this.warn(`Failed to sync ${filePath}: ${(error as any).message || error}`);\n }\n }, debounceMs);\n \n this.debounceTimers.set(filePath, timer);\n }\n \n private async syncJsonFile(\n filePath: string,\n entityDir: string,\n entityConfig: any\n ): Promise<void> {\n const recordData: RecordData = await fs.readJson(filePath);\n \n // Build defaults\n const defaults = await this.syncEngine.buildDefaults(filePath, entityConfig);\n \n // Load or create entity\n let entity: BaseEntity | null = null;\n let isNew = false;\n \n if (recordData.primaryKey) {\n entity = await this.syncEngine.loadEntity(entityConfig.entity, recordData.primaryKey);\n }\n \n if (!entity) {\n // New record\n entity = await this.syncEngine.createEntityObject(entityConfig.entity);\n entity.NewRecord();\n isNew = true;\n }\n \n // Apply defaults first\n for (const [field, value] of Object.entries(defaults)) {\n if (field in entity) {\n (entity as any)[field] = value;\n }\n }\n \n // Apply record fields\n for (const [field, value] of Object.entries(recordData.fields)) {\n if (field in entity) {\n const processedValue = await this.syncEngine.processFieldValue(value, path.dirname(filePath));\n (entity as any)[field] = processedValue;\n }\n }\n \n // Check if the record is dirty before saving\n let wasActuallyUpdated = false;\n if (!isNew && entity.Dirty) {\n // Record is dirty, get the changes\n const changes = entity.GetChangesSinceLastSave();\n const changeKeys = Object.keys(changes);\n if (changeKeys.length > 0) {\n wasActuallyUpdated = true;\n \n // Get primary key info for display\n const entityInfo = this.syncEngine.getEntityInfo(entityConfig.entity);\n const primaryKeyDisplay: string[] = [];\n if (entityInfo) {\n for (const pk of entityInfo.PrimaryKeys) {\n primaryKeyDisplay.push(`${pk.Name}: ${entity.Get(pk.Name)}`);\n }\n }\n \n this.log(''); // Add newline before update output\n this.log(`📝 Updating ${entityConfig.entity} record:`);\n if (primaryKeyDisplay.length > 0) {\n this.log(` Primary Key: ${primaryKeyDisplay.join(', ')}`);\n }\n this.log(` Changes:`);\n for (const fieldName of changeKeys) {\n const field = entity.GetFieldByName(fieldName);\n const oldValue = field ? field.OldValue : undefined;\n const newValue = (changes as any)[fieldName];\n this.log(` ${fieldName}: ${oldValue} → ${newValue}`);\n }\n }\n } else if (isNew) {\n wasActuallyUpdated = true;\n }\n \n // Save the record\n const saved = await entity.Save();\n if (!saved) {\n const message = entity.LatestResult?.Message;\n if (message) {\n throw new Error(`Failed to save record: ${message}`);\n }\n \n const errors = entity.LatestResult?.Errors?.map(err => \n typeof err === 'string' ? err : (err?.message || JSON.stringify(err))\n )?.join(', ') || 'Unknown error';\n throw new Error(`Failed to save record: ${errors}`);\n }\n \n if (wasActuallyUpdated) {\n this.log(`Successfully ${isNew ? 'created' : 'updated'} ${entityConfig.entity} record`);\n } else {\n this.log(`No changes detected for ${entityConfig.entity} record - skipped update`);\n }\n \n // Update the local file with new primary key if created\n if (isNew) {\n const entityInfo = this.syncEngine.getEntityInfo(entityConfig.entity);\n if (entityInfo) {\n const newPrimaryKey: Record<string, any> = {};\n for (const pk of entityInfo.PrimaryKeys) {\n newPrimaryKey[pk.Name] = entity.Get(pk.Name);\n }\n recordData.primaryKey = newPrimaryKey;\n \n // Update sync metadata\n recordData.sync = {\n lastModified: new Date().toISOString(),\n checksum: this.syncEngine.calculateChecksum(recordData.fields)\n };\n \n // Write back to file\n await fs.writeJson(filePath, recordData, { spaces: 2 });\n }\n }\n }\n \n private async syncExternalFile(\n filePath: string,\n entityDir: string,\n entityConfig: any\n ): Promise<void> {\n // Find the corresponding JSON file\n const fileName = path.basename(filePath);\n const parts = fileName.split('.');\n \n if (parts.length >= 3) {\n // Format: uuid.fieldname.ext\n const jsonFileName = `${parts[0]}.json`;\n const fieldName = parts[1];\n const jsonFilePath = path.join(path.dirname(filePath), jsonFileName);\n \n if (await fs.pathExists(jsonFilePath)) {\n // Update the JSON file's sync metadata to trigger a sync\n const recordData: RecordData = await fs.readJson(jsonFilePath);\n recordData.sync = {\n lastModified: new Date().toISOString(),\n checksum: recordData.sync?.checksum || ''\n };\n await fs.writeJson(jsonFilePath, recordData, { spaces: 2 });\n \n this.log(`Updated sync metadata for ${jsonFileName} due to external file change`);\n }\n }\n }\n}"]}
package/dist/config.d.ts CHANGED
@@ -46,6 +46,8 @@ export interface MJConfig {
46
46
  export interface SyncConfig {
47
47
  /** Version of the sync configuration format */
48
48
  version: string;
49
+ /** Glob pattern for finding data files (defaults to "*.json") */
50
+ filePattern?: string;
49
51
  /**
50
52
  * Directory processing order (only applies to root-level config, not inherited by subdirectories)
51
53
  * Specifies the order in which subdirectories should be processed to handle dependencies.
@@ -58,6 +60,11 @@ export interface SyncConfig {
58
60
  validateBeforePush?: boolean;
59
61
  /** Whether to require user confirmation before push */
60
62
  requireConfirmation?: boolean;
63
+ /**
64
+ * Whether to automatically create new records when a primaryKey exists but record is not found
65
+ * Defaults to false - will warn instead of creating
66
+ */
67
+ autoCreateMissingRecords?: boolean;
61
68
  };
62
69
  /** SQL logging configuration (only applies to root-level config, not inherited by subdirectories) */
63
70
  sqlLogging?: {
@@ -1 +1 @@
1
- {"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;GAUG;;;;;;AAGH,gDAAwB;AACxB,wDAA0B;AAC1B,yDAAq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,GAAW;IAChD,MAAM,UAAU,GAAG,cAAI,CAAC,IAAI,CAAC,GAAG,EAAE,iBAAiB,CAAC,CAAC;IAErD,IAAI,MAAM,kBAAE,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC;QACpC,IAAI,CAAC;YACH,OAAO,MAAM,kBAAE,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;QACvC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,KAAK,CAAC,8BAA8B,EAAE,KAAK,CAAC,CAAC;YACrD,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAbD,4CAaC","sourcesContent":["/**\n * @fileoverview Configuration types and loaders for MetadataSync\n * @module config\n * \n * This module defines configuration interfaces and provides utilities for loading\n * various configuration files used by the MetadataSync tool. It supports:\n * - MemberJunction database configuration (mj.config.cjs)\n * - Sync configuration (.mj-sync.json)\n * - Entity-specific configuration (.mj-sync.json with entity field)\n * - Folder-level defaults (.mj-folder.json)\n */\n\nimport { cosmiconfigSync } from 'cosmiconfig';\nimport path from 'path';\nimport fs from 'fs-extra';\nimport { configManager } from './lib/config-manager';\n\n/**\n * MemberJunction database configuration\n * \n * Defines connection parameters and settings for connecting to the MemberJunction\n * database. Typically loaded from mj.config.cjs in the project root.\n */\nexport interface MJConfig {\n /** Database server hostname or IP address */\n dbHost: string;\n /** Database server port (defaults to 1433 for SQL Server) */\n dbPort?: number;\n /** Database name to connect to */\n dbDatabase: string;\n /** Database authentication username */\n dbUsername: string;\n /** Database authentication password */\n dbPassword: string;\n /** Whether to trust the server certificate (Y/N) */\n dbTrustServerCertificate?: string;\n /** Whether to encrypt the connection (Y/N, auto-detected for Azure SQL) */\n dbEncrypt?: string;\n /** SQL Server instance name (for named instances) */\n dbInstanceName?: string;\n /** Schema name for MemberJunction core tables (defaults to __mj) */\n mjCoreSchema?: string;\n /** Allow additional properties for extensibility */\n [key: string]: any;\n}\n\n/**\n * Global sync configuration\n * \n * Defines settings that apply to the entire sync process, including push/pull\n * behaviors and watch mode configuration. Stored in .mj-sync.json at the root.\n */\nexport interface SyncConfig {\n /** Version of the sync configuration format */\n version: string;\n /** \n * Directory processing order (only applies to root-level config, not inherited by subdirectories)\n * Specifies the order in which subdirectories should be processed to handle dependencies.\n * Directories not listed in this array will be processed after the ordered ones in alphabetical order.\n */\n directoryOrder?: string[];\n /** Push command configuration */\n push?: {\n /** Whether to validate records before pushing to database */\n validateBeforePush?: boolean;\n /** Whether to require user confirmation before push */\n requireConfirmation?: boolean;\n };\n /** SQL logging configuration (only applies to root-level config, not inherited by subdirectories) */\n sqlLogging?: {\n /** Whether to enable SQL logging during push operations */\n enabled?: boolean;\n /** Directory to output SQL log files (relative to command execution directory, defaults to './sql_logging') */\n outputDirectory?: string;\n /** Whether to format SQL as migration-ready files with Flyway schema placeholders */\n formatAsMigration?: boolean;\n };\n /** Watch command configuration */\n watch?: {\n /** Milliseconds to wait before processing file changes */\n debounceMs?: number;\n /** File patterns to ignore during watch */\n ignorePatterns?: string[];\n };\n}\n\n/**\n * Configuration for related entity synchronization\n * \n * Defines how to pull and push related entities that have foreign key relationships\n * with a parent entity. Supports nested relationships for deep object graphs.\n * NEW: Supports automatic recursive patterns for self-referencing entities.\n */\nexport interface RelatedEntityConfig {\n /** Name of the related entity to sync */\n entity: string;\n /** Field name that contains the foreign key reference to parent (e.g., \"PromptID\") */\n foreignKey: string;\n /** Optional SQL filter to apply when pulling related records */\n filter?: string;\n /** \n * Enable recursive fetching for self-referencing entities\n * When true, automatically fetches all levels of the hierarchy until no more children found\n */\n recursive?: boolean;\n /** \n * Maximum depth for recursive fetching (optional, defaults to 10)\n * Prevents infinite loops and controls memory usage\n * Only applies when recursive is true\n */\n maxDepth?: number;\n /** Fields to externalize to separate files for this related entity */\n externalizeFields?: string[] | {\n [fieldName: string]: {\n /** File extension to use (e.g., \".md\", \".txt\", \".html\") */\n extension?: string;\n }\n } | Array<{\n /** Field name to externalize */\n field: string;\n /** Pattern for the output file. Supports placeholders from the entity */\n pattern: string;\n }>;\n /** Fields to exclude from the pulled data for this related entity */\n excludeFields?: string[];\n /** Foreign key fields to convert to @lookup references for this related entity */\n lookupFields?: {\n [fieldName: string]: {\n entity: string;\n field: string;\n };\n };\n /** Nested related entities for deep object graphs */\n relatedEntities?: Record<string, RelatedEntityConfig>;\n}\n\n/**\n * Entity-specific configuration\n * \n * Defines settings for a specific entity type within a directory. Stored in\n * .mj-sync.json files that contain an \"entity\" field. Supports defaults,\n * file patterns, and related entity configuration.\n */\nexport interface EntityConfig {\n /** Name of the entity this directory contains */\n entity: string;\n /** Glob pattern for finding data files (defaults to \"*.json\") */\n filePattern?: string;\n /** Default field values applied to all records in this directory */\n defaults?: Record<string, any>;\n /** Pull command specific configuration */\n pull?: {\n /** Glob pattern for finding existing files to update (defaults to filePattern) */\n filePattern?: string;\n /** Whether to create new files for records not found locally */\n createNewFileIfNotFound?: boolean;\n /** Filename for new records when createNewFileIfNotFound is true */\n newFileName?: string;\n /** Whether to append multiple new records to a single file */\n appendRecordsToExistingFile?: boolean;\n /** Whether to update existing records found in local files */\n updateExistingRecords?: boolean;\n /** Fields to preserve during updates (never overwrite these) */\n preserveFields?: string[];\n /** Strategy for merging updates: \"overwrite\" | \"merge\" | \"skip\" */\n mergeStrategy?: 'overwrite' | 'merge' | 'skip';\n /** Create backup files before updating existing files */\n backupBeforeUpdate?: boolean;\n /** Directory name for backup files (defaults to \".backups\") */\n backupDirectory?: string;\n /** SQL filter to apply when pulling records from database */\n filter?: string;\n /** Configuration for pulling related entities */\n relatedEntities?: Record<string, RelatedEntityConfig>;\n /** Fields to externalize to separate files with optional configuration */\n externalizeFields?: string[] | {\n [fieldName: string]: {\n /** File extension to use (e.g., \".md\", \".txt\", \".html\") */\n extension?: string;\n }\n } | Array<{\n /** Field name to externalize */\n field: string;\n /** Pattern for the output file. Supports placeholders:\n * - {Name}: Entity's name field value\n * - {ID}: Entity's ID\n * - {FieldName}: The field being externalized\n * - Any other {FieldName} from the entity\n * Example: \"@file:templates/{Name}.template.md\"\n */\n pattern: string;\n }>;\n /** Fields to exclude from the pulled data (e.g., [\"TemplateID\"]) */\n excludeFields?: string[];\n /** Foreign key fields to convert to @lookup references */\n lookupFields?: {\n /** Field name in this entity (e.g., \"CategoryID\") */\n [fieldName: string]: {\n /** Target entity name (e.g., \"AI Prompt Categories\") */\n entity: string;\n /** Field in target entity to use for lookup (e.g., \"Name\") */\n field: string;\n };\n };\n };\n}\n\n/**\n * Folder-level configuration\n * \n * Defines default values that cascade down to all subdirectories. Stored in\n * .mj-folder.json files. Child folders can override parent defaults.\n */\nexport interface FolderConfig {\n /** Default field values that apply to all entities in this folder and subfolders */\n defaults: Record<string, any>;\n}\n\n/**\n * Load MemberJunction configuration from the filesystem\n * \n * Searches for mj.config.cjs starting from the current directory and walking up\n * the directory tree. Uses cosmiconfig for flexible configuration loading.\n * \n * @returns MJConfig object if found, null if not found or invalid\n * \n * @example\n * ```typescript\n * const config = loadMJConfig();\n * if (config) {\n * console.log(`Connecting to ${config.dbHost}:${config.dbPort || 1433}`);\n * }\n * ```\n */\nexport function loadMJConfig(): MJConfig | null {\n return configManager.loadMJConfig();\n}\n\n/**\n * Load sync configuration from a directory\n * \n * Loads .mj-sync.json file from the specified directory. This file can contain\n * either global sync configuration (no entity field) or entity-specific\n * configuration (with entity field).\n * \n * @param dir - Directory path to load configuration from\n * @returns Promise resolving to SyncConfig if found and valid, null otherwise\n * \n * @example\n * ```typescript\n * const syncConfig = await loadSyncConfig('/path/to/project');\n * if (syncConfig?.push?.requireConfirmation) {\n * // Show confirmation prompt\n * }\n * ```\n */\nexport async function loadSyncConfig(dir: string): Promise<SyncConfig | null> {\n const configPath = path.join(dir, '.mj-sync.json');\n \n if (await fs.pathExists(configPath)) {\n try {\n return await fs.readJson(configPath);\n } catch (error) {\n console.error('Error loading sync config:', error);\n return null;\n }\n }\n \n return null;\n}\n\n/**\n * Load entity-specific configuration from a directory\n * \n * Loads .mj-sync.json file that contains an \"entity\" field, indicating this\n * directory contains data for a specific entity type. Returns null if the\n * file doesn't exist or doesn't contain an entity field.\n * \n * @param dir - Directory path to load configuration from\n * @returns Promise resolving to EntityConfig if found and valid, null otherwise\n * \n * @example\n * ```typescript\n * const entityConfig = await loadEntityConfig('./ai-prompts');\n * if (entityConfig) {\n * console.log(`Directory contains ${entityConfig.entity} records`);\n * }\n * ```\n */\nexport async function loadEntityConfig(dir: string): Promise<EntityConfig | null> {\n const configPath = path.join(dir, '.mj-sync.json');\n \n if (await fs.pathExists(configPath)) {\n try {\n const config = await fs.readJson(configPath);\n if (config.entity) {\n return config;\n }\n } catch (error) {\n console.error('Error loading entity config:', error);\n }\n }\n \n return null;\n}\n\n/**\n * Load folder-level configuration\n * \n * Loads .mj-folder.json file that contains default values to be applied to\n * all entities in this folder and its subfolders. Used for cascading defaults\n * in deep directory structures.\n * \n * @param dir - Directory path to load configuration from\n * @returns Promise resolving to FolderConfig if found and valid, null otherwise\n * \n * @example\n * ```typescript\n * const folderConfig = await loadFolderConfig('./templates');\n * if (folderConfig?.defaults) {\n * // Apply folder defaults to records\n * }\n * ```\n */\nexport async function loadFolderConfig(dir: string): Promise<FolderConfig | null> {\n const configPath = path.join(dir, '.mj-folder.json');\n \n if (await fs.pathExists(configPath)) {\n try {\n return await fs.readJson(configPath);\n } catch (error) {\n console.error('Error loading folder config:', error);\n return null;\n }\n }\n \n return null;\n}"]}
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;GAUG;;;;;;AAGH,gDAAwB;AACxB,wDAA0B;AAC1B,yDAAqD;AAkNrD;;;;;;;;;;;;;;;GAeG;AACH,SAAgB,YAAY;IAC1B,OAAO,8BAAa,CAAC,YAAY,EAAE,CAAC;AACtC,CAAC;AAFD,oCAEC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACI,KAAK,UAAU,cAAc,CAAC,GAAW;IAC9C,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E,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;QACvC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,KAAK,CAAC,8BAA8B,EAAE,KAAK,CAAC,CAAC;YACrD,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAbD,4CAaC","sourcesContent":["/**\n * @fileoverview Configuration types and loaders for MetadataSync\n * @module config\n * \n * This module defines configuration interfaces and provides utilities for loading\n * various configuration files used by the MetadataSync tool. It supports:\n * - MemberJunction database configuration (mj.config.cjs)\n * - Sync configuration (.mj-sync.json)\n * - Entity-specific configuration (.mj-sync.json with entity field)\n * - Folder-level defaults (.mj-folder.json)\n */\n\nimport { cosmiconfigSync } from 'cosmiconfig';\nimport path from 'path';\nimport fs from 'fs-extra';\nimport { configManager } from './lib/config-manager';\n\n/**\n * MemberJunction database configuration\n * \n * Defines connection parameters and settings for connecting to the MemberJunction\n * database. Typically loaded from mj.config.cjs in the project root.\n */\nexport interface MJConfig {\n /** Database server hostname or IP address */\n dbHost: string;\n /** Database server port (defaults to 1433 for SQL Server) */\n dbPort?: number;\n /** Database name to connect to */\n dbDatabase: string;\n /** Database authentication username */\n dbUsername: string;\n /** Database authentication password */\n dbPassword: string;\n /** Whether to trust the server certificate (Y/N) */\n dbTrustServerCertificate?: string;\n /** Whether to encrypt the connection (Y/N, auto-detected for Azure SQL) */\n dbEncrypt?: string;\n /** SQL Server instance name (for named instances) */\n dbInstanceName?: string;\n /** Schema name for MemberJunction core tables (defaults to __mj) */\n mjCoreSchema?: string;\n /** Allow additional properties for extensibility */\n [key: string]: any;\n}\n\n/**\n * Global sync configuration\n * \n * Defines settings that apply to the entire sync process, including push/pull\n * behaviors and watch mode configuration. Stored in .mj-sync.json at the root.\n */\nexport interface SyncConfig {\n /** Version of the sync configuration format */\n version: string;\n /** Glob pattern for finding data files (defaults to \"*.json\") */\n filePattern?: string;\n /** \n * Directory processing order (only applies to root-level config, not inherited by subdirectories)\n * Specifies the order in which subdirectories should be processed to handle dependencies.\n * Directories not listed in this array will be processed after the ordered ones in alphabetical order.\n */\n directoryOrder?: string[];\n /** Push command configuration */\n push?: {\n /** Whether to validate records before pushing to database */\n validateBeforePush?: boolean;\n /** Whether to require user confirmation before push */\n requireConfirmation?: boolean;\n /** \n * Whether to automatically create new records when a primaryKey exists but record is not found\n * Defaults to false - will warn instead of creating\n */\n autoCreateMissingRecords?: boolean;\n };\n /** SQL logging configuration (only applies to root-level config, not inherited by subdirectories) */\n sqlLogging?: {\n /** Whether to enable SQL logging during push operations */\n enabled?: boolean;\n /** Directory to output SQL log files (relative to command execution directory, defaults to './sql_logging') */\n outputDirectory?: string;\n /** Whether to format SQL as migration-ready files with Flyway schema placeholders */\n formatAsMigration?: boolean;\n };\n /** Watch command configuration */\n watch?: {\n /** Milliseconds to wait before processing file changes */\n debounceMs?: number;\n /** File patterns to ignore during watch */\n ignorePatterns?: string[];\n };\n}\n\n/**\n * Configuration for related entity synchronization\n * \n * Defines how to pull and push related entities that have foreign key relationships\n * with a parent entity. Supports nested relationships for deep object graphs.\n * NEW: Supports automatic recursive patterns for self-referencing entities.\n */\nexport interface RelatedEntityConfig {\n /** Name of the related entity to sync */\n entity: string;\n /** Field name that contains the foreign key reference to parent (e.g., \"PromptID\") */\n foreignKey: string;\n /** Optional SQL filter to apply when pulling related records */\n filter?: string;\n /** \n * Enable recursive fetching for self-referencing entities\n * When true, automatically fetches all levels of the hierarchy until no more children found\n */\n recursive?: boolean;\n /** \n * Maximum depth for recursive fetching (optional, defaults to 10)\n * Prevents infinite loops and controls memory usage\n * Only applies when recursive is true\n */\n maxDepth?: number;\n /** Fields to externalize to separate files for this related entity */\n externalizeFields?: string[] | {\n [fieldName: string]: {\n /** File extension to use (e.g., \".md\", \".txt\", \".html\") */\n extension?: string;\n }\n } | Array<{\n /** Field name to externalize */\n field: string;\n /** Pattern for the output file. Supports placeholders from the entity */\n pattern: string;\n }>;\n /** Fields to exclude from the pulled data for this related entity */\n excludeFields?: string[];\n /** Foreign key fields to convert to @lookup references for this related entity */\n lookupFields?: {\n [fieldName: string]: {\n entity: string;\n field: string;\n };\n };\n /** Nested related entities for deep object graphs */\n relatedEntities?: Record<string, RelatedEntityConfig>;\n}\n\n/**\n * Entity-specific configuration\n * \n * Defines settings for a specific entity type within a directory. Stored in\n * .mj-sync.json files that contain an \"entity\" field. Supports defaults,\n * file patterns, and related entity configuration.\n */\nexport interface EntityConfig {\n /** Name of the entity this directory contains */\n entity: string;\n /** Glob pattern for finding data files (defaults to \"*.json\") */\n filePattern?: string;\n /** Default field values applied to all records in this directory */\n defaults?: Record<string, any>;\n /** Pull command specific configuration */\n pull?: {\n /** Glob pattern for finding existing files to update (defaults to filePattern) */\n filePattern?: string;\n /** Whether to create new files for records not found locally */\n createNewFileIfNotFound?: boolean;\n /** Filename for new records when createNewFileIfNotFound is true */\n newFileName?: string;\n /** Whether to append multiple new records to a single file */\n appendRecordsToExistingFile?: boolean;\n /** Whether to update existing records found in local files */\n updateExistingRecords?: boolean;\n /** Fields to preserve during updates (never overwrite these) */\n preserveFields?: string[];\n /** Strategy for merging updates: \"overwrite\" | \"merge\" | \"skip\" */\n mergeStrategy?: 'overwrite' | 'merge' | 'skip';\n /** Create backup files before updating existing files */\n backupBeforeUpdate?: boolean;\n /** Directory name for backup files (defaults to \".backups\") */\n backupDirectory?: string;\n /** SQL filter to apply when pulling records from database */\n filter?: string;\n /** Configuration for pulling related entities */\n relatedEntities?: Record<string, RelatedEntityConfig>;\n /** Fields to externalize to separate files with optional configuration */\n externalizeFields?: string[] | {\n [fieldName: string]: {\n /** File extension to use (e.g., \".md\", \".txt\", \".html\") */\n extension?: string;\n }\n } | Array<{\n /** Field name to externalize */\n field: string;\n /** Pattern for the output file. Supports placeholders:\n * - {Name}: Entity's name field value\n * - {ID}: Entity's ID\n * - {FieldName}: The field being externalized\n * - Any other {FieldName} from the entity\n * Example: \"@file:templates/{Name}.template.md\"\n */\n pattern: string;\n }>;\n /** Fields to exclude from the pulled data (e.g., [\"TemplateID\"]) */\n excludeFields?: string[];\n /** Foreign key fields to convert to @lookup references */\n lookupFields?: {\n /** Field name in this entity (e.g., \"CategoryID\") */\n [fieldName: string]: {\n /** Target entity name (e.g., \"AI Prompt Categories\") */\n entity: string;\n /** Field in target entity to use for lookup (e.g., \"Name\") */\n field: string;\n };\n };\n };\n}\n\n/**\n * Folder-level configuration\n * \n * Defines default values that cascade down to all subdirectories. Stored in\n * .mj-folder.json files. Child folders can override parent defaults.\n */\nexport interface FolderConfig {\n /** Default field values that apply to all entities in this folder and subfolders */\n defaults: Record<string, any>;\n}\n\n/**\n * Load MemberJunction configuration from the filesystem\n * \n * Searches for mj.config.cjs starting from the current directory and walking up\n * the directory tree. Uses cosmiconfig for flexible configuration loading.\n * \n * @returns MJConfig object if found, null if not found or invalid\n * \n * @example\n * ```typescript\n * const config = loadMJConfig();\n * if (config) {\n * console.log(`Connecting to ${config.dbHost}:${config.dbPort || 1433}`);\n * }\n * ```\n */\nexport function loadMJConfig(): MJConfig | null {\n return configManager.loadMJConfig();\n}\n\n/**\n * Load sync configuration from a directory\n * \n * Loads .mj-sync.json file from the specified directory. This file can contain\n * either global sync configuration (no entity field) or entity-specific\n * configuration (with entity field).\n * \n * @param dir - Directory path to load configuration from\n * @returns Promise resolving to SyncConfig if found and valid, null otherwise\n * \n * @example\n * ```typescript\n * const syncConfig = await loadSyncConfig('/path/to/project');\n * if (syncConfig?.push?.requireConfirmation) {\n * // Show confirmation prompt\n * }\n * ```\n */\nexport async function loadSyncConfig(dir: string): Promise<SyncConfig | null> {\n const configPath = path.join(dir, '.mj-sync.json');\n \n if (await fs.pathExists(configPath)) {\n try {\n return await fs.readJson(configPath);\n } catch (error) {\n console.error('Error loading sync config:', error);\n return null;\n }\n }\n \n return null;\n}\n\n/**\n * Load entity-specific configuration from a directory\n * \n * Loads .mj-sync.json file that contains an \"entity\" field, indicating this\n * directory contains data for a specific entity type. Returns null if the\n * file doesn't exist or doesn't contain an entity field.\n * \n * @param dir - Directory path to load configuration from\n * @returns Promise resolving to EntityConfig if found and valid, null otherwise\n * \n * @example\n * ```typescript\n * const entityConfig = await loadEntityConfig('./ai-prompts');\n * if (entityConfig) {\n * console.log(`Directory contains ${entityConfig.entity} records`);\n * }\n * ```\n */\nexport async function loadEntityConfig(dir: string): Promise<EntityConfig | null> {\n const configPath = path.join(dir, '.mj-sync.json');\n \n if (await fs.pathExists(configPath)) {\n try {\n const config = await fs.readJson(configPath);\n if (config.entity) {\n return config;\n }\n } catch (error) {\n console.error('Error loading entity config:', error);\n }\n }\n \n return null;\n}\n\n/**\n * Load folder-level configuration\n * \n * Loads .mj-folder.json file that contains default values to be applied to\n * all entities in this folder and its subfolders. Used for cascading defaults\n * in deep directory structures.\n * \n * @param dir - Directory path to load configuration from\n * @returns Promise resolving to FolderConfig if found and valid, null otherwise\n * \n * @example\n * ```typescript\n * const folderConfig = await loadFolderConfig('./templates');\n * if (folderConfig?.defaults) {\n * // Apply folder defaults to records\n * }\n * ```\n */\nexport async function loadFolderConfig(dir: string): Promise<FolderConfig | null> {\n const configPath = path.join(dir, '.mj-folder.json');\n \n if (await fs.pathExists(configPath)) {\n try {\n return await fs.readJson(configPath);\n } catch (error) {\n console.error('Error loading folder config:', error);\n return null;\n }\n }\n \n return null;\n}"]}
package/dist/index.d.ts CHANGED
@@ -1 +1,4 @@
1
1
  export { run } from '@oclif/core';
2
+ export { FileBackupManager } from './lib/file-backup-manager';
3
+ export { SyncEngine } from './lib/sync-engine';
4
+ export type { RecordData } from './lib/sync-engine';
package/dist/index.js CHANGED
@@ -1,6 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.run = void 0;
3
+ exports.SyncEngine = exports.FileBackupManager = exports.run = void 0;
4
4
  var core_1 = require("@oclif/core");
5
5
  Object.defineProperty(exports, "run", { enumerable: true, get: function () { return core_1.run; } });
6
+ var file_backup_manager_1 = require("./lib/file-backup-manager");
7
+ Object.defineProperty(exports, "FileBackupManager", { enumerable: true, get: function () { return file_backup_manager_1.FileBackupManager; } });
8
+ var sync_engine_1 = require("./lib/sync-engine");
9
+ Object.defineProperty(exports, "SyncEngine", { enumerable: true, get: function () { return sync_engine_1.SyncEngine; } });
6
10
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,oCAAkC;AAAzB,2FAAA,GAAG,OAAA","sourcesContent":["export { run } from '@oclif/core';"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,oCAAkC;AAAzB,2FAAA,GAAG,OAAA;AACZ,iEAA8D;AAArD,wHAAA,iBAAiB,OAAA;AAC1B,iDAA+C;AAAtC,yGAAA,UAAU,OAAA","sourcesContent":["export { run } from '@oclif/core';\nexport { FileBackupManager } from './lib/file-backup-manager';\nexport { SyncEngine } from './lib/sync-engine';\nexport type { RecordData } from './lib/sync-engine';"]}
@@ -0,0 +1,90 @@
1
+ /**
2
+ * @fileoverview File backup and rollback manager for MetadataSync operations
3
+ * @module file-backup-manager
4
+ *
5
+ * This module provides functionality to create backups of files before modification
6
+ * and allows rolling back all changes if any operation fails. It ensures atomic
7
+ * file operations by maintaining a temporary backup directory during push operations.
8
+ */
9
+ /**
10
+ * Manages file backups and rollback operations for MetadataSync
11
+ *
12
+ * Creates temporary backups of all modified files during a push operation,
13
+ * allowing atomic rollback of all file changes if any error occurs.
14
+ *
15
+ * @class FileBackupManager
16
+ * @example
17
+ * ```typescript
18
+ * const backupManager = new FileBackupManager();
19
+ * await backupManager.initialize();
20
+ *
21
+ * try {
22
+ * await backupManager.backupFile('/path/to/file.json');
23
+ * // Modify the file
24
+ * await fs.writeJson('/path/to/file.json', newData);
25
+ *
26
+ * // If all operations succeed
27
+ * await backupManager.cleanup();
28
+ * } catch (error) {
29
+ * // Rollback all file changes
30
+ * await backupManager.rollback();
31
+ * throw error;
32
+ * }
33
+ * ```
34
+ */
35
+ export declare class FileBackupManager {
36
+ private backupDir;
37
+ private backups;
38
+ private initialized;
39
+ /**
40
+ * Initialize the backup manager by creating a temporary directory
41
+ *
42
+ * Creates a unique temporary directory for storing file backups during
43
+ * the current operation. Must be called before any backup operations.
44
+ *
45
+ * @returns Promise that resolves when initialization is complete
46
+ * @throws Error if temporary directory creation fails
47
+ */
48
+ initialize(): Promise<void>;
49
+ /**
50
+ * Create a backup of a file before modification
51
+ *
52
+ * Copies the current state of a file to the backup directory. If the file
53
+ * doesn't exist, records that fact for proper rollback handling.
54
+ *
55
+ * @param filePath - Absolute path to the file to backup
56
+ * @returns Promise that resolves when backup is complete
57
+ * @throws Error if backup operation fails
58
+ */
59
+ backupFile(filePath: string): Promise<void>;
60
+ /**
61
+ * Rollback all file changes by restoring from backups
62
+ *
63
+ * Restores all backed-up files to their original state. Files that didn't
64
+ * exist before the operation are deleted. This operation is atomic - either
65
+ * all files are restored or none are.
66
+ *
67
+ * @returns Promise that resolves when rollback is complete
68
+ * @throws Error if any rollback operation fails
69
+ */
70
+ rollback(): Promise<void>;
71
+ /**
72
+ * Clean up backup directory after successful operation
73
+ *
74
+ * Removes all temporary backup files and the backup directory. Should be
75
+ * called after all operations complete successfully.
76
+ *
77
+ * @returns Promise that resolves when cleanup is complete
78
+ */
79
+ cleanup(): Promise<void>;
80
+ /**
81
+ * Get statistics about current backup session
82
+ *
83
+ * @returns Object containing backup statistics
84
+ */
85
+ getStats(): {
86
+ totalBackups: number;
87
+ backupDir: string;
88
+ initialized: boolean;
89
+ };
90
+ }
@@ -0,0 +1,186 @@
1
+ "use strict";
2
+ /**
3
+ * @fileoverview File backup and rollback manager for MetadataSync operations
4
+ * @module file-backup-manager
5
+ *
6
+ * This module provides functionality to create backups of files before modification
7
+ * and allows rolling back all changes if any operation fails. It ensures atomic
8
+ * file operations by maintaining a temporary backup directory during push operations.
9
+ */
10
+ var __importDefault = (this && this.__importDefault) || function (mod) {
11
+ return (mod && mod.__esModule) ? mod : { "default": mod };
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.FileBackupManager = void 0;
15
+ const fs_extra_1 = __importDefault(require("fs-extra"));
16
+ const path_1 = __importDefault(require("path"));
17
+ const os_1 = __importDefault(require("os"));
18
+ const global_1 = require("@memberjunction/global");
19
+ /**
20
+ * Manages file backups and rollback operations for MetadataSync
21
+ *
22
+ * Creates temporary backups of all modified files during a push operation,
23
+ * allowing atomic rollback of all file changes if any error occurs.
24
+ *
25
+ * @class FileBackupManager
26
+ * @example
27
+ * ```typescript
28
+ * const backupManager = new FileBackupManager();
29
+ * await backupManager.initialize();
30
+ *
31
+ * try {
32
+ * await backupManager.backupFile('/path/to/file.json');
33
+ * // Modify the file
34
+ * await fs.writeJson('/path/to/file.json', newData);
35
+ *
36
+ * // If all operations succeed
37
+ * await backupManager.cleanup();
38
+ * } catch (error) {
39
+ * // Rollback all file changes
40
+ * await backupManager.rollback();
41
+ * throw error;
42
+ * }
43
+ * ```
44
+ */
45
+ class FileBackupManager {
46
+ backupDir = '';
47
+ backups = [];
48
+ initialized = false;
49
+ /**
50
+ * Initialize the backup manager by creating a temporary directory
51
+ *
52
+ * Creates a unique temporary directory for storing file backups during
53
+ * the current operation. Must be called before any backup operations.
54
+ *
55
+ * @returns Promise that resolves when initialization is complete
56
+ * @throws Error if temporary directory creation fails
57
+ */
58
+ async initialize() {
59
+ if (this.initialized) {
60
+ throw new Error('FileBackupManager already initialized');
61
+ }
62
+ // Create a unique temporary directory for this session
63
+ const tempRoot = os_1.default.tmpdir();
64
+ const sessionId = (0, global_1.uuidv4)();
65
+ this.backupDir = path_1.default.join(tempRoot, 'mj-metadata-sync', sessionId);
66
+ await fs_extra_1.default.ensureDir(this.backupDir);
67
+ this.initialized = true;
68
+ }
69
+ /**
70
+ * Create a backup of a file before modification
71
+ *
72
+ * Copies the current state of a file to the backup directory. If the file
73
+ * doesn't exist, records that fact for proper rollback handling.
74
+ *
75
+ * @param filePath - Absolute path to the file to backup
76
+ * @returns Promise that resolves when backup is complete
77
+ * @throws Error if backup operation fails
78
+ */
79
+ async backupFile(filePath) {
80
+ if (!this.initialized) {
81
+ throw new Error('FileBackupManager not initialized. Call initialize() first.');
82
+ }
83
+ // Check if we already have a backup for this file
84
+ const existingBackup = this.backups.find(b => b.originalPath === filePath);
85
+ if (existingBackup) {
86
+ // Already backed up, don't backup again
87
+ return;
88
+ }
89
+ const exists = await fs_extra_1.default.pathExists(filePath);
90
+ const backupFileName = `${path_1.default.basename(filePath)}_${Date.now()}_${this.backups.length}`;
91
+ const backupPath = path_1.default.join(this.backupDir, backupFileName);
92
+ const backupEntry = {
93
+ originalPath: filePath,
94
+ backupPath: backupPath,
95
+ existed: exists
96
+ };
97
+ if (exists) {
98
+ // Copy the file to backup location
99
+ await fs_extra_1.default.copy(filePath, backupPath);
100
+ }
101
+ this.backups.push(backupEntry);
102
+ }
103
+ /**
104
+ * Rollback all file changes by restoring from backups
105
+ *
106
+ * Restores all backed-up files to their original state. Files that didn't
107
+ * exist before the operation are deleted. This operation is atomic - either
108
+ * all files are restored or none are.
109
+ *
110
+ * @returns Promise that resolves when rollback is complete
111
+ * @throws Error if any rollback operation fails
112
+ */
113
+ async rollback() {
114
+ if (!this.initialized) {
115
+ return; // Nothing to rollback
116
+ }
117
+ const errors = [];
118
+ // Process backups in reverse order (last modified first)
119
+ for (const backup of this.backups.reverse()) {
120
+ try {
121
+ if (backup.existed) {
122
+ // Restore the original file
123
+ await fs_extra_1.default.copy(backup.backupPath, backup.originalPath, { overwrite: true });
124
+ }
125
+ else {
126
+ // File didn't exist before, remove it
127
+ if (await fs_extra_1.default.pathExists(backup.originalPath)) {
128
+ await fs_extra_1.default.remove(backup.originalPath);
129
+ }
130
+ }
131
+ }
132
+ catch (error) {
133
+ const errorMsg = `Failed to rollback ${backup.originalPath}: ${error instanceof Error ? error.message : String(error)}`;
134
+ errors.push(errorMsg);
135
+ console.error(errorMsg);
136
+ }
137
+ }
138
+ // Clean up backup directory
139
+ try {
140
+ await this.cleanup();
141
+ }
142
+ catch (error) {
143
+ console.error('Failed to cleanup backup directory during rollback:', error);
144
+ }
145
+ if (errors.length > 0) {
146
+ throw new Error(`Rollback completed with errors:\n${errors.join('\n')}`);
147
+ }
148
+ }
149
+ /**
150
+ * Clean up backup directory after successful operation
151
+ *
152
+ * Removes all temporary backup files and the backup directory. Should be
153
+ * called after all operations complete successfully.
154
+ *
155
+ * @returns Promise that resolves when cleanup is complete
156
+ */
157
+ async cleanup() {
158
+ if (!this.initialized || !this.backupDir) {
159
+ return;
160
+ }
161
+ try {
162
+ await fs_extra_1.default.remove(this.backupDir);
163
+ }
164
+ catch (error) {
165
+ // Log but don't throw - cleanup errors shouldn't fail the operation
166
+ console.warn(`Failed to cleanup backup directory ${this.backupDir}:`, error);
167
+ }
168
+ this.backups = [];
169
+ this.initialized = false;
170
+ this.backupDir = '';
171
+ }
172
+ /**
173
+ * Get statistics about current backup session
174
+ *
175
+ * @returns Object containing backup statistics
176
+ */
177
+ getStats() {
178
+ return {
179
+ totalBackups: this.backups.length,
180
+ backupDir: this.backupDir,
181
+ initialized: this.initialized
182
+ };
183
+ }
184
+ }
185
+ exports.FileBackupManager = FileBackupManager;
186
+ //# sourceMappingURL=file-backup-manager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-backup-manager.js","sourceRoot":"","sources":["../../src/lib/file-backup-manager.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;;;;AAEH,wDAA0B;AAC1B,gDAAwB;AACxB,4CAAoB;AACpB,mDAAgD;AAchD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAa,iBAAiB;IACpB,SAAS,GAAW,EAAE,CAAC;IACvB,OAAO,GAAkB,EAAE,CAAC;IAC5B,WAAW,GAAY,KAAK,CAAC;IAErC;;;;;;;;OAQG;IACH,KAAK,CAAC,UAAU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,4BAA4B;QAC5B,IAAI,CAAC;YACH,MAAM,IAAI,CAAC,OAAO,EAAE,CAAC;QACvB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,KAAK,CAAC,qDAAqD,EAAE,KAAK,CAAC,CAAC;QAC9E,CAAC;QAED,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACtB,MAAM,IAAI,KAAK,CAAC,oCAAoC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QAC3E,CAAC;IACH,CAAC;IAED;;;;;;;OAOG;IACH,KAAK,CAAC,OAAO;QACX,IAAI,CAAC,IAAI,CAAC,WAAW,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;YACzC,OAAO;QACT,CAAC;QAED,IAAI,CAAC;YACH,MAAM,kBAAE,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QAClC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,oEAAoE;YACpE,OAAO,CAAC,IAAI,CAAC,sCAAsC,IAAI,CAAC,SAAS,GAAG,EAAE,KAAK,CAAC,CAAC;QAC/E,CAAC;QAED,IAAI,CAAC,OAAO,GAAG,EAAE,CAAC;QAClB,IAAI,CAAC,WAAW,GAAG,KAAK,CAAC;QACzB,IAAI,CAAC,SAAS,GAAG,EAAE,CAAC;IACtB,CAAC;IAED;;;;OAIG;IACH,QAAQ;QACN,OAAO;YACL,YAAY,EAAE,IAAI,CAAC,OAAO,CAAC,MAAM;YACjC,SAAS,EAAE,IAAI,CAAC,SAAS;YACzB,WAAW,EAAE,IAAI,CAAC,WAAW;SAC9B,CAAC;IACJ,CAAC;CACF;AAzJD,8CAyJC","sourcesContent":["/**\n * @fileoverview File backup and rollback manager for MetadataSync operations\n * @module file-backup-manager\n * \n * This module provides functionality to create backups of files before modification\n * and allows rolling back all changes if any operation fails. It ensures atomic\n * file operations by maintaining a temporary backup directory during push operations.\n */\n\nimport fs from 'fs-extra';\nimport path from 'path';\nimport os from 'os';\nimport { uuidv4 } from '@memberjunction/global';\n\n/**\n * Represents a backed-up file with its original and backup paths\n */\ninterface BackupEntry {\n /** Original file path that was backed up */\n originalPath: string;\n /** Path to the backup copy of the file */\n backupPath: string;\n /** Whether the file existed before modification */\n existed: boolean;\n}\n\n/**\n * Manages file backups and rollback operations for MetadataSync\n * \n * Creates temporary backups of all modified files during a push operation,\n * allowing atomic rollback of all file changes if any error occurs.\n * \n * @class FileBackupManager\n * @example\n * ```typescript\n * const backupManager = new FileBackupManager();\n * await backupManager.initialize();\n * \n * try {\n * await backupManager.backupFile('/path/to/file.json');\n * // Modify the file\n * await fs.writeJson('/path/to/file.json', newData);\n * \n * // If all operations succeed\n * await backupManager.cleanup();\n * } catch (error) {\n * // Rollback all file changes\n * await backupManager.rollback();\n * throw error;\n * }\n * ```\n */\nexport class FileBackupManager {\n private backupDir: string = '';\n private backups: BackupEntry[] = [];\n private initialized: boolean = false;\n \n /**\n * Initialize the backup manager by creating a temporary directory\n * \n * Creates a unique temporary directory for storing file backups during\n * the current operation. Must be called before any backup operations.\n * \n * @returns Promise that resolves when initialization is complete\n * @throws Error if temporary directory creation fails\n */\n async initialize(): Promise<void> {\n if (this.initialized) {\n throw new Error('FileBackupManager already initialized');\n }\n \n // Create a unique temporary directory for this session\n const tempRoot = os.tmpdir();\n const sessionId = uuidv4();\n this.backupDir = path.join(tempRoot, 'mj-metadata-sync', sessionId);\n \n await fs.ensureDir(this.backupDir);\n this.initialized = true;\n }\n \n /**\n * Create a backup of a file before modification\n * \n * Copies the current state of a file to the backup directory. If the file\n * doesn't exist, records that fact for proper rollback handling.\n * \n * @param filePath - Absolute path to the file to backup\n * @returns Promise that resolves when backup is complete\n * @throws Error if backup operation fails\n */\n async backupFile(filePath: string): Promise<void> {\n if (!this.initialized) {\n throw new Error('FileBackupManager not initialized. Call initialize() first.');\n }\n \n // Check if we already have a backup for this file\n const existingBackup = this.backups.find(b => b.originalPath === filePath);\n if (existingBackup) {\n // Already backed up, don't backup again\n return;\n }\n \n const exists = await fs.pathExists(filePath);\n const backupFileName = `${path.basename(filePath)}_${Date.now()}_${this.backups.length}`;\n const backupPath = path.join(this.backupDir, backupFileName);\n \n const backupEntry: BackupEntry = {\n originalPath: filePath,\n backupPath: backupPath,\n existed: exists\n };\n \n if (exists) {\n // Copy the file to backup location\n await fs.copy(filePath, backupPath);\n }\n \n this.backups.push(backupEntry);\n }\n \n /**\n * Rollback all file changes by restoring from backups\n * \n * Restores all backed-up files to their original state. Files that didn't\n * exist before the operation are deleted. This operation is atomic - either\n * all files are restored or none are.\n * \n * @returns Promise that resolves when rollback is complete\n * @throws Error if any rollback operation fails\n */\n async rollback(): Promise<void> {\n if (!this.initialized) {\n return; // Nothing to rollback\n }\n \n const errors: string[] = [];\n \n // Process backups in reverse order (last modified first)\n for (const backup of this.backups.reverse()) {\n try {\n if (backup.existed) {\n // Restore the original file\n await fs.copy(backup.backupPath, backup.originalPath, { overwrite: true });\n } else {\n // File didn't exist before, remove it\n if (await fs.pathExists(backup.originalPath)) {\n await fs.remove(backup.originalPath);\n }\n }\n } catch (error) {\n const errorMsg = `Failed to rollback ${backup.originalPath}: ${error instanceof Error ? error.message : String(error)}`;\n errors.push(errorMsg);\n console.error(errorMsg);\n }\n }\n \n // Clean up backup directory\n try {\n await this.cleanup();\n } catch (error) {\n console.error('Failed to cleanup backup directory during rollback:', error);\n }\n \n if (errors.length > 0) {\n throw new Error(`Rollback completed with errors:\\n${errors.join('\\n')}`);\n }\n }\n \n /**\n * Clean up backup directory after successful operation\n * \n * Removes all temporary backup files and the backup directory. Should be\n * called after all operations complete successfully.\n * \n * @returns Promise that resolves when cleanup is complete\n */\n async cleanup(): Promise<void> {\n if (!this.initialized || !this.backupDir) {\n return;\n }\n \n try {\n await fs.remove(this.backupDir);\n } catch (error) {\n // Log but don't throw - cleanup errors shouldn't fail the operation\n console.warn(`Failed to cleanup backup directory ${this.backupDir}:`, error);\n }\n \n this.backups = [];\n this.initialized = false;\n this.backupDir = '';\n }\n \n /**\n * Get statistics about current backup session\n * \n * @returns Object containing backup statistics\n */\n getStats(): { totalBackups: number; backupDir: string; initialized: boolean } {\n return {\n totalBackups: this.backups.length,\n backupDir: this.backupDir,\n initialized: this.initialized\n };\n }\n}"]}
@@ -73,8 +73,8 @@ export declare function getSystemUser(): UserInfo;
73
73
  * @example
74
74
  * ```typescript
75
75
  * const provider = getDataProvider();
76
- * if (provider?.createSqlLogger) {
77
- * const logger = await provider.createSqlLogger('/path/to/log.sql');
76
+ * if (provider?.CreateSqlLogger) {
77
+ * const logger = await provider.CreateSqlLogger('/path/to/log.sql');
78
78
  * }
79
79
  * ```
80
80
  */
@@ -92,8 +92,7 @@ async function initializeProvider(config) {
92
92
  // Store for cleanup
93
93
  globalPool = pool;
94
94
  // Create provider config
95
- const providerConfig = new sqlserver_dataprovider_1.SQLServerProviderConfigData(pool, 'system@sync.cli', // Default user for CLI
96
- config.mjCoreSchema || '__mj', 0);
95
+ const providerConfig = new sqlserver_dataprovider_1.SQLServerProviderConfigData(pool, config.mjCoreSchema || '__mj');
97
96
  // Use setupSQLServerClient to properly initialize
98
97
  globalProvider = await (0, sqlserver_dataprovider_1.setupSQLServerClient)(providerConfig);
99
98
  return globalProvider;
@@ -161,8 +160,8 @@ exports.getSystemUser = getSystemUser;
161
160
  * @example
162
161
  * ```typescript
163
162
  * const provider = getDataProvider();
164
- * if (provider?.createSqlLogger) {
165
- * const logger = await provider.createSqlLogger('/path/to/log.sql');
163
+ * if (provider?.CreateSqlLogger) {
164
+ * const logger = await provider.CreateSqlLogger('/path/to/log.sql');
166
165
  * }
167
166
  * ```
168
167
  */