makestatic-filewrap v1.0.35
Filewrap
File object wrapper
Wraps a file path as a complex object with many useful properties and functions.
Install
yarn add makestatic-filewrapAPI
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.
optionsfile options.
Options
nameString the file name.rootString the directory the file is relative to.outputString the output file path to write to.contentBuffer|String the file output content.bufferBuffer|String the file source content.extensionString an output file extension.sealFunction a function used to update the file content.
name
String nameRaw input file name, may be absolute or relative.
basename
readonly String basenameThe basename for the input file assigned when the name is set.
path
readonly String pathFile 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 statA stat object associated with the source file.
buffer
Object bufferA buffer containing the source file content.
output
String outputThe output file path.
content
Buffer contentThe content to write to disc.
transient
Boolean transientMark a file as transient.
Files marked as transient should never be written to disc.
seal
Function sealA 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 rootThe directory path that this file is relative to.
hash
Object hashAn object used to store checksums.
extension
String extensionExtension used to rename the file extension when the output path
is set. It should not contain a period.
ast
readonly Object astMap 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.
nameString 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.
resolveFunction called when the file is written.rejectFunction called if the file write errors.
Throws
Errorif this file has already been written to disc.Errorif 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.
valString|Buffer the value for the checksum.algorithmString=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
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago