import type { Source } from '@graphql-tools/utils' ; import fs from 'fs' ; import { dirname } from 'path' ; import { promisify } from 'util' ; const _mkdir = promisify ( fs . mkdir ) ; const _readFile = promisify ( fs . readFile ) ; const _writeFile = promisify ( fs . writeFile ) ; export const readFile = async ( path ) => _readFile ( path , { encoding : 'utf-8' } ) ; export const writeFile = async ( path , data ) => { await _mkdir ( dirname ( path ) , { recursive : true } ) ; await _writeFile ( path , data , { encoding : 'utf-8' } ) ; } ; export const delay = ( ms ) => new Promise < void > ( res => setTimeout ( res , ms ) ) ; export const formatLanguage = ( lang ) => ( ( lang === 'typescript' ) ? 'TypeScript' : 'Flow' ) ; export function deduplicateFragmentFromDocuments ( documents ) { const existFragmentNames = new Set < string > ( ) ; return documents . map ( source => { const { document } = source ; if ( ! document ) { return source ; } const uniqDefinitions = document . definitions . filter ( def => { if ( def . kind !== 'FragmentDefinition' ) { return true ; } const fragmentName = def . name . value ; const duplicated = existFragmentNames . has ( fragmentName ) ; if ( ! duplicated ) existFragmentNames . add ( fragmentName ) ; return ! duplicated ; } ) ; return { ... source , document : { ... document , definitions : uniqDefinitions } } ; } ) ; }