filewrap

File wrapper library.

Custom file wrapper.

To create a new file you should pass it the file name and a root directory the file is relative to:

const file = new File({ name: 'src/file.txt', root: 'src' })

If you already have the file content and know where it should be written you can set the output and content properties:

const file = new File({
  name: 'src/file.txt',
  root: 'src',
  output: 'public/out.txt',
  content: 'File content' })

new File(options)

Create a new File instance.

You can pass any writable properties in the options object and they will be set on this file.

String name

Raw input file name, may be absolute or relative.

readonly String basename

The basename for the input file assigned when the name is set.

readonly String path

File path relative to the root directory.

If an output path is specified it is used otherwise the raw input file name is used.

This property evaluates the path using the current output or file name so should be used only when necessary but will always reflect the current state of the file not the instantiation state.

Object stat

A stat object associated with the source file.

Object buffer

A buffer containing the source file content.

String output

The output file path.

Buffer content

The content to write to disc.

Boolean transient

Mark a file as transient.

Files marked as transient should never be written to disc.

Function seal

A function used to seal the file contents.

Typically used when parsing a file to an abstract syntax tree and performing transformations on the tree.

String root

The directory path that this file is relative to.

Object hash

An object used to store checksums.

String extension

Extension used to rename the file extension when the output path is set. It should not contain a period.

readonly Object ast

Map of abstract syntax tree objects associated with this file.

File.prototype.getDetails(name)

Get an object with name and file extension properties.

When no name argument is given the basename of this file is used.

Returns object map containing name and extension properties.

File.prototype.write(resolve, reject)

Write this file to disc using the output path and content buffer.

If an error occurs it is passed to the reject function otherwise the resolve function is called.

File.prototype.update()

Update the output content associated with this file.

If a seal function has been assigned it is called and should return the transformed content.

Returns buffer updated file content.

File.prototype.checksum(val, algorithm)

Calculate a checksum for a value.

Returns string hex encoded checksum.

File.prototype.source()

Webpack asset compatible function to access the output content buffer.

Returns buffer file output content.

File.prototype.size()

Webpack asset compatible function to access the output content buffer size.

Returns number of bytes in the output content buffer.