Skip to main content

@rtorcato/js-common / file

file

Functions​

readFileAsString()​

readFileAsString(filePath): Promise<string>

Defined in: file/index.ts:15

Reads a file as a string (UTF-8).

Example​

await readFileAsString('./package.json') // '{\n "name": "@rtorcato/js-common",\n ...'

Parameters​

filePath​

string

The path to the file.

Returns​

Promise<string>

The file contents as a string.


writeFileAsString()​

writeFileAsString(filePath, data): Promise<void>

Defined in: file/index.ts:32

Writes a string to a file (UTF-8).

Example​

await writeFileAsString('./out.txt', 'hello')
await readFileAsString('./out.txt') // 'hello'

Parameters​

filePath​

string

The path to the file.

data​

string

The string data to write.

Returns​

Promise<void>


fileExists()​

fileExists(filePath): Promise<boolean>

Defined in: file/index.ts:48

Checks if a file exists.

Example​

await fileExists('./package.json') // true
await fileExists('./nope.txt') // false

Parameters​

filePath​

string

The path to the file.

Returns​

Promise<boolean>

True if the file exists, false otherwise.


deleteFile()​

deleteFile(filePath): Promise<void>

Defined in: file/index.ts:62

Deletes a file.

Parameters​

filePath​

string

The path to the file.

Returns​

Promise<void>


getFileExtension()​

getFileExtension(filePath): string

Defined in: file/index.ts:71

Gets the file extension from a file path.

Parameters​

filePath​

string

The path to the file.

Returns​

string

The file extension (including the dot), or an empty string if none.