import * as core from '@actions/core' ; import { promises as fs } from 'fs' ; const lineBraekRe = /\r?\n/ ; export async function getLines ( file ) { return ( await fs . readFile ( file , 'utf8' ) ) . split ( lineBraekRe ) ; } export function updateFeed ( lines , newlines , startFlag , endFlag ) { const startFlagPos = lines . indexOf ( startFlag ) ; const endFlagPos = lines . indexOf ( endFlag , Math . max ( startFlagPos , 0 ) ) ; core . info ( `start_flag position: ${ startFlagPos }` ) ; core . info ( `end_flag position: ${ endFlagPos }` ) ; return insertLines ( lines , newlines , startFlagPos , endFlagPos ) ; } function insertLines ( lines , newlines , startFlagPos , endFlagPos ) { const flagMax = Math . max ( startFlagPos , endFlagPos ) ; const flagMin = Math . min ( startFlagPos , endFlagPos ) ; let newStartPos = startFlagPos ; let newEndPos = endFlagPos ; if ( flagMax < 0 ) { return [ ... lines , ... newlines ] ; } if ( flagMin < 0 ) { newStartPos = newEndPos = flagMax ; } return [ ... lines . slice ( 0 , newStartPos ) , ... newlines , ... lines . slice ( newEndPos + 1 ) ] ; } export async function writeLines ( file , lines ) { await fs . writeFile ( file , lines . join ( '\n' ) ) ; } export async function write ( file , data ) { await fs . writeFile ( file , data ) ; } export function isChanged ( orig , curr ) { if ( orig . length !== curr . length ) { return true ; } return ! orig . every ( ( l , i ) => l === curr [ i ] ) ; }