fs

Documentation not complete. Please help us to fill this page :-)

This module implements the file system API specified in CommonJS.

This module is mostly copied from the Mozilla Add-on SDK’s io/file module. its documentation may be useful.

changeWorkingDirectory(path)

It changes the working directory. But only in the SlimerJS “process”. When SlimerJS is terminated, the real working directory is the same when SlimerJS started.

workingDirectory

This property contains the current working directory. It cannot be set. Use changeWorkingDirectory.

Note: in CommonJS, it is defined as a method, but in phantomjs, it is a property. So this is a property to be compatible with phantomjs.

exists(path)

true if the file or directory exists. Otherwise it returns false.

isFile(path)

true if this object corresponds to a normal file. Otherwise it returns false.

isDirectory(path)

true if this object corresponds to a directory. Otherwise it returns false.

isReadable(path)

This method tests whether or not this object corresponds to a file or directory that may be read by the user.

isWritable(path)

true if the file or directory may be modified by the user. Otherwise it returns false.

size(path)

returns the number of bytes corresponding to the data represented by the file.

lastModified(path)

returns a Date object of the last modified time of the file.

read(path, mode)

Shorthand for open(path,mode).read(). Stream is closed after reading.

write(path, content, mode)

Shorthand for open(path,mode).write(content). Stream is closed after writing.

separator

A valid path component separator. May be hard-coded to ‘/’.

join(basepath, dirname, ... ,filename)

Takes a variable number of strings, joins them on the file system’s path separator, and returns the result.

split(path)

Returns an array of path components from a path.

directory(path)

Returns path with the last path component removed.

Not implemented in PhantomJS <=2.1 at least.

dirname(path)

Deprecated, use directory() instead. Not implemented in PhantomJS

base(path)

Returns the final component of a path. Not implemented in PhantomJS <=2.1 at least.

basename(path)

Deprecated, use base() instead. Not implemented in PhantomJS.

absolute(path)

Returns the absolute path of a given path, resolving any components ”.” or ”..” and replacing multiple separators with single separators. Does not resolve Unix symbolic links.

extension(path, withoutdot)

It returns a dot following by the part of the path after the last dot.

The method accept a non standard second parameters, a boolean indicating if you want the extension without the dot character (true) or not.

fs.extension("file.txt"); // returns ".txt"
fs.extension("file.txt", true); // returns "txt"
In SlimerJS 0.9.6 and lower, this method return always the extension without the dot, which is not the result as expected by the CommonJS Filesystem specification.

Not implemented in PhantomJS <=2.1 at least.

list(path)

Returns an array of file names in the given directory.

open(filename, opts)

Returns a stream providing access to the contents of a file.

opts is optional. It can be a single string, indicating the open mode, or an object containing these properties:

  • mode: the open mode

  • charset: the charset code (IANA code). Charset is ignored if the mode is binary mode

  • nobuffer: if true, for mode=”w”, there will be a flush each time you call

    the write method of the stream (SlimerJS only).

Mode is a string that can contain character which describes a characteristic of the returned stream.

  • If the string contains “r”, the file is opened in read-only mode.

  • “w” opens the file in write-only mode.

  • “b” opens the file in binary mode. If “b” is not present, the file is

    opened in text mode, and its contents are assumed to be UTF-8.

  • “a” means to open as “append” mode: the file is open in write-only mode and all written character are append to the file

remove(path)

Removes a file from the file system. To remove directories, use rmdir.

makeDirectory(path)

Create a single directory specified by path. If the directory cannot be created for any reason an exception will be thrown. This includes if the parent directories of “path” are not present.

makeTree(path)

Documentation needed

mkpath(path)

Deprecated. Use makeDirectory(path). Not implemented in PhantomJS.

removeDirectory(path)

Removes a directory from the file system. If the directory is not empty, an exception is thrown.

removeTree(path)

Removes a directory and its contents recursively.

rmdir(path)

Deprecated. Use removeDirectory.

Not implemented in PhantomJS.

copy(source, target)

Documentation needed

copyTree(source, target)

Documentation needed

rename(path, newname)

Documentation needed.

Not implemented in PhantomJS <=2.1 at least.

move(source, target)

Documentation needed

touch(path, date)

Documentation needed

isAbsolute(path)

Documentation needed.

Not defined in the CommonJS specification.

isExecutable(path)

Documentation needed.

Not defined in the CommonJS specification.