import { constants as fsConstants , promises as fs } from "fs" ; import path from "path" ; import { DataState , SchemaState , State } from "./types/core" ; import log from "./utils/logger" ; param filePath export const stateFileExits = async ( filePath ) : Promise < boolean > => { return fs . access ( path . resolve ( process . cwd ( ) , filePath ) , fsConstants . F_OK ) . then ( ( ) => true ) . catch ( ( ) => false ) ; } ; param filePath export const readStateFile = async ( filePath ) : Promise < State > => { let fileContent = "" ; try { fileContent = await fs . readFile ( path . resolve ( process . cwd ( ) , filePath ) , { encoding : "utf-8" } ) ; } catch ( e ) { log . message ( "error" , `Count not read the state file from ${ filePath }. ${ e instanceof Error && e . message }` ) ; return process . exit ( 1 ) ; } let state = undefined ; try { state = JSON . parse ( fileContent ) ; } catch ( e ) { log . message ( "error" , `Could not parse the state file. ${ e instanceof Error && e . message }` ) ; return process . exit ( 1 ) ; } if ( ! state ?. data || ! state ?. schema ) { log . message ( "error" , "Error: state file seems incorrect" ) ; return process . exit ( 1 ) ; } return state ; } ; param filePath param schemaState param dataState export const createStateFile = async ( filePath , schemaState , dataState ) : Promise < void > => { log . message ( "loading" , "Creating a state file" ) ; const state = { schema : schemaState , data : dataState , } ; const fileName = path . resolve ( process . cwd ( ) , filePath ) ; await fs . writeFile ( fileName , JSON . stringify ( state ) ) ; log . plain ( `Done - ${ path . resolve ( process . cwd ( ) , filePath ) }` ) ; } ;