Skip to content
an async method-chaining wrapper library for the HTML5 File System API
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


cd.js v0.2 - a comfortable async method chaining wrapper library for the HTML5 File System API

(c) 2012 Zoran Obradović, GPL 3.0 applies.


to write a file:

  .write('myText.txt','My foobar is great.');

to read a file and then write to another file:

  .read('bar.txt', function(done,res) { 
    .write('report.txt', 'bar.txt contains '+res);

to list a directory:

  .for('*',function(done,file) { 

to do lots of stuff, all in the expected order:

  var store = {}, total = 0, report = '';

  .echo('backup done')
  .echo('creating report')
  .for('*.lst', function(done,file) {, function (done,res) {
      var numberOfAs = res.replace(/[^aA]/g,'').length;
      store[] = numberOfAs;
      total += numberOfAs;
  .echo('writing report')
  .then(function() {
    var report = 'number of As in /foo/baz/:\n';
    for (var i in store) report+= i+': '+store[i] + '\n';
    report += "total: '+total;
    this.write('/var/report.txt', report);
  .echo('all done');

CD Object interface:



Start a new fs method queue, change the current directory to path.


Execute the function when the file system becomes available.



The current directory



Change the current directory


cd ..


cd /


Output args... for debugging purposes. uses console.log by default, can be overriden at CD.echo.


Echo the current directory.


Define an error handler.



Add a callback to the queue, with an optional argument.



Call cbFound for each matching file entry, or cbNotFound if no match found.


Like .for, but includes metadata in arg.metadata. For now, this is only includes .modificationTime. (TODO: add size(?) and URL).


Like .for, but allows separate callbacks for files and directories.


Like .for, but only include files.


Like .for, but only include directories.



Remove matching files


Move matching entries to the destination directory, or move a single entry to the destination path, or throw an error if neither is possible.


Copy matching entries to the destination directory, or copy a single entry to the destination path, or throw an error.


Remove matching files and recursively remove matching directories.


Recursively create a directory.



Read content of matching files.


Write a file. content can be a string or, a blob.

TODO: Allow globs. Accept entry, file, image, callback

TODO .append(glob,content)

Append to matching files. content can be a string, a blob, entry, file, image, callback.

TODO .truncate(glob,offset,content)

Truncate matching files, and append optional content. offset can be a number or a callback. content can be a string, a blob, file, image, callback.

TODO .writeAt(glob,offset,content)

offset can be a number or a callback. content can be a string, a blob, file, image, callback.


(must be defined before including cd.js)

cdOptions = {
  required: size in megabytes,
  dont: if true, the file system won't be requested automatically, it can be done later by calling CD.init()



function cb (done,arg) {...}


If the callback returns true, then it is responsible for advancing the queue by calling done().

TODO: if it returns false, the error handler is called and the queue cancelled (or not, see error handling)


Additional argument, if supplied. In .for() loops, this will be the Entry object, with the following added properties:

  • .list - the array of all matched file entries
  • .index - the index of the current file entry
  • .single - whether this file was the only one found

TODO: allow multiple arguments

The callback's context (i.e. this) will be the same CD object. any methods called on it will be placed in the queue and executed after the callback exits exits. if the callback needs to make any async calls, it can return true and call done() manually when the async calls are complete.

If the callback is a non-truthy value, it is ignored.

TODO: If the callback is not a function, an error is thrown.



  • an absolute or a relative path from the current directory, possibly including wildcards (* and ?) in the last part (after the last slash), OR
  • an entry object, OR
  • an array of entry objects, OR
  • TODO an array of paths

Will throw an error if the directory (the path before the last slash) does not exist, or if the glob is misformed.

Something went wrong with that request. Please try again.