by Before Semicolon

File

File details
Details
name file
type class
arguments file absolute path, source directory
description A class representing a file.

Constructor

The constructor takes a required absolute path to the file and an optional source directory.

Properties

ext
type: string, readonly : The extension of the file.
file
type: string, readonly : The name of the file including the extension.
name
type: string, readonly : Name of the file without extension.
filePath
type: string, readonly : The file path relative to the source directory path.
fileAbsolutePath
type: string, readonly : File full absolute path.
fileDirectoryPath
type: string, readonly : File parent directory absolute path.
srcDirectoryPath
type: string, readonly : The directory containing the file, defaults to the parent directory but this can be any ancestor directory. For example, if you put all page files inside the pages directory, this pages directory is the source directory no matter how deep you nest the files and all file relative paths will be to it.
content
type: string | Buffer : A property you can read and write the content of the file. It takes a string or a Buffer as value.

Methods

load
type: function : A function that when called will load the file synchronously. It will read the file every time this method is called and update the internal cached content.
toString
type: function : A function that returns the string representation of the file. Will load the file if the file was never loaded before.
toBuffer
type: function : A function that returns the Buffer representation of the file. Always returns a string version of the file.

Usage Examples

const path = require("path");
const {File} = require("@beforesemicolon/html-plus");

const project = new File(path.resolve(__dirname, 'project.html'));

console.log(`${project.file} content is ${project.toString()}`);