Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/codemods/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"bin": "dist/index.js",
"scripts": {
"build:cli": "bun build src/cli/index.ts --target node --outdir dist --sourcemap",
"build:pkg": "tsc",
"prepack": "pnpm run build:cli",
"lint": "eslint . --quiet --cache --cache-strategy=content"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/codemods/src/legacy-compat-builders/options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SharedCodemodOptions } from '../utils/options.js';
import type { SharedCodemodOptions } from '../cli/index.js';
import type { LegacyStoreMethod } from './config.js';

export interface Options extends SharedCodemodOptions {
Expand Down
60 changes: 45 additions & 15 deletions packages/codemods/src/schema-migration/codemod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,36 @@ import { basename, extname, join, resolve } from 'path';
import { InstanciatedLogger } from '../../utils/logger.js';
import type { FinalOptions } from './config.js';
import { analyzeModelMixinUsage } from './processors/mixin-analyzer.js';
import type { TransformArtifact } from './utils/ast-utils.js';
import type { ParsedFile } from './utils/file-parser.js';
import { parseFile } from './utils/file-parser.js';
import { FILE_EXTENSION_REGEX, TRAILING_SINGLE_WILDCARD_REGEX, TRAILING_WILDCARD_REGEX } from './utils/string.js';

export type Filename = string;
export type InputFile = { path: string; code: string };

export type SkipReason =
| 'dts-file'
| 'file-not-found'
| 'already-processed'
| 'intermediate-model'
| 'parse-error'
| 'invalid-model'
| 'not-mixin-file-type'
| 'mixin-not-connected'
| 'empty-artifacts';

export interface SkippedFile {
file: string;
reason: SkipReason;
phase: 'discovery' | 'parsing' | 'generation';
}

export interface TransformerResult {
artifacts: TransformArtifact[];
skipReason?: SkipReason;
}

/**
* Check if a file path matches any intermediate model path
*/
Expand Down Expand Up @@ -66,25 +89,26 @@ function expandGlobPattern(dir: string): string {

async function findFiles(
sources: string[],
predicate: (file: string) => boolean,
predicate: (file: string) => SkipReason | null,
finalOptions: FinalOptions,
logger: InstanciatedLogger
): Promise<{ output: InputFile[]; skipped: string[]; errors: Error[] }> {
): Promise<{ output: InputFile[]; skipped: SkippedFile[]; errors: Error[] }> {
const output: InputFile[] = [];
const errors: Error[] = [];
const skipped: string[] = [];
const skipped: SkippedFile[] = [];

for (const source of sources) {
try {
const files = await glob(source);

for (const file of files) {
if (predicate(file)) {
const skipReason = predicate(file);
if (skipReason === null) {
const content = await readFile(file, 'utf-8');

output.push({ path: file, code: content });
} else {
skipped.push(file);
skipped.push({ file, reason: skipReason, phase: 'discovery' });
}
}

Expand All @@ -107,7 +131,7 @@ export class Input {
mixins: Map<Filename, InputFile> = new Map();
parsedModels: Map<Filename, ParsedFile> = new Map();
parsedMixins: Map<Filename, ParsedFile> = new Map();
skipped: string[] = [];
skipped: SkippedFile[] = [];
errors: Error[] = [];
}

Expand Down Expand Up @@ -150,7 +174,6 @@ export class Codemod {

let modelsParsed = 0;
let mixinsParsed = 0;
let parseErrors = 0;

for (const [filePath, inputFile] of this.input.models) {
try {
Expand All @@ -159,7 +182,7 @@ export class Codemod {
modelsParsed++;
} catch (error) {
this.logger.error(`❌ Error parsing model ${filePath}: ${String(error)}`);
parseErrors++;
this.input.skipped.push({ file: filePath, reason: 'parse-error', phase: 'parsing' });
}
}

Expand All @@ -170,10 +193,11 @@ export class Codemod {
mixinsParsed++;
} catch (error) {
this.logger.error(`❌ Error parsing mixin ${filePath}: ${String(error)}`);
parseErrors++;
this.input.skipped.push({ file: filePath, reason: 'parse-error', phase: 'parsing' });
}
}

const parseErrors = this.input.skipped.filter((s) => s.reason === 'parse-error').length;
this.logger.info(`✅ Parsed ${modelsParsed} models and ${mixinsParsed} mixins (${parseErrors} errors).`);
}

Expand Down Expand Up @@ -208,11 +232,14 @@ export class Codemod {
const models = await findFiles(
fileSources,
(file) => {
return (
existsSync(file) &&
(!this.finalOptions.skipProcessed || !isAlreadyProcessed(file)) &&
!isIntermediateModel(file, this.finalOptions.intermediateModelPaths, this.finalOptions.additionalModelSources)
);
if (file.endsWith('.d.ts')) return 'dts-file';
if (!existsSync(file)) return 'file-not-found';
if (this.finalOptions.skipProcessed && isAlreadyProcessed(file)) return 'already-processed';
if (
isIntermediateModel(file, this.finalOptions.intermediateModelPaths, this.finalOptions.additionalModelSources)
)
return 'intermediate-model';
return null;
},
this.finalOptions,
this.logger
Expand Down Expand Up @@ -242,7 +269,10 @@ export class Codemod {
const models = await findFiles(
fileSources,
(file) => {
return existsSync(file) && (!this.finalOptions.skipProcessed || !isAlreadyProcessed(file));
if (file.endsWith('.d.ts')) return 'dts-file';
if (!existsSync(file)) return 'file-not-found';
if (this.finalOptions.skipProcessed && isAlreadyProcessed(file)) return 'already-processed';
return null;
},
this.finalOptions,
this.logger
Expand Down
33 changes: 18 additions & 15 deletions packages/codemods/src/schema-migration/processors/mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { existsSync } from 'fs';
import { join } from 'path';

import { logger } from '../../../utils/logger.js';
import type { TransformerResult } from '../codemod.js';
import type { TransformOptions } from '../config.js';
import type { ExtractedType, PropertyInfo, SchemaField, TransformArtifact } from '../utils/ast-utils.js';
import {
Expand Down Expand Up @@ -129,12 +130,12 @@ export default function transform(filePath: string, source: string, options: Tra
* This does not modify the original source. The CLI can use this to write
* files to the requested output directories.
*/
export function toArtifacts(parsedFile: ParsedFile, options: TransformOptions): TransformArtifact[] {
export function toArtifacts(parsedFile: ParsedFile, options: TransformOptions): TransformerResult {
const { path: filePath, source, baseName, camelName: mixinName } = parsedFile;

if (parsedFile.fileType !== 'mixin') {
log.debug('Not a mixin file, returning empty artifacts');
return [];
return { artifacts: [], skipReason: 'not-mixin-file-type' };
}

const traitFields = parsedFile.fields.map((f) => ({
Expand All @@ -161,19 +162,21 @@ export function toArtifacts(parsedFile: ParsedFile, options: TransformOptions):

if (!isConnectedToModel) {
log.debug(`Skipping ${mixinName}: not connected to any models`);
return [];
return { artifacts: [], skipReason: 'mixin-not-connected' };
}

return generateMixinArtifacts(
filePath,
source,
baseName,
mixinName,
traitFields,
extensionProperties,
extendedTraits,
options
);
return {
artifacts: generateMixinArtifacts(
filePath,
source,
baseName,
mixinName,
traitFields,
extensionProperties,
extendedTraits,
options
),
};
}

/**
Expand Down Expand Up @@ -231,7 +234,7 @@ function generateMixinArtifacts(

collectTraitImports(extendedTraits, imports, options);

const traitSchemaName = traitInterfaceName;
const traitSchemaName = `${toPascalCase(baseName)}Schema`;
const traitInternalName = pascalToKebab(mixinName);
const traitSchemaObject = buildTraitSchemaObject(traitFields as SchemaField[], extendedTraits, {
name: traitInternalName,
Expand Down Expand Up @@ -264,7 +267,7 @@ function generateMixinArtifacts(
filePath,
source,
baseName,
`${mixinName}Extension`,
`${toPascalCase(mixinName)}Extension`,
extensionProperties,
options,
traitInterfaceName,
Expand Down
44 changes: 13 additions & 31 deletions packages/codemods/src/schema-migration/processors/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
import { dirname, join, resolve } from 'path';

import { logger } from '../../../utils/logger.js';
import type { Filename } from '../codemod.js';
import type { Filename, TransformerResult } from '../codemod.js';
import type { TransformOptions } from '../config.js';
import type { ExtractedType, SchemaField, TransformArtifact } from '../utils/ast-utils.js';
import {
Expand Down Expand Up @@ -54,7 +54,7 @@ import {
NODE_KIND_METHOD_DEFINITION,
NODE_KIND_PROPERTY_IDENTIFIER,
} from '../utils/code-processing.js';
import { appendExtensionSignatureType, createExtensionFromOriginalFile } from '../utils/extension-generation.js';
import { createExtensionFromOriginalFile } from '../utils/extension-generation.js';
import type { ParsedFile } from '../utils/file-parser.js';
import { isClassMethodSyntax } from '../utils/file-parser.js';
import { replaceWildcardPattern } from '../utils/path-utils.js';
Expand Down Expand Up @@ -294,7 +294,7 @@ function extractHeritageInfo(
const mixinImports = getMixinImports(root, options);
mixinTraits.push(...extractMixinTraits(heritageClause, root, mixinImports, options));

const mixinExts = extractMixinExtensions(heritageClause, root, mixinImports, filePath, options);
const mixinExts = extractMixinExtensions(filePath, options);
mixinExtensions.push(...mixinExts);

if (options?.intermediateModelPaths && options.intermediateModelPaths.length > 0) {
Expand Down Expand Up @@ -649,6 +649,7 @@ function generateRegularModelArtifacts(
const schemaObject = buildLegacySchemaObject(baseName, schemaFields, mixinTraits, mixinExtensions, isFragment);

// Generate merged schema code (schema + types in one file)
const extensionName = extensionProperties.length > 0 ? `${modelName}Extension` : undefined;
const mergedSchemaCode = generateMergedSchemaCode({
baseName,
interfaceName: modelName,
Expand All @@ -659,6 +660,7 @@ function generateRegularModelArtifacts(
imports: schemaImports,
isTypeScript,
options,
extensionName,
});

artifacts.push({
Expand All @@ -668,8 +670,7 @@ function generateRegularModelArtifacts(
suggestedFileName: `${baseName}.schema${originalExtension}`,
});

// Create extension artifact preserving original file content
const modelInterfaceName = modelName;
const modelInterfaceName = `${modelName}Trait`;
const modelImportPath = options?.resourcesImport
? `${options.resourcesImport}/${baseName}.schema`
: `../resources/${baseName}.schema`;
Expand All @@ -692,27 +693,18 @@ function generateRegularModelArtifacts(
artifacts.push(extensionArtifact);
}

// Create extension signature type alias if there are extension properties
log.debug(
`Extension properties length: ${extensionProperties.length}, extensionArtifact exists: ${!!extensionArtifact}`
);
log.debug(`Extension properties: ${JSON.stringify(extensionProperties.map((p) => p.name))}`);
if (extensionProperties.length > 0 && extensionArtifact) {
appendExtensionSignatureType(extensionArtifact, modelName);
}

return artifacts;
}

export function toArtifacts(parsedFile: ParsedFile, options: TransformOptions): TransformArtifact[] {
export function toArtifacts(parsedFile: ParsedFile, options: TransformOptions): TransformerResult {
log.debug(`=== DEBUG: Processing ${parsedFile.path} ===`);

const analysis = analyzeModelFromParsed(parsedFile, options);
if (!analysis.isValid) {
log.debug('Model analysis failed, skipping artifact generation');
return [];
return { artifacts: [], skipReason: 'invalid-model' };
}
return generateRegularModelArtifacts(parsedFile.path, parsedFile.source, analysis, options);
return { artifacts: generateRegularModelArtifacts(parsedFile.path, parsedFile.source, analysis, options) };
}

/**
Expand Down Expand Up @@ -919,9 +911,6 @@ function generateIntermediateModelTraitArtifacts(
);
if (extensionArtifact) {
artifacts.push(extensionArtifact);

// Create extension signature type alias if there are extension properties
appendExtensionSignatureType(extensionArtifact, traitPascalName);
}
}

Expand Down Expand Up @@ -1689,17 +1678,9 @@ function extractMixinTraits(
}

/**
* Extract mixin extensions for a model file
* Uses the pre-computed modelToMixinsMap to look up which mixins this model uses,
* then checks the mixinExtensionCache to get extension names for mixins with extension properties
* Get mixin extension names based on model imports.
*/
function extractMixinExtensions(
_heritageClause: SgNode,
_root: SgNode,
_mixinImports: Map<string, string>,
filePath: string,
options?: TransformOptions
): string[] {
function extractMixinExtensions(filePath: string, options?: TransformOptions): string[] {
const mixinExtensions: string[] = [];

const modelMixins = options?.modelToMixinsMap?.get(filePath);
Expand All @@ -1711,7 +1692,8 @@ function extractMixinExtensions(
// Check the mixinExtensionCache to see if this mixin has an extension
const cacheEntry = mixinExtensionCache.get(mixinFilePath);
if (cacheEntry?.hasExtension && cacheEntry.extensionName) {
mixinExtensions.push(cacheEntry.extensionName);
const basePart = cacheEntry.extensionName.replace(/Extension$/, '');
mixinExtensions.push(`${toPascalCase(basePart)}Extension`);
}
}

Expand Down
Loading
Loading