import { API , FileInfo , Options } from 'jscodeshift' ; const middlewares = [ 'myself' , 'authenticatedOnly' , 'requiredRoles' , 'requiredFeatures' , 'validationSchema' , ] ; export function graphqlExtensions ( file , api , options ) { const j = api . jscodeshift ; const root = j ( file . source ) ; const printOptions = options . printOptions || { quote : 'single' , trailingComma : false , lineTerminator : '\n' } ; root . find ( j . ExportDefaultDeclaration ) . filter ( ( path ) => { if ( path . node . declaration . type === 'ObjectExpression' ) { if ( path . node . declaration . properties . length > 1 ) { const [ spread , ... middleware ] = path . node . declaration . properties ; if ( middlewares . includes ( middleware [ 0 ] . key . name ) ) { return true ; } } } return false ; } ) . forEach ( ( path ) => { const { declaration } = path . node ; const [ spread , ... middleware ] = declaration . properties ; const expressionObject = j . objectProperty ( j . identifier ( 'extensions' ) , j . objectExpression ( middleware ) ) ; j ( path ) . replaceWith ( j . exportDefaultDeclaration ( j . objectExpression ( [ spread , expressionObject ] ) ) ) ; return path . node ; } ) ; return root . toSource ( printOptions ) ; } export const parser = 'tsx' ; export default graphqlExtensions ;