1.0.35 • Published 6 years ago

makestatic-filewrap v1.0.35

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

Filewrap

File object wrapper

Wraps a file path as a complex object with many useful properties and functions.



Install

yarn add makestatic-filewrap

API

File

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' })

File

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.

  • options file options.
Options
  • name String the file name.
  • root String the directory the file is relative to.
  • output String the output file path to write to.
  • content Buffer|String the file output content.
  • buffer Buffer|String the file source content.
  • extension String an output file extension.
  • seal Function a function used to update the file content.

name

String name

Raw input file name, may be absolute or relative.

basename

readonly String basename

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

path

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.

stat

Object stat

A stat object associated with the source file.

buffer

Object buffer

A buffer containing the source file content.

output

String output

The output file path.

content

Buffer content

The content to write to disc.

transient

Boolean transient

Mark a file as transient.

Files marked as transient should never be written to disc.

seal

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.

root

String root

The directory path that this file is relative to.

hash

Object hash

An object used to store checksums.

extension

String extension

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

ast

readonly Object ast

Map of abstract syntax tree objects associated with this file.

.getDetails

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.

  • name String the name of the file.

.write

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.

  • resolve Function called when the file is written.
  • reject Function called if the file write errors.
Throws
  • Error if this file has already been written to disc.
  • Error if the file write fails.

.update

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.

.checksum

File.prototype.checksum(val, algorithm)

Calculate a checksum for a value.

Returns string hex encoded checksum.

  • val String|Buffer the value for the checksum.
  • algorithm String=sha512 the checksum algorithm.

.source

File.prototype.source()

Webpack asset compatible function to access the output content buffer.

Returns buffer file output content.

.size

File.prototype.size()

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

Returns number of bytes in the output content buffer.

License

MIT


Created by mkdoc on March 12, 2017

1.0.35

6 years ago

1.0.34

6 years ago

1.0.33

7 years ago

1.0.32

7 years ago

1.0.31

7 years ago

1.0.30

7 years ago

1.0.29

7 years ago

1.0.28

7 years ago

1.0.27

7 years ago

1.0.26

7 years ago

1.0.25

7 years ago

1.0.24

7 years ago

1.0.23

7 years ago

1.0.22

7 years ago

1.0.21

7 years ago

1.0.20

7 years ago

1.0.19

7 years ago

1.0.18

7 years ago

1.0.17

7 years ago

1.0.16

7 years ago

1.0.15

7 years ago

1.0.14

7 years ago

1.0.13

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago