import * as prettier from "prettier/standalone" ; import * as parserGraphql from "prettier/parser-graphql" ; export function prettify ( str ) { return ( prettier . format ( str , { parser : "graphql" , plugins : [ parserGraphql ] } ) . replace ( /^\s+|\s+$/g , "" ) ) ; } export const padOperation = ( operation , indentation ) : string => operation . split ( "\n" ) . map ( ( s ) => " " . repeat ( indentation ) + s ) . join ( "\n" ) ; const initialWhitespaceRegexp = new RegExp ( /^[\s]*(?=[\w])/g ) ; const endingWhitespaceRegexp = new RegExp ( /[\s]*$/g ) ; export const findOperationPadding = ( operation ) : number => { const initialWhitespace = ( operation . match ( initialWhitespaceRegexp ) || [ ] ) . pop ( ) ; const firstRelevantLine = ( initialWhitespace || "" ) . split ( "\n" ) . pop ( ) ; return firstRelevantLine ? firstRelevantLine . length : 0 ; } ; export const restoreOperationPadding = ( operation , initialOperation ) : string => { const endingWhitespace = ( initialOperation . match ( endingWhitespaceRegexp ) || [ ] ) . join ( "" ) ; return ( "\n" + padOperation ( operation , findOperationPadding ( initialOperation ) ) + endingWhitespace ) ; } ;