makestatic-filewrap
v1.0.35
Published
File wrapper library
Downloads
11
Readme
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