import { join , relative , SEP } from "https://deno.land/std@0.85.0/path/mod.ts" ; import { existsSync , Injectable } from "../../deps.ts" ; import { FileInfo } from "../models/file-info.dto.ts" ; @ Injectable ( ) export class FileIOService { get pathDelimiter ( ) { return SEP ; } getUserWorkingDirectoryPath ( subpath ? ) { let pathParts = [ Deno . cwd ( ) ] ; if ( subpath ?. length ) { pathParts = pathParts . concat ( Array . isArray ( subpath ) ? subpath : [ subpath ] ) ; } return join ( ... pathParts ) ; } getRelativePathBetween ( fromFile , toFile ) { const fromFileArr = fromFile . split ( this . pathDelimiter ) ; const toFileArr = toFile . split ( this . pathDelimiter ) ; fromFileArr . pop ( ) ; const toFileName = toFileArr . pop ( ) ; fromFile = this . joinPaths ( ... fromFileArr ) ; toFile = this . joinPaths ( ... toFileArr ) ; const relativePath = relative ( fromFile , toFile ) . replaceAll ( `\\` , `/` ) ; if ( relativePath === "" ) { return `./${ toFileName }` ; } if ( relativePath . startsWith ( "." ) ) { return `${ relativePath }/${ toFileName }` ; } return `./${ relativePath }/${ toFileName }` ; } getDirectoryContents ( dirPath ) { const dirEntries = [ ] ; if ( ! existsSync ( dirPath ) ) { return dirEntries ; } for ( const dirEntry of Deno . readDirSync ( dirPath ) ) { dirEntries . push ( dirEntry ) ; } return dirEntries ; } readFile ( filePath ) { return Deno . readTextFileSync ( filePath ) ; } writeFile ( filePath , contents ) { return Deno . writeTextFileSync ( filePath , contents ) ; } createDirectory ( dirPath ) { return Deno . mkdirSync ( dirPath ) ; } joinPaths ( ... paths ) { return join ( ... paths ) ; } getFileInfo ( pathRelativeToUserDirectory ) { const pathAbsolute = this . getUserWorkingDirectoryPath ( pathRelativeToUserDirectory ) ; const exists = existsSync ( pathAbsolute ) ; const details = exists ? Deno . statSync ( pathAbsolute ) : null ; return new FileInfo ( { pathRelativeToUserDirectory , pathAbsolute , exists , isFile : details ?. isFile ?? false , isDirectory : details ?. isDirectory ?? false } ) ; } }