Skip to content
jcreamer898 edited this page Oct 19, 2012 · 9 revisions

fileSystem.js

The core module, fileSystem.js is a wrapper around many of the common node.js file manipulation commands. The API is documented below.

anvil.fs.buildFileData( baseline, workingBase, file )

arguments

  • baseline:
  • workingBase:
  • file:

anvil.fs.buildPath( pathSpec )

Build a local path for a folder or file in a project.

arguments

  • pathSpec [string] required : Specify a path around which to build a full fileSystem path.

example

var pathath = anvil.fs.buildPath( "src/lib" );

anvil.fs.cleanDirectory( pathSpec, [onDeleted] )

Clean up a directory, and provide an optional onDeleted callback to run on deletion.

arguments

  • pathSpec [string] required : Path to directory being cleaned.
  • onDeleted [function]: Optional callback to executed on successful deletion.

anvil.fs.copy( from, to, onComplete )

Copy a folder or file from the from to the to, and fire onComplete when done.

arguments

  • from [string] required : Path to directory or file being copied from.
  • to [string] required: Path to directory or file being copied to.
  • onComplete [function] required: Callback when copying is complete.

anvil.fs.copyDirectory( from, to, onComplete )

Copy a folder from the from to the to, and fire onComplete when done. Actually calls anvil.fs.copyFile for each file in the directory.

arguments

  • from [string] required : Path to directory being copied from.
  • to [string] required: Path to directory being copied to.
  • onComplete [function] required: Callback when copying is complete.

anvil.fs.copyFile( from, to, onComplete )

Copy a file from the from to the to, and fire onComplete when done.

arguments

  • from [string] required : Path to file being copied from.
  • to [string] required: Path to file being copied to.
  • onComplete [function] required: Callback when copying is complete. Passed the to argument originally passed in.

anvil.fs.delete( pathSpec, onDelete )

Delete file or directory defined by pathSpec and fire the onDelete callback when complete. Calls anvil.fs.deleteDirectory or anvil.fs.deleteFile depending on usage.

arguments

  • pathSpec [string] required : Path to file or directory being deleted.
  • onDelete [function] required: Callback when deleting is complete.

anvil.fs.deleteFile( pathSpec, onDelete )

Delete file defined by pathSpec and fire the onDelete callback when complete.

arguments

  • pathSpec [string] required : Path to file being deleted.
  • onDelete [function] required: Callback when deleting is complete. Passed an error argument.

anvil.fs.deleteDirectory( pathSpec, onDelete )

Delete directory defined by pathSpec and fire the onDelete callback when complete.

arguments

  • pathSpec [string] required : Path to directory being deleted.
  • onDelete [function] required: Callback when deleting is complete.

anvil.fs.deleteLink( pathSpec, [onDelete] )

Unlink a path specified by pathSpec fires an optional onDelete callback.

arguments

  • pathSpec [string] required : Path to directory being deleted.
  • onDelete [function]: Callback when deleting is complete. Passed an error argument.

anvil.fs.ensurePath( pathSpec, [onComplete] )

Checks for the existance of the pathSpec, if not there, it will create the directory.

arguments

  • pathSpec [string] required : Path to directory being inspected.
  • onDelete [function]: Callback when path has been created or inspected. Passed an error argument.

anvil.fs.getFiles( pathSpec, workingPath, onFiles, [filters], [limit] )

Recursively searches pathSpec and builds a list a files, and directories which can be filtered by the filters argument and the limited recursion by the limit. The results are passed to onFiles.

arguments

  • pathSpec [string] required : Path to directory being inspected.
  • workingPath [string] required : Path to directory being inspected.
  • onFiles [function] required: Callback when directory has been recursively searched. Passed files and directories as arguments.
  • filters [array]: A list of the file extensions to filter out of the results.
  • limit [number] : Limit the amount of recursion.

example

anvil.fs.getFiles( "src", "src", function( files, directories ) {
    console.log( files, directories );
});

anvil.fs.link( from, to, [done] )

Create a symlink of from to to, and call the done callback

arguments

  • from [string] required : From destination of the symlink.
  • to [string] required : To destination of the symlink.
  • done [function]: Callback when symlink has been created.

anvil.fs.metadata( pathSpec, onStat )

Get metadata on a file from pathSpec.

arguments

  • pathSpec [string] required : File to retrieve metadata of.
  • onStat [function] required: Callback when stats have been retrieved. Passed back an object with the stats.

anvil.fs.pathExists( pathSpec, [callback] )

Check if a the path given by pathSepc exists. Will check sync with no callback, and async with it.

arguments

  • pathSpec [string] required : Folder to check if exists.
  • callback [function]: Optional callback after checking the path.

example

var fileExists = anvil.fs.pathExists( "spec" );

anvil.fs.read( pathSpec, onContent )

Read the contents of a file from pathSpec, and fire onContent with the contents of the file.

arguments

  • pathSpec [string] required : File to read.
  • onContent [function] required: Callback once the file is read. If successful, the content will be passed to the callback, if not successful, an empty string, and an error will be passed.

example

var contents = anvil.fs.read( "src/index.js", function( content, err ) {
    if ( err ) {
        anvil.log.error( "An error has occurred": + err );
    }
    // do something with content
});

anvil.fs.readSync( pathSpec )

Read the contents of a file from pathSpec synchronously, and directly returns it.

arguments

  • pathSpec [string] required : File to read.

example

var contents = anvil.fs.read( "src/index.js" );
// Do something with contents

anvil.fs.transform( from, transform, to, [onComplete] )

Read the contents of a file from from, and pass the contents to a function transform. The transform function will operate on the contents and write the result to to, and fire the onComplete callback.

arguments

  • from [string] required : File to read.
  • transform [function] required: An operation to run that will be passed the content of the to file, and an callback function to execute that has modified, and error arguments.
  • to [string] required : Once the file has been run through the transform function, it will be written here.
  • onComplete [function]: A callback function to be fired after the file has been transformed and written. Passed an error.

example

anvil.fs.transform( "./src/index.js", function( contents, done ) {
    contents = contents.toLowerCase();
    done( contents );
}, "./src/index.lc.js");

anvil.fs.watch( pathSpec, onEvent )

Watches a folder tree pathSpec and fires the onEvent callback when an event occurs.

arguments

  • pathSpec [string] required : Folder tree to watch.
  • onEvent [function] required : A callback to execute when an event occurs. Passed an event object with the details of the event.

anvil.fs.write( pathSpec, content, [onComplete] )

Write a file to pathSpec with content, and fire the onComplete or simply write the file out synchronously.

arguments

  • pathSpec [string] required : File to write.
  • content [string] required : Content to write.
  • onComplete [function] required : An optional callback to execute once the file is written. Passed an error.