@memberjunction/codegen-lib 6.3.0-edge.0 → 6.3.0-edge.1
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 +37 -1
- package/dist/Config/config.d.ts +36 -0
- package/dist/Config/config.d.ts.map +1 -1
- package/dist/Config/config.js +10 -0
- package/dist/Config/config.js.map +1 -1
- package/dist/Database/codeGenDatabaseProvider.d.ts +7 -1
- package/dist/Database/codeGenDatabaseProvider.d.ts.map +1 -1
- package/dist/Database/codeGenDatabaseProvider.js +23 -1
- package/dist/Database/codeGenDatabaseProvider.js.map +1 -1
- package/dist/Database/manage-metadata.d.ts +54 -15
- package/dist/Database/manage-metadata.d.ts.map +1 -1
- package/dist/Database/manage-metadata.js +306 -144
- package/dist/Database/manage-metadata.js.map +1 -1
- package/dist/Database/organic-key-sync.d.ts +70 -0
- package/dist/Database/organic-key-sync.d.ts.map +1 -0
- package/dist/Database/organic-key-sync.js +83 -0
- package/dist/Database/organic-key-sync.js.map +1 -0
- package/dist/Database/providers/postgresql/PostgreSQLCodeGenProvider.d.ts +7 -0
- package/dist/Database/providers/postgresql/PostgreSQLCodeGenProvider.d.ts.map +1 -1
- package/dist/Database/providers/postgresql/PostgreSQLCodeGenProvider.js +74 -16
- package/dist/Database/providers/postgresql/PostgreSQLCodeGenProvider.js.map +1 -1
- package/dist/Database/providers/postgresql/__tests__/MetadataSupportObjectsDDL.test.js +14 -0
- package/dist/Database/providers/postgresql/__tests__/MetadataSupportObjectsDDL.test.js.map +1 -1
- package/dist/Database/providers/postgresql/__tests__/NullableKeyReselect.test.d.ts +2 -0
- package/dist/Database/providers/postgresql/__tests__/NullableKeyReselect.test.d.ts.map +1 -0
- package/dist/Database/providers/postgresql/__tests__/NullableKeyReselect.test.js +36 -0
- package/dist/Database/providers/postgresql/__tests__/NullableKeyReselect.test.js.map +1 -0
- package/dist/Database/providers/postgresql/__tests__/PostgreSQLHierarchyFunctions.test.js +5 -0
- package/dist/Database/providers/postgresql/__tests__/PostgreSQLHierarchyFunctions.test.js.map +1 -1
- package/dist/Database/providers/postgresql/__tests__/SoftPrimaryKeyIndex.test.js +6 -0
- package/dist/Database/providers/postgresql/__tests__/SoftPrimaryKeyIndex.test.js.map +1 -1
- package/dist/Database/providers/postgresql/metadataSupportObjects.js +43 -11
- package/dist/Database/providers/postgresql/metadataSupportObjects.js.map +1 -1
- package/dist/Database/providers/postgresql/viewFallback.d.ts +25 -0
- package/dist/Database/providers/postgresql/viewFallback.d.ts.map +1 -1
- package/dist/Database/providers/postgresql/viewFallback.js +101 -48
- package/dist/Database/providers/postgresql/viewFallback.js.map +1 -1
- package/dist/Database/providers/sqlserver/SQLServerCodeGenProvider.d.ts +12 -2
- package/dist/Database/providers/sqlserver/SQLServerCodeGenProvider.d.ts.map +1 -1
- package/dist/Database/providers/sqlserver/SQLServerCodeGenProvider.js +96 -32
- package/dist/Database/providers/sqlserver/SQLServerCodeGenProvider.js.map +1 -1
- package/dist/Database/providers/sqlserver/__tests__/BaseViewEmissionGoldenMaster.test.js +4 -3
- package/dist/Database/providers/sqlserver/__tests__/BaseViewEmissionGoldenMaster.test.js.map +1 -1
- package/dist/Database/providers/sqlserver/__tests__/SQLServerCodeGenProvider.test.js +41 -0
- package/dist/Database/providers/sqlserver/__tests__/SQLServerCodeGenProvider.test.js.map +1 -1
- package/dist/Database/providers/sqlserver/__tests__/SoftPrimaryKeyIndex.test.js +6 -0
- package/dist/Database/providers/sqlserver/__tests__/SoftPrimaryKeyIndex.test.js.map +1 -1
- package/dist/Database/sql_codegen.d.ts +74 -2
- package/dist/Database/sql_codegen.d.ts.map +1 -1
- package/dist/Database/sql_codegen.js +536 -125
- package/dist/Database/sql_codegen.js.map +1 -1
- package/dist/Manifest/GenerateClassRegistrationsManifest.d.ts +49 -1
- package/dist/Manifest/GenerateClassRegistrationsManifest.d.ts.map +1 -1
- package/dist/Manifest/GenerateClassRegistrationsManifest.js +392 -60
- package/dist/Manifest/GenerateClassRegistrationsManifest.js.map +1 -1
- package/dist/Misc/advanced_generation.d.ts +15 -3
- package/dist/Misc/advanced_generation.d.ts.map +1 -1
- package/dist/Misc/advanced_generation.js +50 -3
- package/dist/Misc/advanced_generation.js.map +1 -1
- package/dist/Misc/createNewUser.js +4 -4
- package/dist/Misc/createNewUser.js.map +1 -1
- package/dist/Misc/entity_subclasses_codegen.d.ts +11 -4
- package/dist/Misc/entity_subclasses_codegen.d.ts.map +1 -1
- package/dist/Misc/entity_subclasses_codegen.js +46 -14
- package/dist/Misc/entity_subclasses_codegen.js.map +1 -1
- package/dist/Misc/smart-field-decision.d.ts +126 -0
- package/dist/Misc/smart-field-decision.d.ts.map +1 -0
- package/dist/Misc/smart-field-decision.js +222 -0
- package/dist/Misc/smart-field-decision.js.map +1 -0
- package/dist/Misc/system_integrity.d.ts +9 -0
- package/dist/Misc/system_integrity.d.ts.map +1 -1
- package/dist/Misc/system_integrity.js +20 -1
- package/dist/Misc/system_integrity.js.map +1 -1
- package/dist/Misc/typed_query_codegen.d.ts +22 -8
- package/dist/Misc/typed_query_codegen.d.ts.map +1 -1
- package/dist/Misc/typed_query_codegen.js +23 -2
- package/dist/Misc/typed_query_codegen.js.map +1 -1
- package/dist/Misc/validator-text.d.ts +14 -5
- package/dist/Misc/validator-text.d.ts.map +1 -1
- package/dist/Misc/validator-text.js +42 -13
- package/dist/Misc/validator-text.js.map +1 -1
- package/dist/plugins/index.d.ts +10 -0
- package/dist/plugins/index.d.ts.map +1 -1
- package/dist/plugins/index.js +22 -2
- package/dist/plugins/index.js.map +1 -1
- package/dist/runCodeGen.d.ts +6 -0
- package/dist/runCodeGen.d.ts.map +1 -1
- package/dist/runCodeGen.js +12 -3
- package/dist/runCodeGen.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +30 -29
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
import ts from 'typescript';
|
|
17
17
|
import * as fs from 'fs';
|
|
18
18
|
import * as path from 'path';
|
|
19
|
+
import * as crypto from 'crypto';
|
|
19
20
|
import { glob } from 'glob';
|
|
20
21
|
import { ordinalCompare } from '@memberjunction/global';
|
|
21
22
|
/**
|
|
@@ -31,7 +32,8 @@ function readPackageJson(dir) {
|
|
|
31
32
|
name: pkg.name || path.basename(dir),
|
|
32
33
|
dir,
|
|
33
34
|
dependencies: pkg.dependencies || {},
|
|
34
|
-
devDependencies: pkg.devDependencies || {}
|
|
35
|
+
devDependencies: pkg.devDependencies || {},
|
|
36
|
+
peerDependencies: pkg.peerDependencies || {}
|
|
35
37
|
};
|
|
36
38
|
}
|
|
37
39
|
catch {
|
|
@@ -71,12 +73,61 @@ function resolvePackageDir(depName, fromDir) {
|
|
|
71
73
|
function isPackageExcluded(packageName, excludePackages) {
|
|
72
74
|
return excludePackages.some(prefix => packageName.startsWith(prefix));
|
|
73
75
|
}
|
|
76
|
+
function isWorkspaceSourceDir(dir) {
|
|
77
|
+
const normalized = dir.replace(/\\/g, '/');
|
|
78
|
+
return !normalized.includes('/node_modules/') && !normalized.includes('/.pnpm/');
|
|
79
|
+
}
|
|
80
|
+
function resolvePackageDirMemo(depName, searchDir, root, cache) {
|
|
81
|
+
if (searchDir === root)
|
|
82
|
+
return null;
|
|
83
|
+
const key = `${searchDir}::${depName}`;
|
|
84
|
+
if (cache.has(key)) {
|
|
85
|
+
return cache.get(key);
|
|
86
|
+
}
|
|
87
|
+
const candidate = path.join(searchDir, 'node_modules', depName);
|
|
88
|
+
let resolved;
|
|
89
|
+
if (fs.existsSync(path.join(candidate, 'package.json'))) {
|
|
90
|
+
try {
|
|
91
|
+
resolved = fs.realpathSync(candidate);
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
resolved = candidate;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
const parent = path.dirname(searchDir);
|
|
99
|
+
resolved = parent === searchDir ? null : resolvePackageDirMemo(depName, parent, root, cache);
|
|
100
|
+
}
|
|
101
|
+
cache.set(key, resolved);
|
|
102
|
+
return resolved;
|
|
103
|
+
}
|
|
74
104
|
function walkDependencyTree(appDir, log, excludePackages = [], includeRootDevDependencies = true) {
|
|
75
105
|
// Map of package name -> resolved directory
|
|
76
106
|
const visited = new Map();
|
|
77
107
|
const queue = [];
|
|
108
|
+
const queuedPairs = new Set();
|
|
109
|
+
const resolveCache = new Map();
|
|
110
|
+
const pkgJsonCache = new Map();
|
|
111
|
+
const readPkgCached = (dir) => {
|
|
112
|
+
if (pkgJsonCache.has(dir))
|
|
113
|
+
return pkgJsonCache.get(dir);
|
|
114
|
+
const parsed = readPackageJson(dir);
|
|
115
|
+
pkgJsonCache.set(dir, parsed);
|
|
116
|
+
return parsed;
|
|
117
|
+
};
|
|
118
|
+
const resolveCached = (depName, fromDir) => {
|
|
119
|
+
const root = path.parse(fromDir).root;
|
|
120
|
+
return resolvePackageDirMemo(depName, fromDir, root, resolveCache);
|
|
121
|
+
};
|
|
122
|
+
const enqueueEdge = (depName, fromDir) => {
|
|
123
|
+
const pairKey = `${fromDir}::${depName}`;
|
|
124
|
+
if (queuedPairs.has(pairKey))
|
|
125
|
+
return;
|
|
126
|
+
queuedPairs.add(pairKey);
|
|
127
|
+
queue.push({ depName, fromDir });
|
|
128
|
+
};
|
|
78
129
|
// Start with the app's own package.json
|
|
79
|
-
const appPkg =
|
|
130
|
+
const appPkg = readPkgCached(appDir);
|
|
80
131
|
if (!appPkg) {
|
|
81
132
|
log(`No package.json found in ${appDir}`);
|
|
82
133
|
return visited;
|
|
@@ -93,33 +144,59 @@ function walkDependencyTree(appDir, log, excludePackages = [], includeRootDevDep
|
|
|
93
144
|
? { ...appPkg.dependencies, ...appPkg.devDependencies }
|
|
94
145
|
: { ...appPkg.dependencies };
|
|
95
146
|
for (const depName of Object.keys(allDeps)) {
|
|
96
|
-
|
|
147
|
+
enqueueEdge(depName, appDir);
|
|
97
148
|
}
|
|
98
149
|
while (queue.length > 0) {
|
|
99
150
|
const { depName, fromDir } = queue.shift();
|
|
100
|
-
//
|
|
101
|
-
if (visited.has(depName))
|
|
151
|
+
// If already visited, prefer live workspace package directories over node_modules/.pnpm tarball copies
|
|
152
|
+
if (visited.has(depName)) {
|
|
153
|
+
const existingDir = visited.get(depName);
|
|
154
|
+
if (isWorkspaceSourceDir(existingDir))
|
|
155
|
+
continue;
|
|
156
|
+
const candidateDir = resolveCached(depName, fromDir);
|
|
157
|
+
if (!candidateDir || candidateDir === existingDir || !isWorkspaceSourceDir(candidateDir)) {
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
log(`Preferring workspace source for ${depName}: ${candidateDir} (over ${existingDir})`);
|
|
161
|
+
visited.set(depName, candidateDir);
|
|
162
|
+
const upgradedPkg = readPkgCached(candidateDir);
|
|
163
|
+
if (upgradedPkg) {
|
|
164
|
+
for (const transitiveDep of Object.keys(upgradedPkg.dependencies)) {
|
|
165
|
+
const existingTransitive = visited.get(transitiveDep);
|
|
166
|
+
if (!existingTransitive || !isWorkspaceSourceDir(existingTransitive)) {
|
|
167
|
+
enqueueEdge(transitiveDep, candidateDir);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
102
171
|
continue;
|
|
172
|
+
}
|
|
103
173
|
// Skip packages matching exclusion prefixes
|
|
104
174
|
if (isPackageExcluded(depName, excludePackages))
|
|
105
175
|
continue;
|
|
106
176
|
// Resolve to actual directory on disk
|
|
107
|
-
const depDir =
|
|
177
|
+
const depDir = resolveCached(depName, fromDir);
|
|
108
178
|
if (!depDir)
|
|
109
179
|
continue; // External package not found locally, skip
|
|
110
180
|
visited.set(depName, depDir);
|
|
111
181
|
// Read this package's dependencies and add to queue (only dependencies, not devDeps for transitive)
|
|
112
|
-
const depPkg =
|
|
182
|
+
const depPkg = readPkgCached(depDir);
|
|
113
183
|
if (depPkg) {
|
|
114
184
|
for (const transitiveDep of Object.keys(depPkg.dependencies)) {
|
|
115
|
-
|
|
116
|
-
|
|
185
|
+
const existingTransitive = visited.get(transitiveDep);
|
|
186
|
+
if (!existingTransitive || !isWorkspaceSourceDir(existingTransitive)) {
|
|
187
|
+
enqueueEdge(transitiveDep, depDir);
|
|
117
188
|
}
|
|
118
189
|
}
|
|
119
190
|
}
|
|
120
191
|
}
|
|
121
192
|
return visited;
|
|
122
193
|
}
|
|
194
|
+
/**
|
|
195
|
+
* Test helper for walking the dependency tree.
|
|
196
|
+
*/
|
|
197
|
+
export function WalkDependencyTreeForTest(appDir, log, excludePackages = [], includeRootDevDependencies = true) {
|
|
198
|
+
return walkDependencyTree(appDir, log || (() => { }), excludePackages, includeRootDevDependencies);
|
|
199
|
+
}
|
|
123
200
|
/**
|
|
124
201
|
* Finds scannable files in a package directory.
|
|
125
202
|
* Prefers TypeScript source files from src/, but falls back to compiled JS
|
|
@@ -378,6 +455,243 @@ function extractRegisterClassFromDecoratorArray(arrayLiteral, className, filePat
|
|
|
378
455
|
return results;
|
|
379
456
|
}
|
|
380
457
|
// ============================================================================
|
|
458
|
+
// Manifest AST Cache
|
|
459
|
+
// ============================================================================
|
|
460
|
+
export const MAX_AST_CACHE_ENTRIES = 20000;
|
|
461
|
+
const astCacheMap = new Map();
|
|
462
|
+
let astCacheHits = 0;
|
|
463
|
+
let astCacheMisses = 0;
|
|
464
|
+
let astCacheDirty = false;
|
|
465
|
+
let astCacheLoadedFromPath = null;
|
|
466
|
+
export function ClearManifestASTCache() {
|
|
467
|
+
astCacheMap.clear();
|
|
468
|
+
astCacheHits = 0;
|
|
469
|
+
astCacheMisses = 0;
|
|
470
|
+
astCacheDirty = false;
|
|
471
|
+
astCacheLoadedFromPath = null;
|
|
472
|
+
}
|
|
473
|
+
export function GetManifestASTCacheStats() {
|
|
474
|
+
return {
|
|
475
|
+
Hits: astCacheHits,
|
|
476
|
+
Misses: astCacheMisses,
|
|
477
|
+
Size: astCacheMap.size
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
export function ResetManifestASTCacheStats() {
|
|
481
|
+
astCacheHits = 0;
|
|
482
|
+
astCacheMisses = 0;
|
|
483
|
+
}
|
|
484
|
+
export function LoadManifestASTCacheFromDisk(cacheFilePath) {
|
|
485
|
+
if (astCacheLoadedFromPath === cacheFilePath && astCacheMap.size > 0) {
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
try {
|
|
489
|
+
if (!fs.existsSync(cacheFilePath))
|
|
490
|
+
return;
|
|
491
|
+
const content = fs.readFileSync(cacheFilePath, 'utf-8');
|
|
492
|
+
const payload = JSON.parse(content);
|
|
493
|
+
if (payload && payload.version === 2 && typeof payload.entries === 'object' && payload.entries !== null) {
|
|
494
|
+
for (const [key, record] of Object.entries(payload.entries)) {
|
|
495
|
+
if (record &&
|
|
496
|
+
typeof record.contentHash === 'string' &&
|
|
497
|
+
typeof record.size === 'number' &&
|
|
498
|
+
typeof record.isCompiledJS === 'boolean' &&
|
|
499
|
+
Array.isArray(record.classes)) {
|
|
500
|
+
const validClasses = [];
|
|
501
|
+
for (const c of record.classes) {
|
|
502
|
+
if (c && typeof c.className === 'string') {
|
|
503
|
+
validClasses.push({
|
|
504
|
+
className: c.className,
|
|
505
|
+
...(typeof c.baseClassName === 'string' ? { baseClassName: c.baseClassName } : {}),
|
|
506
|
+
...(typeof c.key === 'string' ? { key: c.key } : {})
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
astCacheMap.set(key, {
|
|
511
|
+
contentHash: record.contentHash,
|
|
512
|
+
size: record.size,
|
|
513
|
+
isCompiledJS: record.isCompiledJS,
|
|
514
|
+
lastSeenMs: typeof record.lastSeenMs === 'number' ? record.lastSeenMs : Date.now(),
|
|
515
|
+
classes: validClasses,
|
|
516
|
+
...(typeof record.mtimeMs === 'number' ? { mtimeMs: record.mtimeMs } : {})
|
|
517
|
+
});
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
astCacheLoadedFromPath = cacheFilePath;
|
|
521
|
+
astCacheDirty = false;
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
catch {
|
|
525
|
+
// Ignored: read errors should never fail manifest generation
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
export function SaveManifestASTCacheToDisk(cacheFilePath, options) {
|
|
529
|
+
try {
|
|
530
|
+
const shouldPrune = options?.PruneMissingFiles ?? options?.pruneMissingFiles ?? true;
|
|
531
|
+
// Concurrent merge: if cache file already exists on disk, read version 2 entries and merge
|
|
532
|
+
if (fs.existsSync(cacheFilePath)) {
|
|
533
|
+
try {
|
|
534
|
+
const diskContent = fs.readFileSync(cacheFilePath, 'utf-8');
|
|
535
|
+
const diskPayload = JSON.parse(diskContent);
|
|
536
|
+
if (diskPayload && diskPayload.version === 2 && typeof diskPayload.entries === 'object' && diskPayload.entries !== null) {
|
|
537
|
+
for (const [key, diskRecord] of Object.entries(diskPayload.entries)) {
|
|
538
|
+
if (diskRecord && typeof diskRecord.contentHash === 'string') {
|
|
539
|
+
const memRecord = astCacheMap.get(key);
|
|
540
|
+
if (!memRecord) {
|
|
541
|
+
astCacheMap.set(key, diskRecord);
|
|
542
|
+
}
|
|
543
|
+
else if ((diskRecord.lastSeenMs ?? 0) > (memRecord.lastSeenMs ?? 0)) {
|
|
544
|
+
astCacheMap.set(key, diskRecord);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
catch {
|
|
551
|
+
// Ignore read/parse error during disk merge
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
// Prune stale entries on save: drop any entry whose filePath no longer exists on disk
|
|
555
|
+
if (shouldPrune) {
|
|
556
|
+
for (const [key] of Array.from(astCacheMap.entries())) {
|
|
557
|
+
const sepIdx = key.indexOf('::');
|
|
558
|
+
const entryFilePath = sepIdx !== -1 ? key.substring(sepIdx + 2) : key;
|
|
559
|
+
try {
|
|
560
|
+
if (path.isAbsolute(entryFilePath) && !fs.existsSync(entryFilePath)) {
|
|
561
|
+
astCacheMap.delete(key);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
catch {
|
|
565
|
+
astCacheMap.delete(key);
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
// Cap total entries at maxEntries (defaults to MAX_AST_CACHE_ENTRIES = 20000), keeping newest lastSeenMs
|
|
570
|
+
const maxEntries = options?.MaxEntries ?? options?.maxEntries ?? MAX_AST_CACHE_ENTRIES;
|
|
571
|
+
if (astCacheMap.size > maxEntries) {
|
|
572
|
+
const sortedEntries = Array.from(astCacheMap.entries()).sort((a, b) => (b[1].lastSeenMs ?? 0) - (a[1].lastSeenMs ?? 0));
|
|
573
|
+
astCacheMap.clear();
|
|
574
|
+
for (let i = 0; i < maxEntries; i++) {
|
|
575
|
+
astCacheMap.set(sortedEntries[i][0], sortedEntries[i][1]);
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
const entries = {};
|
|
579
|
+
for (const [key, value] of astCacheMap.entries()) {
|
|
580
|
+
entries[key] = value;
|
|
581
|
+
}
|
|
582
|
+
const payload = {
|
|
583
|
+
version: 2,
|
|
584
|
+
entries
|
|
585
|
+
};
|
|
586
|
+
const dir = path.dirname(cacheFilePath);
|
|
587
|
+
if (!fs.existsSync(dir)) {
|
|
588
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
589
|
+
}
|
|
590
|
+
const tmpPath = `${cacheFilePath}.${process.pid}.${Date.now()}.${Math.random().toString(36).slice(2)}.tmp`;
|
|
591
|
+
try {
|
|
592
|
+
fs.writeFileSync(tmpPath, JSON.stringify(payload), 'utf-8');
|
|
593
|
+
fs.renameSync(tmpPath, cacheFilePath);
|
|
594
|
+
}
|
|
595
|
+
catch (writeErr) {
|
|
596
|
+
try {
|
|
597
|
+
if (fs.existsSync(tmpPath)) {
|
|
598
|
+
fs.unlinkSync(tmpPath);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
catch {
|
|
602
|
+
// Ignore unlink failure
|
|
603
|
+
}
|
|
604
|
+
throw writeErr;
|
|
605
|
+
}
|
|
606
|
+
astCacheLoadedFromPath = cacheFilePath;
|
|
607
|
+
astCacheDirty = false;
|
|
608
|
+
}
|
|
609
|
+
catch {
|
|
610
|
+
// Ignored: write errors should never fail manifest generation
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
export function ResolveDefaultAstCachePath(startDir) {
|
|
614
|
+
const baseDir = findCacheBaseDir(startDir);
|
|
615
|
+
try {
|
|
616
|
+
if (fs.existsSync(path.join(baseDir, 'turbo.json')) ||
|
|
617
|
+
fs.existsSync(path.join(baseDir, '.turbo'))) {
|
|
618
|
+
return path.join(baseDir, '.turbo', 'mj-manifest-ast-cache-v2.json');
|
|
619
|
+
}
|
|
620
|
+
if (fs.existsSync(path.join(baseDir, 'node_modules'))) {
|
|
621
|
+
return path.join(baseDir, 'node_modules', '.cache', 'memberjunction', 'mj-manifest-ast-cache-v2.json');
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
catch {
|
|
625
|
+
// Ignore access errors
|
|
626
|
+
}
|
|
627
|
+
return null;
|
|
628
|
+
}
|
|
629
|
+
/** @deprecated Use {@link ResolveDefaultAstCachePath}. */
|
|
630
|
+
export const resolveDefaultAstCachePath = ResolveDefaultAstCachePath;
|
|
631
|
+
function findCacheBaseDir(startDir) {
|
|
632
|
+
let current = startDir;
|
|
633
|
+
const root = path.parse(current).root;
|
|
634
|
+
while (current !== root) {
|
|
635
|
+
try {
|
|
636
|
+
if (fs.existsSync(path.join(current, 'pnpm-workspace.yaml')) ||
|
|
637
|
+
fs.existsSync(path.join(current, 'turbo.json')) ||
|
|
638
|
+
fs.existsSync(path.join(current, '.git'))) {
|
|
639
|
+
return current;
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
catch {
|
|
643
|
+
// Ignore access errors
|
|
644
|
+
}
|
|
645
|
+
const parent = path.dirname(current);
|
|
646
|
+
if (parent === current)
|
|
647
|
+
break;
|
|
648
|
+
current = parent;
|
|
649
|
+
}
|
|
650
|
+
return startDir;
|
|
651
|
+
}
|
|
652
|
+
function extractRegisterClassForFile(filePath, depName, isCompiledJS) {
|
|
653
|
+
let sourceText;
|
|
654
|
+
try {
|
|
655
|
+
sourceText = fs.readFileSync(filePath, 'utf-8');
|
|
656
|
+
}
|
|
657
|
+
catch {
|
|
658
|
+
return [];
|
|
659
|
+
}
|
|
660
|
+
const contentHash = crypto.createHash('sha256').update(sourceText).digest('hex');
|
|
661
|
+
const cacheKey = `${isCompiledJS ? 'js' : 'ts'}::${filePath}`;
|
|
662
|
+
const cached = astCacheMap.get(cacheKey);
|
|
663
|
+
if (cached &&
|
|
664
|
+
cached.contentHash === contentHash &&
|
|
665
|
+
cached.isCompiledJS === isCompiledJS) {
|
|
666
|
+
astCacheHits++;
|
|
667
|
+
cached.lastSeenMs = Date.now();
|
|
668
|
+
return cached.classes.map(c => ({
|
|
669
|
+
className: c.className,
|
|
670
|
+
filePath,
|
|
671
|
+
packageName: depName,
|
|
672
|
+
...(c.baseClassName !== undefined ? { baseClassName: c.baseClassName } : {}),
|
|
673
|
+
...(c.key !== undefined ? { key: c.key } : {})
|
|
674
|
+
}));
|
|
675
|
+
}
|
|
676
|
+
astCacheMisses++;
|
|
677
|
+
const parsed = isCompiledJS
|
|
678
|
+
? extractRegisterClassFromCompiledJS(filePath, sourceText, depName)
|
|
679
|
+
: extractRegisterClassDecorators(filePath, sourceText, depName);
|
|
680
|
+
astCacheMap.set(cacheKey, {
|
|
681
|
+
contentHash,
|
|
682
|
+
size: Buffer.byteLength(sourceText, 'utf-8'),
|
|
683
|
+
isCompiledJS,
|
|
684
|
+
lastSeenMs: Date.now(),
|
|
685
|
+
classes: parsed.map(c => ({
|
|
686
|
+
className: c.className,
|
|
687
|
+
...(c.baseClassName !== undefined ? { baseClassName: c.baseClassName } : {}),
|
|
688
|
+
...(c.key !== undefined ? { key: c.key } : {})
|
|
689
|
+
}))
|
|
690
|
+
});
|
|
691
|
+
astCacheDirty = true;
|
|
692
|
+
return parsed;
|
|
693
|
+
}
|
|
694
|
+
// ============================================================================
|
|
381
695
|
// Export Verification
|
|
382
696
|
// ============================================================================
|
|
383
697
|
/**
|
|
@@ -600,10 +914,14 @@ function topologicallySortPackages(packageMap, depTree, log) {
|
|
|
600
914
|
const pkgJson = pkgDir ? readPackageJson(pkgDir) : null;
|
|
601
915
|
if (!pkgJson)
|
|
602
916
|
continue;
|
|
603
|
-
|
|
917
|
+
const combinedDeps = [...Object.keys(pkgJson.dependencies), ...Object.keys(pkgJson.peerDependencies)];
|
|
918
|
+
for (const depName of combinedDeps) {
|
|
604
919
|
if (packageSet.has(depName) && depName !== pkg) {
|
|
605
|
-
depsOf.get(pkg)
|
|
606
|
-
|
|
920
|
+
const pkgDeps = depsOf.get(pkg);
|
|
921
|
+
if (!pkgDeps.has(depName)) {
|
|
922
|
+
pkgDeps.add(depName);
|
|
923
|
+
dependentsOf.get(depName).push(pkg);
|
|
924
|
+
}
|
|
607
925
|
}
|
|
608
926
|
}
|
|
609
927
|
}
|
|
@@ -636,6 +954,12 @@ function topologicallySortPackages(packageMap, depTree, log) {
|
|
|
636
954
|
}
|
|
637
955
|
return result;
|
|
638
956
|
}
|
|
957
|
+
/**
|
|
958
|
+
* Test helper for topologically sorting packages.
|
|
959
|
+
*/
|
|
960
|
+
export function TopologicallySortPackagesForTest(packageMap, depTree, log) {
|
|
961
|
+
return topologicallySortPackages(packageMap, depTree, log || (() => { }));
|
|
962
|
+
}
|
|
639
963
|
// NOTE: deliberately takes no dep-tree-size argument. The walked count is environment-dependent
|
|
640
964
|
// and must not reach the emitted file — see buildFileHeader for what that cost us. It is still
|
|
641
965
|
// reported on the RESULT object (and hence the console log), which is where it belongs.
|
|
@@ -1009,16 +1333,17 @@ export function resolveSubpathExports(packageDir) {
|
|
|
1009
1333
|
return ResolveSubpathExports(packageDir);
|
|
1010
1334
|
}
|
|
1011
1335
|
/**
|
|
1012
|
-
* Count
|
|
1013
|
-
* carrying a `types` field.
|
|
1336
|
+
* Count declared subpath exports whose `.d.ts` target is missing on disk: non-`.` entries
|
|
1337
|
+
* carrying a `types` field where the resolved `.d.ts` file does not exist.
|
|
1014
1338
|
*
|
|
1015
1339
|
* This is deliberately narrower than {@link hasSubpathExports}, which answers "is there any key
|
|
1016
1340
|
* besides `.`" and therefore counts entries that were never code subpaths at all. A built Angular
|
|
1017
1341
|
* package publishes `"./package.json": { "default": "./package.json" }`, which has no `types` and
|
|
1018
|
-
* which `resolveSubpathExportsDetailed()` skips by design.
|
|
1019
|
-
*
|
|
1342
|
+
* which `resolveSubpathExportsDetailed()` skips by design. Likewise, a built subpath whose
|
|
1343
|
+
* `.d.ts` exists on disk but declares no classes (e.g., `./schemas` exporting Zod schemas)
|
|
1344
|
+
* is genuinely built and must not be flagged as unbuilt/missing.
|
|
1020
1345
|
*/
|
|
1021
|
-
function
|
|
1346
|
+
function countMissingTypedSubpathExports(packageDir) {
|
|
1022
1347
|
const pkgPath = path.join(packageDir, 'package.json');
|
|
1023
1348
|
if (!fs.existsSync(pkgPath))
|
|
1024
1349
|
return 0;
|
|
@@ -1037,8 +1362,9 @@ function countTypedSubpathExports(packageDir) {
|
|
|
1037
1362
|
if (subpath === '.')
|
|
1038
1363
|
continue;
|
|
1039
1364
|
const typesField = typeof entry === 'object' && entry !== null ? entry.types : undefined;
|
|
1040
|
-
if (typesField)
|
|
1365
|
+
if (typesField && !fs.existsSync(path.resolve(packageDir, typesField))) {
|
|
1041
1366
|
count++;
|
|
1367
|
+
}
|
|
1042
1368
|
}
|
|
1043
1369
|
return count;
|
|
1044
1370
|
}
|
|
@@ -1061,15 +1387,15 @@ function countTypedSubpathExports(packageDir) {
|
|
|
1061
1387
|
* So refuse. A missing build is recoverable in one command; a silently de-optimised bundle
|
|
1062
1388
|
* shipped to production is not.
|
|
1063
1389
|
*
|
|
1064
|
-
* @throws when `packageDir` declares typed subpath exports
|
|
1390
|
+
* @throws when `packageDir` declares typed subpath exports whose `.d.ts` target does not exist on disk.
|
|
1065
1391
|
*/
|
|
1066
1392
|
export function ResolveLazySubpathExports(packageName, packageDir) {
|
|
1067
1393
|
const subpaths = resolveSubpathExportsDetailed(packageDir);
|
|
1068
1394
|
if (subpaths.size === 0) {
|
|
1069
|
-
const
|
|
1070
|
-
if (
|
|
1071
|
-
throw new Error(`Cannot resolve the subpath exports of '${packageName}'. Its package.json declares ${
|
|
1072
|
-
`subpath export(s) with a "types" target
|
|
1395
|
+
const missing = countMissingTypedSubpathExports(packageDir);
|
|
1396
|
+
if (missing > 0) {
|
|
1397
|
+
throw new Error(`Cannot resolve the subpath exports of '${packageName}'. Its package.json declares ${missing} ` +
|
|
1398
|
+
`subpath export(s) with a "types" target that do not exist under ${packageDir} — ` +
|
|
1073
1399
|
`the package has not been built.\n\n` +
|
|
1074
1400
|
`Build the workspace before generating the manifest. Emitting a manifest from here would ` +
|
|
1075
1401
|
`silently collapse this package's per-subpath lazy chunks into one eager import, removing ` +
|
|
@@ -1197,10 +1523,7 @@ async function scanPackagesForDecoratorsAsync(packages, excludePatterns, scanDis
|
|
|
1197
1523
|
const { files, isCompiledJS } = await findScannableFiles(depDir, excludePatterns, scanDist);
|
|
1198
1524
|
for (const filePath of files) {
|
|
1199
1525
|
try {
|
|
1200
|
-
const
|
|
1201
|
-
const found = isCompiledJS
|
|
1202
|
-
? extractRegisterClassFromCompiledJS(filePath, sourceText, depName)
|
|
1203
|
-
: extractRegisterClassDecorators(filePath, sourceText, depName);
|
|
1526
|
+
const found = extractRegisterClassForFile(filePath, depName, isCompiledJS);
|
|
1204
1527
|
classes.push(...found);
|
|
1205
1528
|
}
|
|
1206
1529
|
catch (err) {
|
|
@@ -1230,10 +1553,7 @@ function scanPackagesForDecorators(packages, excludePatterns, scanDist, errors)
|
|
|
1230
1553
|
}
|
|
1231
1554
|
for (const filePath of files) {
|
|
1232
1555
|
try {
|
|
1233
|
-
const
|
|
1234
|
-
const found = isCompiledJS
|
|
1235
|
-
? extractRegisterClassFromCompiledJS(filePath, sourceText, depName)
|
|
1236
|
-
: extractRegisterClassDecorators(filePath, sourceText, depName);
|
|
1556
|
+
const found = extractRegisterClassForFile(filePath, depName, isCompiledJS);
|
|
1237
1557
|
classes.push(...found);
|
|
1238
1558
|
}
|
|
1239
1559
|
catch (err) {
|
|
@@ -1297,40 +1617,33 @@ function groupClassesIntoChunks(lazyClasses, lazyPackages, log) {
|
|
|
1297
1617
|
const subpaths = packagesWithLazyClasses.has(depName)
|
|
1298
1618
|
? ResolveLazySubpathExports(depName, depDir)
|
|
1299
1619
|
: resolveSubpathExportsDetailed(depDir);
|
|
1300
|
-
|
|
1301
|
-
packageSubpaths.set(depName, subpaths);
|
|
1302
|
-
}
|
|
1620
|
+
packageSubpaths.set(depName, subpaths);
|
|
1303
1621
|
}
|
|
1304
1622
|
// Map: "packageName::subpath" -> LazyChunk
|
|
1305
1623
|
const chunks = new Map();
|
|
1306
1624
|
const keysSeen = new Map(); // key -> chunkKey (collision detection)
|
|
1625
|
+
const keysInChunk = new Map(); // chunkKey -> Set<compoundKey>
|
|
1307
1626
|
for (const cls of lazyClasses) {
|
|
1308
1627
|
if (!cls.key)
|
|
1309
1628
|
continue;
|
|
1310
1629
|
const subpaths = packageSubpaths.get(cls.packageName);
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
// Package has subpath exports — find which one contains this class
|
|
1316
|
-
// Use file path matching to disambiguate classes with the same name
|
|
1317
|
-
const packageDir = lazyPackages.get(cls.packageName);
|
|
1318
|
-
const foundSubpath = findClassSubpathByFile(cls, subpaths, packageDir);
|
|
1319
|
-
if (!foundSubpath) {
|
|
1320
|
-
log(` Warning: ${cls.key} (${cls.className}) not found in any subpath of ${cls.packageName}, skipping`);
|
|
1321
|
-
continue;
|
|
1322
|
-
}
|
|
1323
|
-
subpath = foundSubpath;
|
|
1324
|
-
const subpathClean = foundSubpath.replace(/^\.\//, '');
|
|
1325
|
-
importPath = `${cls.packageName}/${subpathClean}`;
|
|
1326
|
-
chunkKey = `${cls.packageName}::${foundSubpath}`;
|
|
1630
|
+
if (!subpaths || subpaths.size === 0) {
|
|
1631
|
+
// Non-class subpath exports (e.g. Zod `./schemas` or `./package.json`) belong to
|
|
1632
|
+
// eager root packages, not lazy chunk splitting — never collapse into a `'.'` chunk.
|
|
1633
|
+
continue;
|
|
1327
1634
|
}
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1635
|
+
// Package has class-bearing subpath exports — find which one contains this class
|
|
1636
|
+
// Use file path matching to disambiguate classes with the same name
|
|
1637
|
+
const packageDir = lazyPackages.get(cls.packageName);
|
|
1638
|
+
const foundSubpath = findClassSubpathByFile(cls, subpaths, packageDir);
|
|
1639
|
+
if (!foundSubpath) {
|
|
1640
|
+
log(` Warning: ${cls.key} (${cls.className}) not found in any subpath of ${cls.packageName}, skipping`);
|
|
1641
|
+
continue;
|
|
1333
1642
|
}
|
|
1643
|
+
const subpath = foundSubpath;
|
|
1644
|
+
const subpathClean = foundSubpath.replace(/^\.\//, '');
|
|
1645
|
+
const importPath = `${cls.packageName}/${subpathClean}`;
|
|
1646
|
+
const chunkKey = `${cls.packageName}::${foundSubpath}`;
|
|
1334
1647
|
// Build compound key for collision detection: 'BaseClassName::Key'
|
|
1335
1648
|
const compoundKey = `${cls.baseClassName || 'Unknown'}::${cls.key}`;
|
|
1336
1649
|
// Collision detection: same compound key in different chunks
|
|
@@ -1348,7 +1661,15 @@ function groupClassesIntoChunks(lazyClasses, lazyPackages, log) {
|
|
|
1348
1661
|
chunkVarName: buildChunkVarName(cls.packageName, subpath),
|
|
1349
1662
|
entries: []
|
|
1350
1663
|
});
|
|
1664
|
+
keysInChunk.set(chunkKey, new Set());
|
|
1665
|
+
}
|
|
1666
|
+
const chunkKeySet = keysInChunk.get(chunkKey);
|
|
1667
|
+
if (chunkKeySet.has(compoundKey)) {
|
|
1668
|
+
// Skip duplicate priority overrides within the same chunk so emitted object literals
|
|
1669
|
+
// never repeat property keys (TS1117).
|
|
1670
|
+
continue;
|
|
1351
1671
|
}
|
|
1672
|
+
chunkKeySet.add(compoundKey);
|
|
1352
1673
|
chunks.get(chunkKey).entries.push({
|
|
1353
1674
|
key: cls.key,
|
|
1354
1675
|
baseClassName: cls.baseClassName || 'Unknown'
|
|
@@ -1562,7 +1883,7 @@ function writeIfChanged(filePath, content) {
|
|
|
1562
1883
|
* ```
|
|
1563
1884
|
*/
|
|
1564
1885
|
export async function GenerateClassRegistrationsManifest(options) {
|
|
1565
|
-
const { outputPath, appDir = process.cwd(), verbose = true, filterBaseClasses, excludePatterns = [], excludePackages = [], syncDependencies = true, scanDist = false, lazyConfigPath } = options;
|
|
1886
|
+
const { outputPath, appDir = process.cwd(), verbose = true, filterBaseClasses, excludePatterns = [], excludePackages = [], syncDependencies = true, scanDist = false, lazyConfigPath, useDiskCache = true, astCachePath: explicitAstCachePath } = options;
|
|
1566
1887
|
const errors = [];
|
|
1567
1888
|
const allClasses = [];
|
|
1568
1889
|
// verbose log: only emitted with verbose=true (e.g. not during prestart)
|
|
@@ -1571,6 +1892,14 @@ export async function GenerateClassRegistrationsManifest(options) {
|
|
|
1571
1892
|
console.log(`[class-manifest] ${msg}`);
|
|
1572
1893
|
};
|
|
1573
1894
|
const absoluteAppDir = path.resolve(appDir);
|
|
1895
|
+
astCacheHits = 0;
|
|
1896
|
+
astCacheMisses = 0;
|
|
1897
|
+
const cacheFilePath = useDiskCache
|
|
1898
|
+
? (explicitAstCachePath ?? ResolveDefaultAstCachePath(absoluteAppDir))
|
|
1899
|
+
: null;
|
|
1900
|
+
if (cacheFilePath) {
|
|
1901
|
+
LoadManifestASTCacheFromDisk(cacheFilePath);
|
|
1902
|
+
}
|
|
1574
1903
|
// Read the app's package.json
|
|
1575
1904
|
const appPkg = readPackageJson(absoluteAppDir);
|
|
1576
1905
|
if (!appPkg) {
|
|
@@ -1594,10 +1923,7 @@ export async function GenerateClassRegistrationsManifest(options) {
|
|
|
1594
1923
|
let foundInPackage = false;
|
|
1595
1924
|
for (const filePath of files) {
|
|
1596
1925
|
try {
|
|
1597
|
-
const
|
|
1598
|
-
const classes = isCompiledJS
|
|
1599
|
-
? extractRegisterClassFromCompiledJS(filePath, sourceText, depName)
|
|
1600
|
-
: extractRegisterClassDecorators(filePath, sourceText, depName);
|
|
1926
|
+
const classes = extractRegisterClassForFile(filePath, depName, isCompiledJS);
|
|
1601
1927
|
if (classes.length > 0) {
|
|
1602
1928
|
foundInPackage = true;
|
|
1603
1929
|
allClasses.push(...classes);
|
|
@@ -1642,6 +1968,9 @@ export async function GenerateClassRegistrationsManifest(options) {
|
|
|
1642
1968
|
}
|
|
1643
1969
|
catch (err) {
|
|
1644
1970
|
errors.push(`Error writing manifest: ${err}`);
|
|
1971
|
+
if (cacheFilePath && astCacheDirty) {
|
|
1972
|
+
SaveManifestASTCacheToDisk(cacheFilePath);
|
|
1973
|
+
}
|
|
1645
1974
|
return { success: false, outputPath: absoluteOutputPath, ManifestChanged: false, classes: allClasses, packages: [], totalDepsWalked: depTree.size, AddedDependencies: {}, errors };
|
|
1646
1975
|
}
|
|
1647
1976
|
const uniquePackages = Array.from(new Set(verifiedClasses.map(c => c.packageName))).sort();
|
|
@@ -1753,6 +2082,9 @@ export async function GenerateClassRegistrationsManifest(options) {
|
|
|
1753
2082
|
errors.push(`Error generating lazy config: ${err}`);
|
|
1754
2083
|
}
|
|
1755
2084
|
}
|
|
2085
|
+
if (cacheFilePath && astCacheDirty) {
|
|
2086
|
+
SaveManifestASTCacheToDisk(cacheFilePath);
|
|
2087
|
+
}
|
|
1756
2088
|
return {
|
|
1757
2089
|
success: errors.length === 0,
|
|
1758
2090
|
outputPath: absoluteOutputPath,
|