@memberjunction/metadata-sync 2.47.0 → 2.48.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.
- package/README.md +217 -15
- package/dist/commands/pull/index.d.ts +90 -5
- package/dist/commands/pull/index.js +930 -118
- package/dist/commands/pull/index.js.map +1 -1
- package/dist/commands/push/index.js +18 -10
- package/dist/commands/push/index.js.map +1 -1
- package/dist/commands/status/index.js +18 -7
- package/dist/commands/status/index.js.map +1 -1
- package/dist/commands/watch/index.js +19 -6
- package/dist/commands/watch/index.js.map +1 -1
- package/dist/config.d.ts +69 -0
- package/dist/config.js +2 -13
- package/dist/config.js.map +1 -1
- package/dist/hooks/init.js +3 -0
- package/dist/hooks/init.js.map +1 -1
- package/dist/lib/config-manager.d.ts +56 -0
- package/dist/lib/config-manager.js +104 -0
- package/dist/lib/config-manager.js.map +1 -0
- package/dist/lib/provider-utils.js +45 -26
- package/dist/lib/provider-utils.js.map +1 -1
- package/dist/lib/singleton-manager.d.ts +34 -0
- package/dist/lib/singleton-manager.js +62 -0
- package/dist/lib/singleton-manager.js.map +1 -0
- package/oclif.manifest.json +50 -43
- package/package.json +6 -6
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/commands/status/index.ts"],"names":[],"mappings":";;;;;AAAA,sCAA6C;AAC7C,wDAA0B;AAC1B,gDAAwB;AACxB,0DAAiC;AACjC,8DAA8B;AAC9B,yCAA8D;AAC9D,uDAA+D;AAC/D,6DAAoG;AAEpG,MAAqB,MAAO,SAAQ,cAAO;IACzC,MAAM,CAAC,WAAW,GAAG,wCAAwC,CAAC;IAE9D,MAAM,CAAC,QAAQ,GAAG;QAChB,qCAAqC;QACrC,wDAAwD;KACzD,CAAC;IAEF,MAAM,CAAC,KAAK,GAAG;QACb,GAAG,EAAE,YAAK,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,2CAA2C,EAAE,CAAC;KAChF,CAAC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AC3D,eAAe,GAAG;wBAChB,GAAG,YAAY;wBACf,GAAG,YAAY;wBACf,QAAQ,EAAE;4BACR,GAAG,YAAY,CAAC,QAAQ;4BACxB,GAAG,CAAC,YAAY,CAAC,QAAQ,IAAI,EAAE,CAAC;yBACjC;qBACF,CAAC;gBACJ,CAAC;gBAED,0CAA0C;gBAC1C,MAAM,SAAS,GAAG,MAAM,IAAI,CAAC,oBAAoB,CAC/C,MAAM,EACN,eAAe,EACf,UAAU,CACX,CAAC;gBAEF,MAAM,CAAC,GAAG,IAAI,SAAS,CAAC,GAAG,CAAC;gBAC5B,MAAM,CAAC,QAAQ,IAAI,SAAS,CAAC,QAAQ,CAAC;gBACtC,MAAM,CAAC,OAAO,IAAI,SAAS,CAAC,OAAO,CAAC;gBACpC,MAAM,CAAC,SAAS,IAAI,SAAS,CAAC,SAAS,CAAC;YAC1C,CAAC;QACH,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;;AAhLH,yBAiLC","sourcesContent":["import { Command, Flags } from '@oclif/core';\nimport fs from 'fs-extra';\nimport path from 'path';\nimport fastGlob from 'fast-glob';\nimport ora from 'ora-classic';\nimport { loadMJConfig, loadEntityConfig } from '../../config';\nimport { SyncEngine, RecordData } from '../../lib/sync-engine';\nimport { initializeProvider, findEntityDirectories, getSystemUser } from '../../lib/provider-utils';\n\nexport default class Status extends Command {\n static description = 'Show status of local files vs 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 check status' }),\n };\n \n async run(): Promise<void> {\n const { flags } = await this.parse(Status);\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 // Initialize data provider\n const provider = await initializeProvider(mjConfig);\n \n // Initialize sync engine\n const syncEngine = new SyncEngine(getSystemUser());\n await syncEngine.initialize();\n spinner.succeed('Configuration loaded');\n \n // Find entity directories to process\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(`Found ${entityDirs.length} entity ${entityDirs.length === 1 ? 'directory' : 'directories'} to check`);\n \n // Process each entity directory\n let totalNew = 0;\n let totalModified = 0;\n let totalDeleted = 0;\n let totalUnchanged = 0;\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(`\\nChecking ${entityConfig.entity} in ${entityDir}`);\n \n const result = await this.checkEntityDirectory(\n entityDir,\n entityConfig,\n syncEngine\n );\n \n totalNew += result.new;\n totalModified += result.modified;\n totalDeleted += result.deleted;\n totalUnchanged += result.unchanged;\n \n // Show directory summary\n if (result.new > 0 || result.modified > 0 || result.deleted > 0) {\n this.log(` New: ${result.new}, Modified: ${result.modified}, Deleted: ${result.deleted}, Unchanged: ${result.unchanged}`);\n } else {\n this.log(` All ${result.unchanged} records are up to date`);\n }\n }\n \n // Overall summary\n this.log('\\n=== Status Summary ===');\n this.log(`New (local only): ${totalNew}`);\n this.log(`Modified locally: ${totalModified}`);\n this.log(`Deleted locally: ${totalDeleted}`);\n this.log(`Unchanged: ${totalUnchanged}`);\n \n } catch (error) {\n spinner.fail('Status check failed');\n this.error(error as Error);\n }\n }\n \n private async checkEntityDirectory(\n entityDir: string,\n entityConfig: any,\n syncEngine: SyncEngine\n ): Promise<{ new: number; modified: number; deleted: number; unchanged: number }> {\n const result = { new: 0, modified: 0, deleted: 0, unchanged: 0 };\n \n // Find JSON files in the current directory only (not subdirectories)\n const pattern = entityConfig.filePattern || '*.json';\n const jsonFiles = await fastGlob(pattern, {\n cwd: entityDir,\n ignore: ['.mj-sync.json', '.mj-folder.json'],\n dot: true // Include dotfiles (files starting with .)\n });\n \n for (const file of jsonFiles) {\n try {\n const filePath = path.join(entityDir, file);\n const recordData: RecordData = await fs.readJson(filePath);\n \n if (recordData.primaryKey) {\n // Check if record exists in database\n const entity = await syncEngine.loadEntity(entityConfig.entity, recordData.primaryKey);\n \n if (!entity) {\n result.deleted++;\n } else {\n // Check if modified\n const currentChecksum = syncEngine.calculateChecksum(recordData.fields);\n if (recordData.sync?.checksum !== currentChecksum) {\n result.modified++;\n } else {\n result.unchanged++;\n }\n }\n } else {\n // New record\n result.new++;\n }\n \n } catch (error) {\n this.warn(`Failed to check ${file}: ${error}`);\n }\n }\n \n // Recursively process subdirectories\n const entries = await fs.readdir(entityDir, { withFileTypes: true });\n for (const entry of entries) {\n if (entry.isDirectory() && !entry.name.startsWith('.')) {\n const subDir = path.join(entityDir, entry.name);\n \n // Load subdirectory config and merge with parent config\n let subEntityConfig = { ...entityConfig };\n const subDirConfig = await loadEntityConfig(subDir);\n \n if (subDirConfig) {\n // Check if this is a new entity type (has different entity name)\n if (subDirConfig.entity && subDirConfig.entity !== entityConfig.entity) {\n // This is a different entity type, skip it (will be processed separately)\n continue;\n }\n \n // Merge defaults: parent defaults + subdirectory overrides\n subEntityConfig = {\n ...entityConfig,\n ...subDirConfig,\n defaults: {\n ...entityConfig.defaults,\n ...(subDirConfig.defaults || {})\n }\n };\n }\n \n // Process subdirectory with merged config\n const subResult = await this.checkEntityDirectory(\n subDir,\n subEntityConfig,\n syncEngine\n );\n \n result.new += subResult.new;\n result.modified += subResult.modified;\n result.deleted += subResult.deleted;\n result.unchanged += subResult.unchanged;\n }\n }\n \n return result;\n }\n}"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/commands/status/index.ts"],"names":[],"mappings":";;;;;AAAA,sCAA6C;AAC7C,wDAA0B;AAC1B,gDAAwB;AACxB,0DAAiC;AACjC,8DAA8B;AAC9B,yCAA8D;AAE9D,6DAAoG;AACpG,mEAA6E;AAC7E,6DAA2D;AAE3D,MAAqB,MAAO,SAAQ,cA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UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;gBACvD,MAAM,MAAM,GAAG,cAAI,CAAC,IAAI,CAAC,SAAS,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC;gBAEhD,wDAAwD;gBACxD,IAAI,eAAe,GAAG,EAAE,GAAG,YAAY,EAAE,CAAC;gBAC1C,MAAM,YAAY,GAAG,MAAM,IAAA,yBAAgB,EAAC,MAAM,CAAC,CAAC;gBAEpD,IAAI,YAAY,EAAE,CAAC;oBACjB,iEAAiE;oBACjE,IAAI,YAAY,CAAC,MAAM,IAAI,YAAY,CAAC,MAAM,KAAK,YAAY,CAAC,MAAM,EAAE,CAAC;wBACvE,0EAA0E;wBAC1E,SAAS;oBACX,CAAC;oBAED,2DAA2D;oBAC3D,eAAe,GAAG;wBAChB,GAAG,YAAY;wBACf,GAAG,YAAY;wBACf,QAAQ,EAAE;4BACR,GAAG,YAAY,CAAC,QAAQ;4BACxB,GAAG,CAAC,YAAY,CAAC,QAAQ,IAAI,EAAE,CAAC;yBACjC;qBACF,CAAC;gBACJ,CAAC;gBAED,0CAA0C;gBAC1C,MAAM,SAAS,GAAG,MAAM,IAAI,CAAC,oBAAoB,CAC/C,MAAM,EACN,eAAe,EACf,UAAU,CACX,CAAC;gBAEF,MAAM,CAAC,GAAG,IAAI,SAAS,CAAC,GAAG,CAAC;gBAC5B,MAAM,CAAC,QAAQ,IAAI,SAAS,CAAC,QAAQ,CAAC;gBACtC,MAAM,CAAC,OAAO,IAAI,SAAS,CAAC,OAAO,CAAC;gBACpC,MAAM,CAAC,SAAS,IAAI,SAAS,CAAC,SAAS,CAAC;YAC1C,CAAC;QACH,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;;AA5LH,yBA6LC","sourcesContent":["import { Command, Flags } from '@oclif/core';\nimport fs from 'fs-extra';\nimport path from 'path';\nimport fastGlob from 'fast-glob';\nimport ora from 'ora-classic';\nimport { loadMJConfig, loadEntityConfig } from '../../config';\nimport { SyncEngine, RecordData } from '../../lib/sync-engine';\nimport { initializeProvider, findEntityDirectories, getSystemUser } from '../../lib/provider-utils';\nimport { getSyncEngine, resetSyncEngine } from '../../lib/singleton-manager';\nimport { cleanupProvider } from '../../lib/provider-utils';\n\nexport default class Status extends Command {\n static description = 'Show status of local files vs 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 check status' }),\n };\n \n async run(): Promise<void> {\n const { flags } = await this.parse(Status);\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 // 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 const 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 process\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(`Found ${entityDirs.length} entity ${entityDirs.length === 1 ? 'directory' : 'directories'} to check`);\n \n // Process each entity directory\n let totalNew = 0;\n let totalModified = 0;\n let totalDeleted = 0;\n let totalUnchanged = 0;\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(`\\nChecking ${entityConfig.entity} in ${entityDir}`);\n \n const result = await this.checkEntityDirectory(\n entityDir,\n entityConfig,\n syncEngine\n );\n \n totalNew += result.new;\n totalModified += result.modified;\n totalDeleted += result.deleted;\n totalUnchanged += result.unchanged;\n \n // Show directory summary\n if (result.new > 0 || result.modified > 0 || result.deleted > 0) {\n this.log(` New: ${result.new}, Modified: ${result.modified}, Deleted: ${result.deleted}, Unchanged: ${result.unchanged}`);\n } else {\n this.log(` All ${result.unchanged} records are up to date`);\n }\n }\n \n // Overall summary\n this.log('\\n=== Status Summary ===');\n this.log(`New (local only): ${totalNew}`);\n this.log(`Modified locally: ${totalModified}`);\n this.log(`Deleted locally: ${totalDeleted}`);\n this.log(`Unchanged: ${totalUnchanged}`);\n \n } catch (error) {\n spinner.fail('Status check failed');\n this.error(error as Error);\n } finally {\n // Reset sync engine singleton\n resetSyncEngine();\n // Clean up database connection\n await cleanupProvider();\n \n // Exit process to prevent background MJ tasks from throwing errors\n process.exit(0);\n }\n }\n \n private async checkEntityDirectory(\n entityDir: string,\n entityConfig: any,\n syncEngine: SyncEngine\n ): Promise<{ new: number; modified: number; deleted: number; unchanged: number }> {\n const result = { new: 0, modified: 0, deleted: 0, unchanged: 0 };\n \n // Find files matching the configured pattern\n const pattern = entityConfig.filePattern || '*.json';\n const jsonFiles = await fastGlob(pattern, {\n cwd: entityDir,\n ignore: ['.mj-sync.json', '.mj-folder.json', '**/*.backup'],\n dot: true // Include dotfiles (files starting with .)\n });\n \n for (const file of jsonFiles) {\n try {\n const filePath = path.join(entityDir, file);\n const recordData: RecordData = await fs.readJson(filePath);\n \n if (recordData.primaryKey) {\n // Check if record exists in database\n const entity = await syncEngine.loadEntity(entityConfig.entity, recordData.primaryKey);\n \n if (!entity) {\n result.deleted++;\n } else {\n // Check if modified\n const currentChecksum = syncEngine.calculateChecksum(recordData.fields);\n if (recordData.sync?.checksum !== currentChecksum) {\n result.modified++;\n } else {\n result.unchanged++;\n }\n }\n } else {\n // New record\n result.new++;\n }\n \n } catch (error) {\n this.warn(`Failed to check ${file}: ${error}`);\n }\n }\n \n // Recursively process subdirectories\n const entries = await fs.readdir(entityDir, { withFileTypes: true });\n for (const entry of entries) {\n if (entry.isDirectory() && !entry.name.startsWith('.')) {\n const subDir = path.join(entityDir, entry.name);\n \n // Load subdirectory config and merge with parent config\n let subEntityConfig = { ...entityConfig };\n const subDirConfig = await loadEntityConfig(subDir);\n \n if (subDirConfig) {\n // Check if this is a new entity type (has different entity name)\n if (subDirConfig.entity && subDirConfig.entity !== entityConfig.entity) {\n // This is a different entity type, skip it (will be processed separately)\n continue;\n }\n \n // Merge defaults: parent defaults + subdirectory overrides\n subEntityConfig = {\n ...entityConfig,\n ...subDirConfig,\n defaults: {\n ...entityConfig.defaults,\n ...(subDirConfig.defaults || {})\n }\n };\n }\n \n // Process subdirectory with merged config\n const subResult = await this.checkEntityDirectory(\n subDir,\n subEntityConfig,\n syncEngine\n );\n \n result.new += subResult.new;\n result.modified += subResult.modified;\n result.deleted += subResult.deleted;\n result.unchanged += subResult.unchanged;\n }\n }\n \n return result;\n }\n}"]}
|
|
@@ -9,8 +9,9 @@ const path_1 = __importDefault(require("path"));
|
|
|
9
9
|
const chokidar_1 = __importDefault(require("chokidar"));
|
|
10
10
|
const ora_classic_1 = __importDefault(require("ora-classic"));
|
|
11
11
|
const config_1 = require("../../config");
|
|
12
|
-
const sync_engine_1 = require("../../lib/sync-engine");
|
|
13
12
|
const provider_utils_1 = require("../../lib/provider-utils");
|
|
13
|
+
const singleton_manager_1 = require("../../lib/singleton-manager");
|
|
14
|
+
const provider_utils_2 = require("../../lib/provider-utils");
|
|
14
15
|
class Watch extends core_1.Command {
|
|
15
16
|
static description = 'Watch for file changes and automatically push to database';
|
|
16
17
|
static examples = [
|
|
@@ -34,12 +35,14 @@ class Watch extends core_1.Command {
|
|
|
34
35
|
this.error('No mj.config.cjs found in current directory or parent directories');
|
|
35
36
|
}
|
|
36
37
|
this.syncConfig = await (0, config_1.loadSyncConfig)(process.cwd());
|
|
38
|
+
// Stop spinner before provider initialization (which logs to console)
|
|
39
|
+
spinner.stop();
|
|
37
40
|
// Initialize data provider
|
|
38
41
|
const provider = await (0, provider_utils_1.initializeProvider)(mjConfig);
|
|
39
|
-
// Initialize sync engine
|
|
40
|
-
this.syncEngine =
|
|
41
|
-
|
|
42
|
-
spinner.succeed('Configuration loaded');
|
|
42
|
+
// Initialize sync engine using singleton pattern
|
|
43
|
+
this.syncEngine = await (0, singleton_manager_1.getSyncEngine)((0, provider_utils_1.getSystemUser)());
|
|
44
|
+
// Show success after all initialization is complete
|
|
45
|
+
spinner.succeed('Configuration and metadata loaded');
|
|
43
46
|
// Find entity directories to watch
|
|
44
47
|
const entityDirs = (0, provider_utils_1.findEntityDirectories)(process.cwd(), flags.dir);
|
|
45
48
|
if (entityDirs.length === 0) {
|
|
@@ -56,6 +59,7 @@ class Watch extends core_1.Command {
|
|
|
56
59
|
}
|
|
57
60
|
this.log(`Watching ${entityConfig.entity} in ${entityDir}`);
|
|
58
61
|
// Watch for JSON files and external files
|
|
62
|
+
// All JSON files will be watched, but only dot-prefixed ones will be processed
|
|
59
63
|
const patterns = [
|
|
60
64
|
path_1.default.join(entityDir, entityConfig.filePattern || '**/*.json'),
|
|
61
65
|
path_1.default.join(entityDir, '**/*.md'),
|
|
@@ -69,6 +73,7 @@ class Watch extends core_1.Command {
|
|
|
69
73
|
'**/.git/**',
|
|
70
74
|
'**/.mj-sync.json',
|
|
71
75
|
'**/.mj-folder.json',
|
|
76
|
+
'**/*.backup',
|
|
72
77
|
...(this.syncConfig?.watch?.ignorePatterns || [])
|
|
73
78
|
];
|
|
74
79
|
const watcher = chokidar_1.default.watch(patterns, {
|
|
@@ -86,14 +91,22 @@ class Watch extends core_1.Command {
|
|
|
86
91
|
// Keep process alive
|
|
87
92
|
process.stdin.resume();
|
|
88
93
|
// Cleanup on exit
|
|
89
|
-
process.on('SIGINT', () => {
|
|
94
|
+
process.on('SIGINT', async () => {
|
|
90
95
|
this.log('\nStopping watchers...');
|
|
91
96
|
watchers.forEach(w => w.close());
|
|
97
|
+
// Reset sync engine singleton
|
|
98
|
+
(0, singleton_manager_1.resetSyncEngine)();
|
|
99
|
+
// Clean up database connection
|
|
100
|
+
await (0, provider_utils_2.cleanupProvider)();
|
|
92
101
|
process.exit(0);
|
|
93
102
|
});
|
|
94
103
|
}
|
|
95
104
|
catch (error) {
|
|
96
105
|
spinner.fail('Watch failed');
|
|
106
|
+
// Reset sync engine singleton
|
|
107
|
+
(0, singleton_manager_1.resetSyncEngine)();
|
|
108
|
+
// Clean up database connection
|
|
109
|
+
await (0, provider_utils_2.cleanupProvider)();
|
|
97
110
|
this.error(error);
|
|
98
111
|
}
|
|
99
112
|
}
|
|
@@ -1 +1 @@
|
|
|
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;AAC9E,uDAA+D;AAC/D,6DAAoG;AAGpG,MAAqB,KAAM,SAAQ,cAAO;IACxC,MAAM,CAAC,WAAW,GAAG,2DAA2D,CAAC;IAEjF,MAAM,CAAC,QAAQ,GAAG;QAChB,qCAAqC;QACrC,wDAAwD;KACzD,CAAC;IAEF,MAAM,CAAC,KAAK,GAAG;QACb,GAAG,EAAE,YAAK,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,oCAAoC,EAAE,CAAC;KACzE,CAAC;IAEM,UAAU,CAAc;IACxB,UAAU,CAAM;IAChB,cAAc,GAAgC,IAAI,GAAG,EAAE,CAAC;IAEhE,KAAK,CAAC,GAAG;QACP,MAAM,EAAE,KAAK,EAAE,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QAC1C,MAAM,OAAO,GAAG,IAAA,qBAAG,GAAE,CAAC;QAEtB,IAAI,CAAC;YACH,sBAAsB;YACtB,OAAO,CAAC,KAAK,CAAC,uBAAuB,CAAC,CAAC;YACvC,MAAM,QAAQ,GAAG,IAAA,qBAAY,GAAE,CAAC;YAChC,IAAI,CAAC,QAAQ,EAAE,CAAC;gBACd,IAAI,CAAC,KAAK,CAAC,mEAAmE,CAAC,CAAC;YAClF,CAAC;YAED,IAAI,CAAC,UAAU,GAAG,M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AC,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;;AAtPH,wBAuPC","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';\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 // Initialize data provider\n const provider = await initializeProvider(mjConfig);\n \n // Initialize sync engine\n this.syncEngine = new SyncEngine(getSystemUser());\n await this.syncEngine.initialize();\n spinner.succeed('Configuration 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 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 ...(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', () => {\n this.log('\\nStopping watchers...');\n watchers.forEach(w => w.close());\n process.exit(0);\n });\n \n } catch (error) {\n spinner.fail('Watch failed');\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 errors = entity.LatestResult?.Errors?.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,GAAG;QAChB,qCAAqC;QACrC,wDAAwD;KACzD,CAAC;IA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MAAM,EAAE,CAAC,EAAE,CAAC,CAAC;gBAE5D,IAAI,CAAC,GAAG,CAAC,6BAA6B,YAAY,8BAA8B,CAAC,CAAC;YACpF,CAAC;QACH,CAAC;IACH,CAAC;;AApQH,wBAqQC","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 // 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 errors = entity.LatestResult?.Errors?.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}"]}
|
package/dist/config.d.ts
CHANGED
|
@@ -74,6 +74,27 @@ export interface RelatedEntityConfig {
|
|
|
74
74
|
foreignKey: string;
|
|
75
75
|
/** Optional SQL filter to apply when pulling related records */
|
|
76
76
|
filter?: string;
|
|
77
|
+
/** Fields to externalize to separate files for this related entity */
|
|
78
|
+
externalizeFields?: string[] | {
|
|
79
|
+
[fieldName: string]: {
|
|
80
|
+
/** File extension to use (e.g., ".md", ".txt", ".html") */
|
|
81
|
+
extension?: string;
|
|
82
|
+
};
|
|
83
|
+
} | Array<{
|
|
84
|
+
/** Field name to externalize */
|
|
85
|
+
field: string;
|
|
86
|
+
/** Pattern for the output file. Supports placeholders from the entity */
|
|
87
|
+
pattern: string;
|
|
88
|
+
}>;
|
|
89
|
+
/** Fields to exclude from the pulled data for this related entity */
|
|
90
|
+
excludeFields?: string[];
|
|
91
|
+
/** Foreign key fields to convert to @lookup references for this related entity */
|
|
92
|
+
lookupFields?: {
|
|
93
|
+
[fieldName: string]: {
|
|
94
|
+
entity: string;
|
|
95
|
+
field: string;
|
|
96
|
+
};
|
|
97
|
+
};
|
|
77
98
|
/** Nested related entities for deep object graphs */
|
|
78
99
|
relatedEntities?: Record<string, RelatedEntityConfig>;
|
|
79
100
|
}
|
|
@@ -93,10 +114,58 @@ export interface EntityConfig {
|
|
|
93
114
|
defaults?: Record<string, any>;
|
|
94
115
|
/** Pull command specific configuration */
|
|
95
116
|
pull?: {
|
|
117
|
+
/** Glob pattern for finding existing files to update (defaults to filePattern) */
|
|
118
|
+
filePattern?: string;
|
|
119
|
+
/** Whether to create new files for records not found locally */
|
|
120
|
+
createNewFileIfNotFound?: boolean;
|
|
121
|
+
/** Filename for new records when createNewFileIfNotFound is true */
|
|
122
|
+
newFileName?: string;
|
|
123
|
+
/** Whether to append multiple new records to a single file */
|
|
124
|
+
appendRecordsToExistingFile?: boolean;
|
|
125
|
+
/** Whether to update existing records found in local files */
|
|
126
|
+
updateExistingRecords?: boolean;
|
|
127
|
+
/** Fields to preserve during updates (never overwrite these) */
|
|
128
|
+
preserveFields?: string[];
|
|
129
|
+
/** Strategy for merging updates: "overwrite" | "merge" | "skip" */
|
|
130
|
+
mergeStrategy?: 'overwrite' | 'merge' | 'skip';
|
|
131
|
+
/** Create backup files before updating existing files */
|
|
132
|
+
backupBeforeUpdate?: boolean;
|
|
133
|
+
/** Directory name for backup files (defaults to ".backups") */
|
|
134
|
+
backupDirectory?: string;
|
|
96
135
|
/** SQL filter to apply when pulling records from database */
|
|
97
136
|
filter?: string;
|
|
98
137
|
/** Configuration for pulling related entities */
|
|
99
138
|
relatedEntities?: Record<string, RelatedEntityConfig>;
|
|
139
|
+
/** Fields to externalize to separate files with optional configuration */
|
|
140
|
+
externalizeFields?: string[] | {
|
|
141
|
+
[fieldName: string]: {
|
|
142
|
+
/** File extension to use (e.g., ".md", ".txt", ".html") */
|
|
143
|
+
extension?: string;
|
|
144
|
+
};
|
|
145
|
+
} | Array<{
|
|
146
|
+
/** Field name to externalize */
|
|
147
|
+
field: string;
|
|
148
|
+
/** Pattern for the output file. Supports placeholders:
|
|
149
|
+
* - {Name}: Entity's name field value
|
|
150
|
+
* - {ID}: Entity's ID
|
|
151
|
+
* - {FieldName}: The field being externalized
|
|
152
|
+
* - Any other {FieldName} from the entity
|
|
153
|
+
* Example: "@file:templates/{Name}.template.md"
|
|
154
|
+
*/
|
|
155
|
+
pattern: string;
|
|
156
|
+
}>;
|
|
157
|
+
/** Fields to exclude from the pulled data (e.g., ["TemplateID"]) */
|
|
158
|
+
excludeFields?: string[];
|
|
159
|
+
/** Foreign key fields to convert to @lookup references */
|
|
160
|
+
lookupFields?: {
|
|
161
|
+
/** Field name in this entity (e.g., "CategoryID") */
|
|
162
|
+
[fieldName: string]: {
|
|
163
|
+
/** Target entity name (e.g., "AI Prompt Categories") */
|
|
164
|
+
entity: string;
|
|
165
|
+
/** Field in target entity to use for lookup (e.g., "Name") */
|
|
166
|
+
field: string;
|
|
167
|
+
};
|
|
168
|
+
};
|
|
100
169
|
};
|
|
101
170
|
}
|
|
102
171
|
/**
|
package/dist/config.js
CHANGED
|
@@ -15,9 +15,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
exports.loadFolderConfig = exports.loadEntityConfig = exports.loadSyncConfig = exports.loadMJConfig = void 0;
|
|
18
|
-
const cosmiconfig_1 = require("cosmiconfig");
|
|
19
18
|
const path_1 = __importDefault(require("path"));
|
|
20
19
|
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
20
|
+
const config_manager_1 = require("./lib/config-manager");
|
|
21
21
|
/**
|
|
22
22
|
* Load MemberJunction configuration from the filesystem
|
|
23
23
|
*
|
|
@@ -35,18 +35,7 @@ const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
|
35
35
|
* ```
|
|
36
36
|
*/
|
|
37
37
|
function loadMJConfig() {
|
|
38
|
-
|
|
39
|
-
const explorer = (0, cosmiconfig_1.cosmiconfigSync)('mj');
|
|
40
|
-
const result = explorer.search(process.cwd());
|
|
41
|
-
if (!result || !result.config) {
|
|
42
|
-
throw new Error('No mj.config.cjs found');
|
|
43
|
-
}
|
|
44
|
-
return result.config;
|
|
45
|
-
}
|
|
46
|
-
catch (error) {
|
|
47
|
-
console.error('Error loading MJ config:', error);
|
|
48
|
-
return null;
|
|
49
|
-
}
|
|
38
|
+
return config_manager_1.configManager.loadMJConfig();
|
|
50
39
|
}
|
|
51
40
|
exports.loadMJConfig = loadMJConfig;
|
|
52
41
|
/**
|
package/dist/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;GAUG;;;;;;AAEH,6CAA8C;AAC9C,gDAAwB;AACxB,wDAA0B;AA2G1B;;;;;;;;;;;;;;;GAeG;AACH,SAAgB,YAAY;IAC1B,IAAI,CAAC;QACH,MAAM,QAAQ,GAAG,IAAA,6BAAe,EAAC,IAAI,CAAC,CAAC;QACvC,MAAM,MAAM,GAAG,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC;QAE9C,IAAI,CAAC,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;YAC9B,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAC;QAC5C,CAAC;QAED,OAAO,MAAM,CAAC,MAAM,CAAC;IACvB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,KAAK,CAAC,0BAA0B,EAAE,KAAK,CAAC,CAAC;QACjD,OAAO,IAAI,CAAC;IACd,CAAC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AI,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';\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 /** 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 /** 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 */\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 /** 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 /** SQL filter to apply when pulling records from database */\n filter?: string;\n /** Configuration for pulling related entities */\n relatedEntities?: Record<string, RelatedEntityConfig>;\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 try {\n const explorer = cosmiconfigSync('mj');\n const result = explorer.search(process.cwd());\n \n if (!result || !result.config) {\n throw new Error('No mj.config.cjs found');\n }\n \n return result.config;\n } catch (error) {\n console.error('Error loading MJ config:', error);\n return null;\n }\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;AAgLrD;;;;;;;;;;;;;;;GAeG;AACH,SAAgB,YAAY;IAC1B,OAAO,8BAAa,CAAC,YAAY,EAAE,CAAC;AACtC,CAAC;AAFD,oCAEC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACI,KAAK,UAAU,cAAc,CAAC,GAAW;IAC9C,MAAM,UAAU,GAAG,cAAI,CAAC,IAAI,CAAC,GAAG,EAAE,eAAe,CAAC,CAAC;IAEnD,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,4BAA4B,EAAE,KAAK,CAAC,CAAC;YACnD,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAbD,wCAaC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACI,KAAK,UAAU,gBAAgB,CAAC,GAAW;IAChD,MAAM,UAAU,GAAG,cAAI,CAAC,IAAI,CAAC,GAAG,EAAE,eAAe,CAAC,CAAC;IAEnD,IAAI,MAAM,kBAAE,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC;QACpC,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,kBAAE,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;YAC7C,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC;gBAClB,OAAO,MAAM,CAAC;YAChB,CAAC;QACH,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,KAAK,CAAC,8BAA8B,EAAE,KAAK,CAAC,CAAC;QACvD,CAAC;IACH,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAfD,4CAeC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACI,KAAK,UAAU,gBAAgB,CAAC,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 /** 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 /** 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 */\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 /** 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/hooks/init.js
CHANGED
|
@@ -27,7 +27,10 @@ const core_entities_server_1 = require("@memberjunction/core-entities-server");
|
|
|
27
27
|
const dotenv = __importStar(require("dotenv"));
|
|
28
28
|
const path = __importStar(require("path"));
|
|
29
29
|
const provider_utils_1 = require("../lib/provider-utils");
|
|
30
|
+
const config_manager_1 = require("../lib/config-manager");
|
|
30
31
|
const hook = async function () {
|
|
32
|
+
// Capture the original working directory FIRST before any changes
|
|
33
|
+
config_manager_1.configManager.getOriginalCwd();
|
|
31
34
|
// Load .env from the repository root first
|
|
32
35
|
dotenv.config({ path: path.join(__dirname, '../../../../.env') });
|
|
33
36
|
// Then load local .env from package directory to override
|
package/dist/hooks/init.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"init.js","sourceRoot":"","sources":["../../src/hooks/init.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AACA,+EAAwF;AACxF,+CAAiC;AACjC,2CAA6B;AAC7B,0DAAwD;
|
|
1
|
+
{"version":3,"file":"init.js","sourceRoot":"","sources":["../../src/hooks/init.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AACA,+EAAwF;AACxF,+CAAiC;AACjC,2CAA6B;AAC7B,0DAAwD;AACxD,0DAAsD;AAEtD,MAAM,IAAI,GAAiB,KAAK;IAC9B,kEAAkE;IAClE,8BAAa,CAAC,cAAc,EAAE,CAAC;IAE/B,2CAA2C;IAC3C,MAAM,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,kBAAkB,CAAC,EAAE,CAAC,CAAC;IAElE,0DAA0D;IAC1D,MAAM,CAAC,MAAM,CAAC;QACZ,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,YAAY,CAAC;QACxC,QAAQ,EAAE,IAAI,CAAE,yDAAyD;KAC1E,CAAC,CAAC;IAEH,uCAAuC;IACvC,IAAA,uDAAgC,GAAE,CAAC;IAEnC,4BAA4B;IAC5B,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,GAAG,EAAE;QACtB,IAAA,gCAAe,GAAE,CAAC,KAAK,CAAC,GAAG,EAAE;YAC3B,+BAA+B;QACjC,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,OAAO,CAAC,EAAE,CAAC,QAAQ,EAAE,KAAK,IAAI,EAAE;QAC9B,MAAM,IAAA,gCAAe,GAAE,CAAC;QACxB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC,CAAC,CAAC;IAEH,OAAO,CAAC,EAAE,CAAC,SAAS,EAAE,KAAK,IAAI,EAAE;QAC/B,MAAM,IAAA,gCAAe,GAAE,CAAC;QACxB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AAEF,kBAAe,IAAI,CAAC","sourcesContent":["import { Hook } from '@oclif/core';\nimport { LoadCoreEntitiesServerSubClasses } from '@memberjunction/core-entities-server';\nimport * as dotenv from 'dotenv';\nimport * as path from 'path';\nimport { cleanupProvider } from '../lib/provider-utils';\nimport { configManager } from '../lib/config-manager';\n\nconst hook: Hook<'init'> = async function () {\n // Capture the original working directory FIRST before any changes\n configManager.getOriginalCwd();\n \n // Load .env from the repository root first\n dotenv.config({ path: path.join(__dirname, '../../../../.env') });\n \n // Then load local .env from package directory to override\n dotenv.config({ \n path: path.join(__dirname, '../../.env'),\n override: true // This ensures local values override already loaded ones\n });\n \n // Load core entities server subclasses\n LoadCoreEntitiesServerSubClasses();\n \n // Register cleanup handlers\n process.on('exit', () => {\n cleanupProvider().catch(() => {\n // Ignore errors during cleanup\n });\n });\n \n process.on('SIGINT', async () => {\n await cleanupProvider();\n process.exit(0);\n });\n \n process.on('SIGTERM', async () => {\n await cleanupProvider();\n process.exit(0);\n });\n};\n\nexport default hook;"]}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Shared configuration manager for MetadataSync commands
|
|
3
|
+
* @module lib/config-manager
|
|
4
|
+
*
|
|
5
|
+
* This module provides a centralized configuration management system that handles
|
|
6
|
+
* loading MJ config from the original working directory, regardless of any
|
|
7
|
+
* directory changes made during command execution.
|
|
8
|
+
*/
|
|
9
|
+
import { MJConfig } from '../config';
|
|
10
|
+
/**
|
|
11
|
+
* Configuration manager singleton for handling MJ configuration
|
|
12
|
+
*
|
|
13
|
+
* Stores the original working directory and MJ configuration to ensure
|
|
14
|
+
* consistent access across all commands, even when the current working
|
|
15
|
+
* directory changes during execution.
|
|
16
|
+
*/
|
|
17
|
+
export declare class ConfigManager {
|
|
18
|
+
private static instance;
|
|
19
|
+
private originalCwd;
|
|
20
|
+
private mjConfig;
|
|
21
|
+
private configLoaded;
|
|
22
|
+
private constructor();
|
|
23
|
+
/**
|
|
24
|
+
* Get the singleton instance of ConfigManager
|
|
25
|
+
*/
|
|
26
|
+
static getInstance(): ConfigManager;
|
|
27
|
+
/**
|
|
28
|
+
* Get the original working directory from when the process started
|
|
29
|
+
*
|
|
30
|
+
* @returns The original working directory path
|
|
31
|
+
*/
|
|
32
|
+
getOriginalCwd(): string;
|
|
33
|
+
/**
|
|
34
|
+
* Set the original working directory (for testing or special cases)
|
|
35
|
+
*
|
|
36
|
+
* @param cwd - The working directory to use as original
|
|
37
|
+
*/
|
|
38
|
+
setOriginalCwd(cwd: string): void;
|
|
39
|
+
/**
|
|
40
|
+
* Load MemberJunction configuration
|
|
41
|
+
*
|
|
42
|
+
* Searches for mj.config.cjs starting from the original working directory
|
|
43
|
+
* and walking up the directory tree. Caches the result for subsequent calls.
|
|
44
|
+
*
|
|
45
|
+
* @param forceReload - Force reload the configuration even if cached
|
|
46
|
+
* @returns MJConfig object if found, null if not found or invalid
|
|
47
|
+
*/
|
|
48
|
+
loadMJConfig(forceReload?: boolean): MJConfig | null;
|
|
49
|
+
/**
|
|
50
|
+
* Get the cached MJ configuration
|
|
51
|
+
*
|
|
52
|
+
* @returns The cached MJConfig or null if not loaded
|
|
53
|
+
*/
|
|
54
|
+
getMJConfig(): MJConfig | null;
|
|
55
|
+
}
|
|
56
|
+
export declare const configManager: ConfigManager;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @fileoverview Shared configuration manager for MetadataSync commands
|
|
4
|
+
* @module lib/config-manager
|
|
5
|
+
*
|
|
6
|
+
* This module provides a centralized configuration management system that handles
|
|
7
|
+
* loading MJ config from the original working directory, regardless of any
|
|
8
|
+
* directory changes made during command execution.
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.configManager = exports.ConfigManager = void 0;
|
|
12
|
+
const cosmiconfig_1 = require("cosmiconfig");
|
|
13
|
+
/**
|
|
14
|
+
* Configuration manager singleton for handling MJ configuration
|
|
15
|
+
*
|
|
16
|
+
* Stores the original working directory and MJ configuration to ensure
|
|
17
|
+
* consistent access across all commands, even when the current working
|
|
18
|
+
* directory changes during execution.
|
|
19
|
+
*/
|
|
20
|
+
class ConfigManager {
|
|
21
|
+
static instance;
|
|
22
|
+
originalCwd = null;
|
|
23
|
+
mjConfig = null;
|
|
24
|
+
configLoaded = false;
|
|
25
|
+
constructor() {
|
|
26
|
+
// Original cwd will be set on first access
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Get the singleton instance of ConfigManager
|
|
30
|
+
*/
|
|
31
|
+
static getInstance() {
|
|
32
|
+
if (!ConfigManager.instance) {
|
|
33
|
+
ConfigManager.instance = new ConfigManager();
|
|
34
|
+
}
|
|
35
|
+
return ConfigManager.instance;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Get the original working directory from when the process started
|
|
39
|
+
*
|
|
40
|
+
* @returns The original working directory path
|
|
41
|
+
*/
|
|
42
|
+
getOriginalCwd() {
|
|
43
|
+
if (!this.originalCwd) {
|
|
44
|
+
// Capture on first access
|
|
45
|
+
this.originalCwd = process.cwd();
|
|
46
|
+
}
|
|
47
|
+
return this.originalCwd;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Set the original working directory (for testing or special cases)
|
|
51
|
+
*
|
|
52
|
+
* @param cwd - The working directory to use as original
|
|
53
|
+
*/
|
|
54
|
+
setOriginalCwd(cwd) {
|
|
55
|
+
this.originalCwd = cwd;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Load MemberJunction configuration
|
|
59
|
+
*
|
|
60
|
+
* Searches for mj.config.cjs starting from the original working directory
|
|
61
|
+
* and walking up the directory tree. Caches the result for subsequent calls.
|
|
62
|
+
*
|
|
63
|
+
* @param forceReload - Force reload the configuration even if cached
|
|
64
|
+
* @returns MJConfig object if found, null if not found or invalid
|
|
65
|
+
*/
|
|
66
|
+
loadMJConfig(forceReload = false) {
|
|
67
|
+
if (this.configLoaded && !forceReload) {
|
|
68
|
+
return this.mjConfig;
|
|
69
|
+
}
|
|
70
|
+
try {
|
|
71
|
+
const explorer = (0, cosmiconfig_1.cosmiconfigSync)('mj');
|
|
72
|
+
// Always search from the original working directory
|
|
73
|
+
const searchPath = this.getOriginalCwd();
|
|
74
|
+
const result = explorer.search(searchPath);
|
|
75
|
+
if (!result || !result.config) {
|
|
76
|
+
throw new Error('No mj.config.cjs found');
|
|
77
|
+
}
|
|
78
|
+
this.mjConfig = result.config;
|
|
79
|
+
this.configLoaded = true;
|
|
80
|
+
return this.mjConfig;
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
console.error('Error loading MJ config:', error);
|
|
84
|
+
this.mjConfig = null;
|
|
85
|
+
this.configLoaded = true;
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Get the cached MJ configuration
|
|
91
|
+
*
|
|
92
|
+
* @returns The cached MJConfig or null if not loaded
|
|
93
|
+
*/
|
|
94
|
+
getMJConfig() {
|
|
95
|
+
if (!this.configLoaded) {
|
|
96
|
+
return this.loadMJConfig();
|
|
97
|
+
}
|
|
98
|
+
return this.mjConfig;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
exports.ConfigManager = ConfigManager;
|
|
102
|
+
// Export singleton instance for convenience
|
|
103
|
+
exports.configManager = ConfigManager.getInstance();
|
|
104
|
+
//# sourceMappingURL=config-manager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config-manager.js","sourceRoot":"","sources":["../../src/lib/config-manager.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;AAEH,6CA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C,QAAQ,GAAG,MAAM,CAAC,MAAM,CAAC;YAC9B,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC;YACzB,OAAO,IAAI,CAAC,QAAQ,CAAC;QACvB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,KAAK,CAAC,0BAA0B,EAAE,KAAK,CAAC,CAAC;YACjD,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;YACrB,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC;YACzB,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC;IAED;;;;OAIG;IACH,WAAW;QACT,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE,CAAC;YACvB,OAAO,IAAI,CAAC,YAAY,EAAE,CAAC;QAC7B,CAAC;QACD,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;CACF;AAxFD,sCAwFC;AAED,4CAA4C;AAC/B,QAAA,aAAa,GAAG,aAAa,CAAC,WAAW,EAAE,CAAC","sourcesContent":["/**\n * @fileoverview Shared configuration manager for MetadataSync commands\n * @module lib/config-manager\n * \n * This module provides a centralized configuration management system that handles\n * loading MJ config from the original working directory, regardless of any\n * directory changes made during command execution.\n */\n\nimport { cosmiconfigSync } from 'cosmiconfig';\nimport { MJConfig } from '../config';\n\n/**\n * Configuration manager singleton for handling MJ configuration\n * \n * Stores the original working directory and MJ configuration to ensure\n * consistent access across all commands, even when the current working\n * directory changes during execution.\n */\nexport class ConfigManager {\n private static instance: ConfigManager;\n private originalCwd: string | null = null;\n private mjConfig: MJConfig | null = null;\n private configLoaded = false;\n\n private constructor() {\n // Original cwd will be set on first access\n }\n\n /**\n * Get the singleton instance of ConfigManager\n */\n static getInstance(): ConfigManager {\n if (!ConfigManager.instance) {\n ConfigManager.instance = new ConfigManager();\n }\n return ConfigManager.instance;\n }\n\n /**\n * Get the original working directory from when the process started\n * \n * @returns The original working directory path\n */\n getOriginalCwd(): string {\n if (!this.originalCwd) {\n // Capture on first access\n this.originalCwd = process.cwd();\n }\n return this.originalCwd;\n }\n \n /**\n * Set the original working directory (for testing or special cases)\n * \n * @param cwd - The working directory to use as original\n */\n setOriginalCwd(cwd: string): void {\n this.originalCwd = cwd;\n }\n\n /**\n * Load MemberJunction configuration\n * \n * Searches for mj.config.cjs starting from the original working directory\n * and walking up the directory tree. Caches the result for subsequent calls.\n * \n * @param forceReload - Force reload the configuration even if cached\n * @returns MJConfig object if found, null if not found or invalid\n */\n loadMJConfig(forceReload = false): MJConfig | null {\n if (this.configLoaded && !forceReload) {\n return this.mjConfig;\n }\n\n try {\n const explorer = cosmiconfigSync('mj');\n // Always search from the original working directory\n const searchPath = this.getOriginalCwd();\n const result = explorer.search(searchPath);\n \n if (!result || !result.config) {\n throw new Error('No mj.config.cjs found');\n }\n \n this.mjConfig = result.config;\n this.configLoaded = true;\n return this.mjConfig;\n } catch (error) {\n console.error('Error loading MJ config:', error);\n this.mjConfig = null;\n this.configLoaded = true;\n return null;\n }\n }\n\n /**\n * Get the cached MJ configuration\n * \n * @returns The cached MJConfig or null if not loaded\n */\n getMJConfig(): MJConfig | null {\n if (!this.configLoaded) {\n return this.loadMJConfig();\n }\n return this.mjConfig;\n }\n}\n\n// Export singleton instance for convenience\nexport const configManager = ConfigManager.getInstance();"]}
|