Home Manual Reference Source
public class | source

TempFiles

A utility service to read, write and delete temporary files.

Constructor Summary

Public Constructor
public

constructor(info: Object, pathUtils: PathUtils, directory: string, locationName: string)

Class constructor.

Member Summary

Public Members
public

The location name for the temp directory path on the pathUtils service.

public

A local reference for the pathUtils service.

Method Summary

Public Methods
public

delete(filepath: string): Promise<string, Error>

Delete a file from the temp directory.

public

deleteSync(filepath: string): string

Delete a file from the temp directory, sync version.

public

Ensure that the temp directory exists

public

Ensure that the temp directory exists, sync version.

public

path(rest: Array): string

Generate a path for the temp directory.

public

read(filepath: string, encoding: string): Promise<string, Error>

Read a file from the temp directory.

public

readSync(filepath: string, encoding: string): string

Read a file from the temp directory, sync version.

public

write(filepath: string, data: string): Promise<string, Error>

Write a file on the temp directory.

public

writeSync(filepath: string, data: string): string

Write a file on the temp directory, sync version.

Public Constructors

public constructor(info: Object, pathUtils: PathUtils, directory: string, locationName: string) source

Class constructor.

Params:

NameTypeAttributeDescription
info Object

The application package.json, necessary to get the module name and build the path of the temp directory.

pathUtils PathUtils

To Register the temp directory location and build the paths to the files.

directory string
  • optional
  • default: '.tmp'

The name of the temp directory.

locationName string
  • optional
  • default: 'temp'

The name that will be used to register the temp directory path as a location on the pathUtils service.

Public Members

public locationName: string source

The location name for the temp directory path on the pathUtils service.

public pathUtils: PathUtils source

A local reference for the pathUtils service.

Public Methods

public delete(filepath: string): Promise<string, Error> source

Delete a file from the temp directory.

Params:

NameTypeAttributeDescription
filepath string

The path to the file.

Return:

Promise<string, Error>

On success, the promise resolves with the absolute path to the file.

public deleteSync(filepath: string): string source

Delete a file from the temp directory, sync version.

Params:

NameTypeAttributeDescription
filepath string

The path to the file.

Return:

string

The absolute path to the file.

Throw:

Error

If the method couldn't delete the file.

public ensureDirectory(): Promise<undefined, Error> source

Ensure that the temp directory exists

Return:

Promise<undefined, Error>

public ensureDirectorySync(): * source

Ensure that the temp directory exists, sync version.

Return:

*

Throw:

Error

If the directory can't be created.

public path(rest: Array): string source

Generate a path for the temp directory.

Params:

NameTypeAttributeDescription
rest Array

The rest of the components that will be added to the path after the one for the temp directory.

Return:

string

public read(filepath: string, encoding: string): Promise<string, Error> source

Read a file from the temp directory.

Params:

NameTypeAttributeDescription
filepath string

The path to the file.

encoding string
  • optional
  • default: 'utf-8'

The text encoding in which the file should be read.

Return:

Promise<string, Error>

public readSync(filepath: string, encoding: string): string source

Read a file from the temp directory, sync version.

Params:

NameTypeAttributeDescription
filepath string

The path to the file.

encoding string
  • optional
  • default: 'utf-8'

The text encoding in which the file should be read.

Return:

string

Throw:

Error

If the file can't be read.

public write(filepath: string, data: string): Promise<string, Error> source

Write a file on the temp directory.

Params:

NameTypeAttributeDescription
filepath string

The path to the file.

data string

The contents of the file.

Return:

Promise<string, Error>

On success, the promise resolves with the absolute path to the file.

public writeSync(filepath: string, data: string): string source

Write a file on the temp directory, sync version.

Params:

NameTypeAttributeDescription
filepath string

The path to the file.

data string

The contents of the file.

Return:

string

The absolute path to the file.

Throw:

Error

If the method couldn't write on the file.