wilton.js  v202103141
 All Namespaces Functions
Functions
fs Namespace Reference

wilton/fs
File I/O and file-system operations. More...

Functions

Undefined appendFile (String path, String|Array data, Object|Undefined options, Function|Undefined callback)
 Append data to a file, creating the file if it does not yet exist.
Undefined copyDirectory (String oldPath, String newPath, Function|Undefined callback)
 Copy a directory.
Undefined copyFile (String oldPath, String newPath, Function|Undefined callback)
 Copy a file.
Boolean exists (String path, Function|Undefined callback)
 Test whether or not the given path exists by checking with the file system.
Undefined insertFile (String sourcePath, String destPath, Number offset, Function|Undefined callback)
 Writes the contents from one file to another.
Boolean isDirectory (String path, Function|Undefined callback)
 Check whether path is a directory.
Boolean isFile (String path, Function|Undefined callback)
 Check whether path is an ordinary file.
Undefined mkdir (String path, Undefined mode, Function|Undefined callback)
 Create a directory.
Array readdir (String path, Undefined options, Function|Undefined callback)
 List the contents of a directory.
String readFile (String path, Object|Undefined options, Function|Undefined callback)
 Read the entire contents of a file.
Array readLines (String path, Function|Undefined callback)
 Reads all lines from a file.
String realpath (String path, Undefined options, Function|Undefined callback)
 Convert relative path into absolute one.
Undefined rename (String oldPath, String newPath, Function|Undefined callback)
 Rename (move) file or directory.
Undefined resizeFile (String path, Number size, Function|Undefined callback)
 Resizes file.
Undefined rmdir (String path, Function|Undefined callback)
 Delete directory.
Object stat (String path, Function|Undefined callback)
 Read file or directory status.
Undefined symlink (String dest, String link, Function|Undefined callback)
 Create a symbolic link.
Undefined unlink (String path, Function|Undefined callback)
 Delete file.
Undefined writeFile (String path, String|Array data, Object|Undefined options, Function|Undefined callback)
 Write specified data to a file, overwriting a file if it exists.

Detailed Description

This module implements a synchronous-only subset of fs module from Node.js.

It provides wrappers around standard posix functions.

Every function in this module has an alias with a Sync postfix (to match Node.js convention), example: readFile() is the same as readFileSync.

Usage example:

// make directory
fs.mkdir("fstest");
// write data to file
fs.writeFile("fstest/test.txt", "foo");
fs.appendFile("fstest/test.txt", "bar");
// read file
fs.readFile("fstest/test.txt"); // "foobar"
// get file status
var props = fs.stat("fstest/test.txt");

Function Documentation

Undefined fs::appendFile ( String  path,
String|Array  data,
Object|Undefined  options,
Function|Undefined  callback 
)

Appends data to a file, creating the file if it does not yet exist.

Parameters
pathString path to file
dataString|Array data to write, can be a string or an array of strings; options.delimiter string is appended after every array element except the last one
optionsObject|Undefined configuration object, can be omitted, see possible options below
callbackFunction|Undefined callback to receive result or error
Returns
Undefined

Options

  • hex Boolean|Undefined whether data is specified in hexadecimal encoding and needs to be converted to binary before appending it to file; false by default
  • delimiter String|Undefined string that is appended after every array element except the last one, default value: \n
Undefined fs::copyDirectory ( String  oldPath,
String  newPath,
Function|Undefined  callback 
)

Copies specified directory recursively.

Parameters
oldPathString existing path
newPathString desired path
callbackFunction|Undefined callback to receive result or error
Returns
Undefined
Undefined fs::copyFile ( String  oldPath,
String  newPath,
Function|Undefined  callback 
)

Copies specified file.

Parameters
oldPathString existing path
newPathString desired path
callbackFunction|Undefined callback to receive result or error
Returns
Undefined
Boolean fs::exists ( String  path,
Function|Undefined  callback 
)

Tests whether or not the given path exists by checking with the file system.

Parameters
pathString path to file or directory
callbackFunction|Undefined callback to receive result or error
Returns
Boolean true if specified path exists, false otherwise
Undefined fs::insertFile ( String  sourcePath,
String  destPath,
Number  offset,
Function|Undefined  callback 
)

Writes the contents of the specified source file to destination file, starts from offset.

Parameters
sourcePathString path to source file
destPathString path to destination file
offsetNumber offset from destination file write position
callbackFunction|Undefined callback to receive result or error
Returns
Undefined
Boolean fs::isDirectory ( String  path,
Function|Undefined  callback 
)

