import { now } from 'moment' ; import { Vault , normalizePath } from 'obsidian' ; import { DAY_PLANNER_DEFAULT_CONTENT , DAY_PLANNER_FILENAME } from './constants' ; import MomentDateRegex from './moment-date-regex' ; import { DayPlannerSettings , DayPlannerMode , NoteForDateQuery } from './settings' ; export default class DayPlannerFile { vault : Vault ; settings : DayPlannerSettings ; momentDateRegex : MomentDateRegex ; noteForDateQuery : NoteForDateQuery ; constructor ( vault , settings ) { this . vault = vault ; this . settings = settings ; this . momentDateRegex = new MomentDateRegex ( ) ; this . noteForDateQuery = new NoteForDateQuery ( ) ; } hasTodayNote ( ) { return this . settings . mode === DayPlannerMode . File || this . noteForDateQuery . exists ( this . settings . notesToDates ) ; } todayPlannerFilePath ( ) { if ( this . settings . mode === DayPlannerMode . Command ) { return this . noteForDateQuery . active ( this . settings . notesToDates ) . notePath ; } const fileName = this . todayPlannerFileName ( ) ; return `${ this . settings . customFolder ?? 'Day Planners' }/${ fileName }` ; } todayPlannerFileName ( ) { return this . momentDateRegex . replace ( DAY_PLANNER_FILENAME ) ; } async prepareFile ( ) { try { if ( this . settings . mode === DayPlannerMode . File ) { await this . createFolderIfNotExists ( this . settings . customFolder ) ; await this . createFileIfNotExists ( this . todayPlannerFilePath ( ) ) ; } } catch ( error ) { console . log ( error ) ; } } async createFolderIfNotExists ( path ) { try { const normalizedPath = normalizePath ( path ) ; const folderExists = await this . vault . adapter . exists ( normalizedPath , false ) ; if ( ! folderExists ) { await this . vault . createFolder ( normalizedPath ) ; } } catch ( error ) { console . log ( error ) ; } } async createFileIfNotExists ( fileName ) { try { const normalizedFileName = normalizePath ( fileName ) ; if ( ! await this . vault . adapter . exists ( normalizedFileName , false ) ) { await this . vault . create ( normalizedFileName , DAY_PLANNER_DEFAULT_CONTENT ) ; } } catch ( error ) { console . log ( error ) ; } } async getFileContents ( fileName ) { this . prepareFile ( ) ; try { return await this . vault . adapter . read ( fileName ) ; } catch ( error ) { console . log ( error ) ; } } async updateFile ( fileName , fileContents ) { this . prepareFile ( ) ; try { return await this . vault . adapter . write ( normalizePath ( fileName ) , fileContents ) ; } catch ( error ) { console . log ( error ) ; } } }