import * as fs from 'fs' ; import { EOL } from 'os' ; param string return export function cleanCsvStorage ( rows ) { return rows . filter ( ( row ) => row ?. trim ( ) ?. length ?? 0 > 0 ) ; } param pathToFile return export function * getCsvFileLinesAsIterable ( pathToFile ) { let fileContent = '' ; try { fileContent = fs . readFileSync ( pathToFile , 'utf8' ) ; } catch ( error ) { console . log ( 'Error reading header from file' , pathToFile , error ) ; return ; } for ( const line of cleanCsvStorage ( fileContent . split ( EOL ) ) ) { yield line ; } } param pathToFile return export function getCsvFileLinesAsArray ( pathToFile ) { return Array . from ( getCsvFileLinesAsIterable ( pathToFile ) ) ; } param pathToFile export const getCsvFileHeader = ( pathToFile ) : string => { return getCsvFileLinesAsIterable ( pathToFile ) . next ( ) . value || '' ; } ; param pathToFile param rows param overwrite return export function setCsvFileLines ( pathToFile , rows , overwrite = true ) { const content = cleanCsvStorage ( rows ) . join ( EOL ) + EOL ; try { if ( overwrite ) { fs . writeFileSync ( pathToFile , content ) ; } else { fs . appendFileSync ( pathToFile , content ) ; } return true ; } catch ( error ) { console . log ( 'Error writing content of file' , pathToFile , error ) ; return false ; } } param pathToFile param fileContent return export function setCsvFileContent ( pathToFile , fileContent ) { try { fs . writeFileSync ( pathToFile , fileContent , 'utf8' ) ; return true ; } catch ( error ) { console . log ( 'Error writing content of file' , pathToFile , error ) ; return false ; } }