Skip to content
Browse files

Added rmdir

  • Loading branch information...
1 parent 2133cde commit 8a54db7278882065c224cb28b763adbbb9983dab @matthewp committed Jan 15, 2013
Showing with 59 additions and 10 deletions.
  1. +11 −9 README.md
  2. +8 −0 filesystem.js
  3. +17 −0 indexeddb.js
  4. +23 −1 test/test.dir.js
View
20 README.md
@@ -38,9 +38,17 @@ Retrieves the file with the name ``fileName`` and calls the ``callback. The firs
Removes the file with the name ``fileName`` from storage and calls the ``callback``. The callback is called even if the file doesn't exist.
-### fs.readdir(directoryName, callback)
+### fs.readdir(fullPath, callback)
-Gets the contents of ``directoryName`` (should be the full path) and calls the ``callback``. The callback will contain an array of ``DirectoryEntry`` objects (see below).
+Gets the contents of ``fullPath`` and calls the ``callback``. The callback will contain an array of ``DirectoryEntry`` objects (see below).
+
+### fs.mkdir(fullPath, callback)
+
+Creates a directory at ``fullPath`` and calls the ``callback``. The only parameter of the callback is an ``Error``, when applicable.
+
+### fs.rmdir(fullPath, callback)
+
+Removes the directory at ``fullPath``, recursively removing any files/subdirectories contained within.
### DirectoryEntry
@@ -64,10 +72,4 @@ The ``type`` of the entry, either **file** or **directory**.
#### readFile(callback)
-A convenience method for calling ``readFile(fileName, callback)``. Throws a TypeError if the entry is not of ``type`` **file**.
-
-## IN PROGRESS
-
-This component is still in very early stages, but plans are to include methods such as:
-
-* readJson - Get a file and return it as an object.
+A convenience method for calling ``readFile(fileName, callback)``. Throws a TypeError if the entry is not of ``type`` **file**.
View
8 filesystem.js
@@ -124,4 +124,12 @@ exports.mkdir = function (path, callback) {
callback(err);
});
});
+};
+
+exports.rmdir = function (path, callback) {
+ init(function (fs) {
+ fs.root.getDirectory(path, {}, function (dirEntry) {
+ dirEntry.remove(callback);
+ });
+ });
};
View
17 indexeddb.js
@@ -159,4 +159,21 @@ exports.mkdir = function (fullPath, callback) {
callback(null);
};
});
+};
+
+exports.rmdir = function (fullPath, callback) {
+ exports.readdir(fullPath, function removeFiles(files) {
+ if (!files || !files.length) {
+ return exports.removeFile(fullPath, callback);
+ }
+
+ var file = files.shift(),
+ func = file.type === 'directory'
+ ? exports.rmdir
+ : exports.removeFile;
+
+ func(file.name, function () {
+ removeFiles(files);
+ });
+ });
};
View
24 test/test.dir.js
@@ -73,7 +73,29 @@ describe('Manipulating directories', function () {
it('Should not return an error', function (done) {
fs.mkdir(dirName, function (err) {
- done(assert(!err));
+ fs.writeFile('dir-remfile1', 'foo bar', function () {
+ fs.writeFile('dir-remfile2', 'baz buz', function () {
+ done();
+ });
+ });
+ });
+ });
+ });
+
+ describe('Removing a directory with files.', function () {
+ var dirName = 'dir-remme';
+
+ beforeEach(function (done) {
+ fs.mkdir(dirName, done);
+ });
+
+ it('Should call the callback.', function () {
+ fs.rmdir(dirName, assert.bind(null, true));
+ });
+
+ it('Should return an empty array for the files', function (done) {
+ fs.readdir(dirName, function (err, files) {
+ done(assert(!files.length));
});
});
});

0 comments on commit 8a54db7

Please sign in to comment.
Something went wrong with that request. Please try again.