Checks whether specified path is a directory. Uses stat(...).isDirectory.

Parameters
pathString
callbackFunction|Undefined callback to receive result or error
Returns
Boolean true if specified path is a directory, false otherwise
Boolean fs::isFile ( String  path,
Function|Undefined  callback 
)

Checks whether specified path is an ordinary file. Uses stat(...).isFile.

Parameters
pathString
callbackFunction|Undefined callback to receive result or error
Returns
Boolean true if specified path is an ordinary file, false otherwise
Undefined fs::mkdir ( String  path,
Undefined  mode,
Function|Undefined  callback 
)

Creates a directory.

Parameters
pathString path to directory
modeUndefined placeholder parameter, added for compatibility with Node API
callbackFunction|Undefined callback to receive result or error
Returns
Undefined
Array fs::readdir ( String  path,
Undefined  options,
Function|Undefined  callback 
)

Lists the contents of a directory.

Parameters
pathString path to directory
optionsUndefined placeholder parameter, added for compatibility with Node API
callbackFunction|Undefined callback to receive result or error
Returns
Array list of files and directories inside the specified directory
String fs::readFile ( String  path,
Object|Undefined  options,
Function|Undefined  callback 
)

Reads the entire contents of a file.

Parameters
pathString path to file
optionsObject|Undefined configuration object, can be omitted, see possible options below
callbackFunction|Undefined callback to receive result or error
Returns
String file contents

Options

  • hex Boolean|Undefined whether data read from file needs to be converted to hexadecimal encoding before returning it to caller; false by default
Array fs::readLines ( String  path,
Function|Undefined  callback 
)

Reads the entire contents of a text file and return them as an array of lines.

Empty strings are ignored.

Parameters
pathString path to file
callbackFunction|Undefined callback to receive result or error
Returns
Array file contents as an array of lines
String fs::realpath ( String  path,
Undefined  options,
Function|Undefined  callback 
)

Converts relative path into absolute one calling FS.

Parameters
pathString relative path
optionsUndefined placeholder parameter, added for compatibility with Node API
callbackFunction|Undefined callback to receive result or error
Returns
String absolute path
Undefined fs::rename ( String  oldPath,
String  newPath,
Function|Undefined  callback 
)

Renames (moves) file or directory.

Parameters
oldPathString existing path
newPathString desired path
callbackFunction|Undefined callback to receive result or error
Returns
Undefined
Undefined fs::resizeFile ( String  path,
Number  size,
Function|Undefined  callback 
)

Resizes existing file or creates file with specified size

Parameters
pathString path to file
sizeNumber offset from current target file write position
callbackFunction|Undefined callback to receive result or error
Returns
Undefined
Undefined fs::rmdir ( String  path,
Function|Undefined  callback 
)

Deletes directory recursively.

Parameters
pathString path to directory
callbackFunction|Undefined callback to receive result or error
Returns
Undefined
Object fs::stat ( String  path,
Function|Undefined  callback 
)

Reads file or directory status.

Parameters
pathString
callbackFunction|Undefined callback to receive result or error
Returns
Object with the following fields:
  • size Number file size in bytes, 0 for directory
  • isFile Boolean whether it is a file
  • isDirectory Boolean whether it is a directory
Undefined fs::symlink ( String  dest,
String  link,
Function|Undefined  callback 
)

Creates a symbolic link to file or directory.

Parameters
destString destination path
linkString path to link to create
callbackFunction|Undefined callback to receive result or error
Returns
Undefined
Undefined fs::unlink ( String  path,
Function|Undefined  callback 
)

Deletes file.

Parameters
pathString path to file
callbackFunction|Undefined callback to receive result or error
Returns
Undefined
Undefined fs::writeFile ( String  path,
String|Array  data,
Object|Undefined  options,
Function|Undefined  callback 
)

Writess pecified data to a file, overwriting a file if it exists.

Parameters
pathString path to file
dataString|Array data to write, can be a string or an array of strings; options.delimiter string is appended after every array element except the last one
optionsObject|Undefined configuration object, can be omitted, see possible options below
callbackFunction|Undefined callback to receive result or error
Returns
Undefined

Options

  • hex Boolean|Undefined whether data is specified in hexadecimal encoding and needs to be converted to binary before writing it to file; false by default
  • delimiter String|Undefined string that is appended after every array element except the last one, default value: \n