cordova-plugin-file-node-like
A wrapper for cordova-plugin-file that provides a Node.js-like API.
Tested on iOS and Android.
Why?
- Because the API provided by cordova-plugin-file is (mainly) the API that was meant to become the HTML5 File API; compared to the Node.js fs module, it is awful!
- Because a rudimentary Node.js-like file API might suffice in most use cases in a Cordova app
Setup
cordova plugin add cordova-plugin-file-node-like
Getting started
Once the deviceready
event has been triggered, here is how to setup this module
//initialize this module, for the application data directory and the cache directorywindowpluginsnodefs;
Available file systems
When you call window.plugins.nodefs.init
, it pre-loads/resolve multiple file systems available in the standard cordova-plugin-file
. Once they are resolved, they are renamed and attached to an object (window
, by default)
See the available storage directories, their layouts and their read/write capabilities in cordova-plugin-file
. Here is how they are renamed:
cordova.file.* name | Node-like FS name |
---|---|
cordova.file.applicationDirectory | \_appFs |
cordova.file.applicationStorageDirectory | \_appStorageFs |
cordova.file.dataDirectory | \_fs |
cordova.file.cacheDirectory | \_cacheFs |
cordova.file.externalApplicationStorageDirectory | \_externalAppStorageFs |
cordova.file.externalDataDirectory | \_externalFs |
cordova.file.externalCacheDirectory | \_externalCacheFs |
cordova.file.externalRootDirectory | \_externalRootFs |
cordova.file.tempDirectory | \_tempFs |
cordova.file.syncedDataDirectory | \_syncedFs |
cordova.file.documentsDirectory | \_documentsFs |
cordova.file.sharedDirectory | \_sharedFs |
NOTE: Not all of the listed file systems are available on every platform. Availability is described in the cordova-plugin-file
documentation.
API
window.plugins.nodefs.init(callback, [hostObject])
: initializes and resolves file systems.
- Function callback(err) : a callback function, receiving error
err
if one occurred - Object hostObject : optional. An object to which the resolved file systems will be attached. Defaults to
window
. (This is where thewindow._fs
andwindow._cacheFs
in the example come from)
window.plugins.node(fsReference)
: initializes a new file system wrapper, for the file system referred to by fsReference
(like _fs
, cacheFs
, _syncedFs
, and others, as described above)
- Returns an FS wrapper object, whose methods are documented below
The FS wrapper object
NOTE: As of now, methods involving blobs/buffers are known not to work on Windows platforms. Please keep that in mind...
fs.writeFile(path, data, cb)
: Write data at the given path. Overwrites the file if it already exists.
- String path : the path at which the file will be written. Note that parent folders must exist before making this call
- String|Uint8Array data : the data to be stored
- Function cb(err) : a callback function, receiving error
err
if one occurred
fs.appendFile(path, data, cb)
: Append data to the file at the given file. Creates the file if it doesn't already exist
- String path : the path to which the data will be appended
- String|Uint8Array data : the data to be stored
- Function cb(err) : a callback function, receiving error
err
if one occurred
fs.readFile(path, [encoding], cb)
: Read data at the given path
- String path : the path of the file to be read
- String encoding : optional string. Defaults to 'buffer'. Accepted values: 'buffer' to get a Uint8Array, 'utf8' to get a string
- Function cb(err, data) : a callback function, receiving the read data (in
data
). receiving errorerr
if one occurred
fs.exists(path, cb)
: Check whether a file or directory exists at the given
- String path : the path to be tested
- Function(exists) : a callback function, receiving the boolean
exists
, whose value indicate whether there is a file or directory at the given path
fs.stat(path, cb)
: Get info about the file/directory at the given path (currently limited to whether it's a file or a directory)
- String path : the path to be tested
- Function cb(err, stat) : a callback function, receiving the resulting Stat object. Receives error
err
, if one occurred
fs.mkdir(path, cb)
: Create a directory, given that its parent directories exist
- String path : path of the directory to be created
- Function cb(err) : a callback function. Receives error
err
if one occurred
fs.mkdirp(path, cb)
: Create a directory, with its parent directories if they do not exist. Purpose : replace the mkdirp
package
- String path : path of the directory to be created
- Function cb(err) : a callback function. Receives error
err
if one occurred
NOTE: fs.mkdirp
is not a standard function in the fs
module of Node.js
fs.readdir(path, cb)
: List the contents of a directory
- String path : path of the directory whose contents should be listed. To list the contents of the root folder, use
'.'
as path - Function cb(err, contents) : a callback function, receiving the array
contents
, listing the files and directories in the requested directory. Receives errorerr
if one occurred
fs.unlink(path, cb)
: Delete a file
- String path : path of the file to be deleted
- Function cb(err) : a callback function, receiving error
err
if one occurred
fs.rmdir(path, cb)
: Delete a directory, if it is empty
- String path : path of the directory to be deleted
- Function cb(err) : a callback function, receiving error
err
if one occurred
fs.rmdirr(path, cb)
: Delete a directory and its contents : replace the rmdir
package
- String path : path of the directory to be deleted
- Function cb(err) : a callback function, receiving error
err
if one occurred
NOTE: fs.rmdirr
is not a standard function in the fs
module of Node.js
Stat
object
The The object resulting from a fs.stat
call
As of now, the only attributes implemented in the stat object are:
Boolean isDirectory
Boolean isFile
Many attributes are missing compared to the fs
module of Node.js
NOTE: These attributes are boolean variables, instead of being functions.
Testing
- Create a Cordova/Phonegap application
- Add the iOS and/or the Android platforms
- Add the testing framework and bind its page as the main page of the app
- Add this plugin
- Add this plugin's test cases, by adding the plugin located in the
tests
folder
cordova plugin add https://github.com/LockateMe/cordova-plugin-file-node-like.git#:/tests
License
This module is released under the terms of the MIT license.