import { RequestError } from "@octokit/request-error" ; import { ProbotOctokit } from "probot" ; import * as config from "./config" ; import * as github from "./github" ; import * as prettier from "./prettier" ; export async function formatFiles ( args ) { const prettifiedFiles = new prettier . Result ( ) ; let configChange = false ; for ( let i = 0 ; i < args . files . length ; i ++ ) { const currentFile = args . files [ i ] ; if ( config . isConfigFile ( currentFile ) ) { configChange = true ; } const filePrefix = `${ args . repoPrefix }: FILE ${ i + 1 }/${ args . files . length } (${ currentFile })` ; const prettifyable = await args . configReader . shouldPrettify ( currentFile ) ; if ( ! prettifyable ) { console . log ( `${ filePrefix } - NOT PRETTIFYABLE OR IGNORED` ) ; continue ; } try { var fileContent = await github . loadFile ( { branch : args . branch , filePath : currentFile , octokit : args . octokit , org : args . org , repo : args . repo , } ) ; } catch ( e ) { if ( e instanceof RequestError ) { if ( e . status === 403 ) { continue ; } if ( e . status === 404 ) { continue ; } } throw e ; } const prettierConfigForFile = prettier . applyConfigOverrides ( args . prettierConfig , currentFile ) ; const formatted = prettier . format ( fileContent , currentFile , prettierConfigForFile ) ; if ( formatted === fileContent ) { console . log ( `${ filePrefix } - ALREADY FORMATTED` ) ; continue ; } prettifiedFiles . push ( { path : currentFile , old : fileContent , formatted } ) ; console . log ( `${ filePrefix } - PRETTIFYING` ) ; } return { prettifiedFiles , configChange } ; }