import { API, FileInfo, Identifier, MemberExpression, ObjectExpression } from 'jscodeshift'; import { getCallExpressions, getFlexVariable, getUIConfig } from '../../utils/parse'; import { ComponentType, LayoutReport, TransformError, UIConfig } from '../../types/runner'; import { saveTempData } from '../../utils/file'; import { updateUIConfig } from '../../utils/transform'; import { RegisterAPI } from '../../types/transformer'; export default async (file: FileInfo, api: API, options: { reportDir: string }): Promise => { const errors: TransformError[] = []; const uiConfig: UIConfig = { components: {}, }; try { const j = api.jscodeshift; const root = j(file.source); const flexVariable = getFlexVariable(root, j); if (flexVariable) { const callExpressions = getCallExpressions(flexVariable, root, j); callExpressions.forEach((exp) => { const registerMethod = ((exp.node.callee as MemberExpression)?.property as Identifier)?.name as RegisterAPI; if (Object.values(RegisterAPI).includes(registerMethod)) { const objectExpression = exp.node.arguments.find( (arg) => arg.type === 'ObjectExpression', ) as ObjectExpression; const { config, operation } = getUIConfig(objectExpression, registerMethod); if (config.componentHint) { updateUIConfig(uiConfig, config.componentHint, operation, { type: ComponentType.Custom, custom_component: config.customComponent, key: config.keyHint, ref: config.ref, }); } } }); } } catch (e: any) { errors.push({ file: file.path, message: e.message, }); } await saveTempData( { errors, uiConfig, }, options.reportDir, ); return file.source; };