cordova-promise-fs
Advanced tools
Comparing version 0.8.0 to 0.9.0
{ | ||
"name": "cordova-promise-fs", | ||
"main": "dist/CordovaPromiseFS.js", | ||
"version": "0.8.0", | ||
"version": "0.9.0", | ||
"homepage": "https://github.com/markmarijnissen/cordova-promise-fs", | ||
@@ -6,0 +6,0 @@ "authors": [ |
@@ -65,16 +65,18 @@ var CordovaPromiseFS = | ||
function dirname(str) { | ||
var parts = str.split('/'); | ||
if(parts.length > 1) { | ||
parts.splice(parts.length-1,1); | ||
return parts.join('/'); | ||
} else { | ||
return ''; | ||
} | ||
str = str.substr(0,str.lastIndexOf('/')+1); | ||
if(str[0] === '/') str = str.substr(1); | ||
return str; | ||
} | ||
function filename(str) { | ||
var parts = str.split('/'); | ||
return parts[parts.length-1]; | ||
return str.substr(str.lastIndexOf('/')+1); | ||
} | ||
function normalize(str){ | ||
if(str[0] === '/') str = str.substr(1); | ||
if(!!str && str.indexOf('.') < 0 && str[str.length-1] !== '/') str += '/'; | ||
if(str === './') str = ''; | ||
return str; | ||
} | ||
var transferQueue = [], // queued fileTransfers | ||
@@ -90,3 +92,3 @@ inprogress = 0; // currently active filetransfers | ||
if(!Promise) { throw new Error("No Promise library given in options.Promise"); } | ||
/* default options */ | ||
@@ -157,8 +159,2 @@ this.options = options = options || {}; | ||
function create(path){ | ||
return ensure(dirname(path)).then(function(){ | ||
return file(path,{create:true}); | ||
}); | ||
} | ||
/* ensure directory exists */ | ||
@@ -180,2 +176,65 @@ function ensure(folders) { | ||
/* get file file */ | ||
function file(path,options){ | ||
path = normalize(path); | ||
options = options || {}; | ||
return new Promise(function(resolve,reject){ | ||
return fs.then(function(fs){ | ||
fs.root.getFile(path,options,resolve,reject); | ||
},reject); | ||
}); | ||
} | ||
/* get directory entry */ | ||
function dir(path,options){ | ||
path = normalize(path); | ||
options = options || {}; | ||
return new Promise(function(resolve,reject){ | ||
return fs.then(function(fs){ | ||
if(!path || path === '/') { | ||
resolve(fs.root); | ||
} else { | ||
fs.root.getDirectory(path,options,resolve,reject); | ||
} | ||
},reject); | ||
}); | ||
} | ||
/* list contents of a directory */ | ||
function list(path,mode) { | ||
mode = mode || ''; | ||
var recursive = mode.indexOf('r') > -1; | ||
var getAsEntries = mode.indexOf('e') > -1; | ||
var onlyFiles = mode.indexOf('f') > -1; | ||
var onlyDirs = mode.indexOf('d') > -1; | ||
if(onlyFiles && onlyDirs) { | ||
onlyFiles = false; | ||
onlyDirs = false; | ||
} | ||
return new Promise(function(resolve,reject){ | ||
return dir(path).then(function(dirEntry){ | ||
var dirReader = dirEntry.createReader(); | ||
dirReader.readEntries(function(entries) { | ||
var promises = [ResolvedPromise(entries)]; | ||
if(recursive) { | ||
entries | ||
.filter(function(entry){return entry.isDirectory; }) | ||
.forEach(function(entry){ | ||
promises.push(list(entry.fullPath,'re')); | ||
}); | ||
} | ||
Promise.all(promises).then(function(values){ | ||
var entries = []; | ||
entries = entries.concat.apply(entries,values); | ||
if(onlyFiles) entries = entries.filter(function(entry) { return entry.isFile; }); | ||
if(onlyDirs) entries = entries.filter(function(entry) { return entry.isDirectory; }); | ||
if(!getAsEntries) entries = entries.map(function(entry) { return entry.fullPath; }); | ||
resolve(entries); | ||
},reject); | ||
}, reject); | ||
},reject); | ||
}); | ||
} | ||
/* does file exist? If so, resolve with fileEntry, if not, resolve with false. */ | ||
@@ -199,2 +258,8 @@ function exists(path){ | ||
function create(path){ | ||
return ensure(dirname(path)).then(function(){ | ||
return file(path,{create:true}); | ||
}); | ||
} | ||
/* convert path to URL to be used in JS/CSS/HTML */ | ||
@@ -212,4 +277,4 @@ function toURL(path) { | ||
toInternalURLSync = function(path){ | ||
if(path[0] !== '/') path = '/' + path; | ||
return 'cdvfile://localhost/'+(options.persistent? 'persistent':'temporary') + path; | ||
path = normalize(path); | ||
return 'cdvfile://localhost/'+(options.persistent? 'persistent/':'temporary/') + path; | ||
}; | ||
@@ -225,4 +290,4 @@ | ||
toInternalURLSync = function(path){ | ||
if(path[0] !== '/') path = '/' + path; | ||
return 'filesystem:'+location.origin+(options.persistent? '/persistent':'/temporary') + path; | ||
path = normalize(path); | ||
return 'filesystem:'+location.origin+(options.persistent? '/persistent/':'/temporary/') + path; | ||
}; | ||
@@ -235,33 +300,4 @@ | ||
}; | ||
} | ||
/* convert path to base64 date URI */ | ||
function toDataURL(path) { | ||
return read(path,'readAsDataURL'); | ||
} | ||
/* get file file */ | ||
function file(path,options){ | ||
options = options || {}; | ||
return new Promise(function(resolve,reject){ | ||
return fs.then(function(fs){ | ||
fs.root.getFile(path,options,resolve,reject); | ||
},reject); | ||
}); | ||
} | ||
/* get directory entry */ | ||
function dir(path,options){ | ||
options = options || {}; | ||
return new Promise(function(resolve,reject){ | ||
return fs.then(function(fs){ | ||
if(!path || path === '/') { | ||
resolve(fs.root); | ||
} else { | ||
fs.root.getDirectory(path,options,resolve,reject); | ||
} | ||
},reject); | ||
}); | ||
} | ||
/* return contents of a file */ | ||
@@ -283,2 +319,8 @@ function read(path,method) { | ||
/* convert path to base64 date URI */ | ||
function toDataURL(path) { | ||
return read(path,'readAsDataURL'); | ||
} | ||
function readJSON(path){ | ||
@@ -357,39 +399,2 @@ return read(path).then(JSON.parse); | ||
/* list contents of a directory */ | ||
function list(path,mode) { | ||
mode = mode || ''; | ||
var recursive = mode.indexOf('r') > -1; | ||
var getAsEntries = mode.indexOf('e') > -1; | ||
var onlyFiles = mode.indexOf('f') > -1; | ||
var onlyDirs = mode.indexOf('d') > -1; | ||
if(onlyFiles && onlyDirs) { | ||
onlyFiles = false; | ||
onlyDirs = false; | ||
} | ||
return new Promise(function(resolve,reject){ | ||
return dir(path).then(function(dirEntry){ | ||
var dirReader = dirEntry.createReader(); | ||
dirReader.readEntries(function(entries) { | ||
var promises = [ResolvedPromise(entries)]; | ||
if(recursive) { | ||
entries | ||
.filter(function(entry){return entry.isDirectory; }) | ||
.forEach(function(entry){ | ||
promises.push(list(entry.fullPath,'re')); | ||
}); | ||
} | ||
Promise.all(promises).then(function(values){ | ||
var entries = []; | ||
entries = entries.concat.apply(entries,values); | ||
if(onlyFiles) entries = entries.filter(function(entry) { return entry.isFile; }); | ||
if(onlyDirs) entries = entries.filter(function(entry) { return entry.isDirectory; }); | ||
if(!getAsEntries) entries = entries.map(function(entry) { return entry.fullPath; }); | ||
resolve(entries); | ||
},reject); | ||
}, reject); | ||
},reject); | ||
}); | ||
} | ||
// Whenever we want to start a transfer, we call popTransferQueue | ||
@@ -401,3 +406,3 @@ function popTransferQueue(){ | ||
inprogress++; | ||
// fetch filetranfer, method-type (isDownload) and arguments | ||
@@ -413,3 +418,3 @@ var args = transferQueue.pop(); | ||
var transferOptions = args.shift(); | ||
if(ft._aborted) { | ||
@@ -428,3 +433,3 @@ inprogress--; | ||
// Promise callback to check if there are any more queued transfers | ||
// Promise callback to check if there are any more queued transfers | ||
function nextTransfer(result){ | ||
@@ -440,3 +445,3 @@ inprogress--; // decrement counter to free up one space to start transfers again! | ||
transferOptions = {}; | ||
} | ||
} | ||
serverUrl = encodeURI(serverUrl); | ||
@@ -450,3 +455,3 @@ if(isCordova) localPath = toInternalURLSync(localPath); | ||
transferOptions.retry = transferOptions.retry.concat(); | ||
var ft = new FileTransfer(); | ||
@@ -496,2 +501,3 @@ onprogress = onprogress || transferOptions.onprogress; | ||
fs: fs, | ||
normalize: normalize, | ||
file: file, | ||
@@ -498,0 +504,0 @@ filename: filename, |
188
index.js
@@ -18,16 +18,18 @@ /** | ||
function dirname(str) { | ||
var parts = str.split('/'); | ||
if(parts.length > 1) { | ||
parts.splice(parts.length-1,1); | ||
return parts.join('/'); | ||
} else { | ||
return ''; | ||
} | ||
str = str.substr(0,str.lastIndexOf('/')+1); | ||
if(str[0] === '/') str = str.substr(1); | ||
return str; | ||
} | ||
function filename(str) { | ||
var parts = str.split('/'); | ||
return parts[parts.length-1]; | ||
return str.substr(str.lastIndexOf('/')+1); | ||
} | ||
function normalize(str){ | ||
if(str[0] === '/') str = str.substr(1); | ||
if(!!str && str.indexOf('.') < 0 && str[str.length-1] !== '/') str += '/'; | ||
if(str === './') str = ''; | ||
return str; | ||
} | ||
var transferQueue = [], // queued fileTransfers | ||
@@ -43,3 +45,3 @@ inprogress = 0; // currently active filetransfers | ||
if(!Promise) { throw new Error("No Promise library given in options.Promise"); } | ||
/* default options */ | ||
@@ -110,8 +112,2 @@ this.options = options = options || {}; | ||
function create(path){ | ||
return ensure(dirname(path)).then(function(){ | ||
return file(path,{create:true}); | ||
}); | ||
} | ||
/* ensure directory exists */ | ||
@@ -133,2 +129,65 @@ function ensure(folders) { | ||
/* get file file */ | ||
function file(path,options){ | ||
path = normalize(path); | ||
options = options || {}; | ||
return new Promise(function(resolve,reject){ | ||
return fs.then(function(fs){ | ||
fs.root.getFile(path,options,resolve,reject); | ||
},reject); | ||
}); | ||
} | ||
/* get directory entry */ | ||
function dir(path,options){ | ||
path = normalize(path); | ||
options = options || {}; | ||
return new Promise(function(resolve,reject){ | ||
return fs.then(function(fs){ | ||
if(!path || path === '/') { | ||
resolve(fs.root); | ||
} else { | ||
fs.root.getDirectory(path,options,resolve,reject); | ||
} | ||
},reject); | ||
}); | ||
} | ||
/* list contents of a directory */ | ||
function list(path,mode) { | ||
mode = mode || ''; | ||
var recursive = mode.indexOf('r') > -1; | ||
var getAsEntries = mode.indexOf('e') > -1; | ||
var onlyFiles = mode.indexOf('f') > -1; | ||
var onlyDirs = mode.indexOf('d') > -1; | ||
if(onlyFiles && onlyDirs) { | ||
onlyFiles = false; | ||
onlyDirs = false; | ||
} | ||
return new Promise(function(resolve,reject){ | ||
return dir(path).then(function(dirEntry){ | ||
var dirReader = dirEntry.createReader(); | ||
dirReader.readEntries(function(entries) { | ||
var promises = [ResolvedPromise(entries)]; | ||
if(recursive) { | ||
entries | ||
.filter(function(entry){return entry.isDirectory; }) | ||
.forEach(function(entry){ | ||
promises.push(list(entry.fullPath,'re')); | ||
}); | ||
} | ||
Promise.all(promises).then(function(values){ | ||
var entries = []; | ||
entries = entries.concat.apply(entries,values); | ||
if(onlyFiles) entries = entries.filter(function(entry) { return entry.isFile; }); | ||
if(onlyDirs) entries = entries.filter(function(entry) { return entry.isDirectory; }); | ||
if(!getAsEntries) entries = entries.map(function(entry) { return entry.fullPath; }); | ||
resolve(entries); | ||
},reject); | ||
}, reject); | ||
},reject); | ||
}); | ||
} | ||
/* does file exist? If so, resolve with fileEntry, if not, resolve with false. */ | ||
@@ -152,2 +211,8 @@ function exists(path){ | ||
function create(path){ | ||
return ensure(dirname(path)).then(function(){ | ||
return file(path,{create:true}); | ||
}); | ||
} | ||
/* convert path to URL to be used in JS/CSS/HTML */ | ||
@@ -165,4 +230,4 @@ function toURL(path) { | ||
toInternalURLSync = function(path){ | ||
if(path[0] !== '/') path = '/' + path; | ||
return 'cdvfile://localhost/'+(options.persistent? 'persistent':'temporary') + path; | ||
path = normalize(path); | ||
return 'cdvfile://localhost/'+(options.persistent? 'persistent/':'temporary/') + path; | ||
}; | ||
@@ -178,4 +243,4 @@ | ||
toInternalURLSync = function(path){ | ||
if(path[0] !== '/') path = '/' + path; | ||
return 'filesystem:'+location.origin+(options.persistent? '/persistent':'/temporary') + path; | ||
path = normalize(path); | ||
return 'filesystem:'+location.origin+(options.persistent? '/persistent/':'/temporary/') + path; | ||
}; | ||
@@ -188,33 +253,4 @@ | ||
}; | ||
} | ||
/* convert path to base64 date URI */ | ||
function toDataURL(path) { | ||
return read(path,'readAsDataURL'); | ||
} | ||
/* get file file */ | ||
function file(path,options){ | ||
options = options || {}; | ||
return new Promise(function(resolve,reject){ | ||
return fs.then(function(fs){ | ||
fs.root.getFile(path,options,resolve,reject); | ||
},reject); | ||
}); | ||
} | ||
/* get directory entry */ | ||
function dir(path,options){ | ||
options = options || {}; | ||
return new Promise(function(resolve,reject){ | ||
return fs.then(function(fs){ | ||
if(!path || path === '/') { | ||
resolve(fs.root); | ||
} else { | ||
fs.root.getDirectory(path,options,resolve,reject); | ||
} | ||
},reject); | ||
}); | ||
} | ||
/* return contents of a file */ | ||
@@ -236,2 +272,8 @@ function read(path,method) { | ||
/* convert path to base64 date URI */ | ||
function toDataURL(path) { | ||
return read(path,'readAsDataURL'); | ||
} | ||
function readJSON(path){ | ||
@@ -310,39 +352,2 @@ return read(path).then(JSON.parse); | ||
/* list contents of a directory */ | ||
function list(path,mode) { | ||
mode = mode || ''; | ||
var recursive = mode.indexOf('r') > -1; | ||
var getAsEntries = mode.indexOf('e') > -1; | ||
var onlyFiles = mode.indexOf('f') > -1; | ||
var onlyDirs = mode.indexOf('d') > -1; | ||
if(onlyFiles && onlyDirs) { | ||
onlyFiles = false; | ||
onlyDirs = false; | ||
} | ||
return new Promise(function(resolve,reject){ | ||
return dir(path).then(function(dirEntry){ | ||
var dirReader = dirEntry.createReader(); | ||
dirReader.readEntries(function(entries) { | ||
var promises = [ResolvedPromise(entries)]; | ||
if(recursive) { | ||
entries | ||
.filter(function(entry){return entry.isDirectory; }) | ||
.forEach(function(entry){ | ||
promises.push(list(entry.fullPath,'re')); | ||
}); | ||
} | ||
Promise.all(promises).then(function(values){ | ||
var entries = []; | ||
entries = entries.concat.apply(entries,values); | ||
if(onlyFiles) entries = entries.filter(function(entry) { return entry.isFile; }); | ||
if(onlyDirs) entries = entries.filter(function(entry) { return entry.isDirectory; }); | ||
if(!getAsEntries) entries = entries.map(function(entry) { return entry.fullPath; }); | ||
resolve(entries); | ||
},reject); | ||
}, reject); | ||
},reject); | ||
}); | ||
} | ||
// Whenever we want to start a transfer, we call popTransferQueue | ||
@@ -354,3 +359,3 @@ function popTransferQueue(){ | ||
inprogress++; | ||
// fetch filetranfer, method-type (isDownload) and arguments | ||
@@ -366,3 +371,3 @@ var args = transferQueue.pop(); | ||
var transferOptions = args.shift(); | ||
if(ft._aborted) { | ||
@@ -381,3 +386,3 @@ inprogress--; | ||
// Promise callback to check if there are any more queued transfers | ||
// Promise callback to check if there are any more queued transfers | ||
function nextTransfer(result){ | ||
@@ -393,3 +398,3 @@ inprogress--; // decrement counter to free up one space to start transfers again! | ||
transferOptions = {}; | ||
} | ||
} | ||
serverUrl = encodeURI(serverUrl); | ||
@@ -403,3 +408,3 @@ if(isCordova) localPath = toInternalURLSync(localPath); | ||
transferOptions.retry = transferOptions.retry.concat(); | ||
var ft = new FileTransfer(); | ||
@@ -449,2 +454,3 @@ onprogress = onprogress || transferOptions.onprogress; | ||
fs: fs, | ||
normalize: normalize, | ||
file: file, | ||
@@ -451,0 +457,0 @@ filename: filename, |
{ | ||
"name": "cordova-promise-fs", | ||
"version": "0.8.0", | ||
"version": "0.9.0", | ||
"description": "Cordova FileSystem convienence functions that return promises.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -115,4 +115,32 @@ cordova-promise-fs | ||
### Normalized path | ||
In CordovaPromiseFS, all filenames and paths are normalized: | ||
* Directories should end with a `/`. | ||
* Filenames and directories should never start with a `/`. | ||
* `"./"` is converted to `""` | ||
This allows you to concatenate normalized paths, i.e. | ||
```javascript | ||
normalize('dir1/dir2') === normalize('dir1') + normalize('dir2') === 'dir1/dir2/'; | ||
``` | ||
If you're storing or saving paths, it is recommended to `normalize` them first to avoid comparison problems. (i.e. paths are not recognized as the same because of a missing trailing slash). | ||
Beware: the original `entry.fullPath` might return a path which starts with a `/`. This causes problems on Android, i.e. | ||
```javascript | ||
var path = filesystem.root.fullPath; // returns something starting with a `/` | ||
filesystem.root.getDirectory(path); // NullPointer error in android. Stupid! | ||
``` | ||
This problem is solved in CordovaPromiseFS. | ||
## Changelog | ||
### 0.9.0 (28/11/2014) | ||
* Normalize path everywhere. | ||
### 0.8.0 (27/11/2014) | ||
@@ -119,0 +147,0 @@ |
@@ -1,322 +0,341 @@ | ||
QUnit.config.reorder = false; | ||
(function(){ | ||
QUnit.config.reorder = false; | ||
/*************************************/ | ||
QUnit.module('Init & Utils'); | ||
/*************************************/ | ||
var fs = null; | ||
/*************************************/ | ||
QUnit.module('CordovaPromiseFS'); | ||
/*************************************/ | ||
window.fs = null; | ||
QUnit.test('fs = CordovaPromiseFS()',function(assert){ | ||
fs = CordovaPromiseFS({ | ||
Promise:Promise, | ||
persistent: typeof cordova !== 'undefined' | ||
QUnit.test('fs = CordovaPromiseFS()',function(assert){ | ||
fs = CordovaPromiseFS({ | ||
Promise:Promise, | ||
persistent: typeof cordova !== 'undefined' | ||
}); | ||
assert.ok(!!fs); | ||
}); | ||
assert.ok(!!fs); | ||
}); | ||
QUnit.asyncTest('deviceready',function(assert){ | ||
fs.deviceready.then(ok(assert,true),err(assert)); | ||
}); | ||
QUnit.asyncTest('fs.deviceready',function(assert){ | ||
fs.deviceready.then(ok(assert,true),err(assert)); | ||
}); | ||
QUnit.test('filename("bla/bla/test.txt") = "test.txt"',function(assert){ | ||
assert.equal(fs.filename('bla/bla/test.txt'),'test.txt'); | ||
}); | ||
QUnit.test('fs.filename("bla/bla/test.txt") = "test.txt"',function(assert){ | ||
assert.equal(fs.filename('bla/bla/test.txt'),'test.txt'); | ||
}); | ||
QUnit.test('dirname("bla/bla/test.txt") = "bla/bla"',function(assert){ | ||
assert.equal(fs.dirname('bla/bla/test.txt'),'bla/bla'); | ||
}); | ||
QUnit.test('fs.dirname("bla/bla/test.txt") = "bla/bla/"',function(assert){ | ||
assert.equal(fs.dirname('bla/bla/test.txt'),'bla/bla/'); | ||
}); | ||
/*************************************/ | ||
QUnit.module('Create'); | ||
/*************************************/ | ||
QUnit.asyncTest('fs.create (in root)',function(assert){ | ||
fs.create('create.txt') | ||
.then(function(){ | ||
return fs.file('create.txt'); | ||
}) | ||
.then(ok(assert,truthy),err(assert)); | ||
}); | ||
QUnit.test('fs.normalize: "/dir/dir/file.txt => dir/file.txt; /dir => dir/; ./ => ""',function(assert){ | ||
assert.equal(fs.normalize('./'), '', '"./" => ""'); | ||
assert.equal(fs.normalize('test'), 'test/', 'test => test/'); | ||
assert.equal(fs.normalize('test/'), 'test/', 'test/ => test/'); | ||
assert.equal(fs.normalize('/test'), 'test/', '/test => test/'); | ||
assert.equal(fs.normalize('/test/'), 'test/', '/test/ => test/'); | ||
assert.equal(fs.normalize('/dir.txt/'),'dir.txt/', '/dir.txt/ => dir.txt/ (directory with a dot)'); | ||
assert.equal(fs.normalize('file.txt'), 'file.txt', 'file.txt => file.txt'); | ||
assert.equal(fs.normalize('/file.txt'), 'file.txt', '/file.txt => file.txt'); | ||
assert.equal(fs.normalize('/dir/sub/sub/text.txt'), 'dir/sub/sub/text.txt', 'subdirectories with file'); | ||
assert.equal(fs.normalize('/dir/sub/sub/sub'), 'dir/sub/sub/sub/', 'subdirectories'); | ||
}); | ||
QUnit.asyncTest('fs.create (in subdirectory)',function(assert){ | ||
fs.create('sub/sub/create.txt') | ||
.then(function(){ | ||
return fs.file('sub/sub/create.txt'); | ||
}) | ||
.then(ok(assert,truthy),err(assert)); | ||
}); | ||
QUnit.asyncTest('fs.ensure (in root)',function(assert){ | ||
fs.ensure('testdir') | ||
.then(function(){ | ||
return fs.dir('testdir'); | ||
}) | ||
.then(ok(assert,truthy),err(assert)); | ||
}); | ||
/*************************************/ | ||
// CREATE | ||
/*************************************/ | ||
QUnit.asyncTest('fs.create (in root)',function(assert){ | ||
fs.create('create.txt') | ||
.then(function(){ | ||
return fs.file('create.txt'); | ||
}) | ||
.then(ok(assert,truthy),err(assert)); | ||
}); | ||
QUnit.asyncTest('fs.ensure (in subdirectory)',function(assert){ | ||
fs.ensure('testdir2/testdir2') | ||
.then(function(){ | ||
return fs.dir('testdir2/testdir2'); | ||
}) | ||
.then(ok(assert,truthy),err(assert)); | ||
}); | ||
QUnit.asyncTest('fs.create (in subdirectory)',function(assert){ | ||
fs.create('sub/sub/create.txt') | ||
.then(function(){ | ||
return fs.file('sub/sub/create.txt'); | ||
}) | ||
.then(ok(assert,truthy),err(assert)); | ||
}); | ||
/*************************************/ | ||
QUnit.module('Entry'); | ||
/*************************************/ | ||
QUnit.asyncTest('fs.file (ok - fileEntry)',function(assert){ | ||
fs.file('create.txt').then(ok(assert,truthy),err(assert)); | ||
}); | ||
QUnit.asyncTest('fs.ensure (in root)',function(assert){ | ||
fs.ensure('testdir') | ||
.then(function(){ | ||
return fs.dir('testdir'); | ||
}) | ||
.then(ok(assert,truthy),err(assert)); | ||
}); | ||
QUnit.asyncTest('fs.file (error - does not exist)',function(assert){ | ||
fs.file('does-not-exist.txt').then(err(assert),function(err){ | ||
assert.equal(err.code,1); | ||
QUnit.start(); | ||
QUnit.asyncTest('fs.ensure (in subdirectory)',function(assert){ | ||
fs.ensure('testdir2/testdir2') | ||
.then(function(){ | ||
return fs.dir('testdir2/testdir2'); | ||
}) | ||
.then(ok(assert,truthy),err(assert)); | ||
}); | ||
}); | ||
QUnit.asyncTest('fs.exists (ok - true)',function(assert){ | ||
fs.exists('create.txt').then(function(fileEntry){ | ||
assert.ok(fileEntry); | ||
QUnit.start(); | ||
},err(assert)); | ||
}); | ||
/*************************************/ | ||
// ENTRY | ||
/*************************************/ | ||
QUnit.asyncTest('fs.file (ok - fileEntry)',function(assert){ | ||
fs.file('create.txt').then(ok(assert,truthy),err(assert)); | ||
}); | ||
QUnit.asyncTest('fs.exists (error - does not exist)',function(assert){ | ||
fs.exists('does-not-exist.txt').then(ok(assert,false),err(assert)); | ||
}); | ||
QUnit.asyncTest('fs.file (error - does not exist)',function(assert){ | ||
fs.file('does-not-exist.txt').then(err(assert),function(err){ | ||
assert.equal(err.code,1); | ||
QUnit.start(); | ||
}); | ||
}); | ||
QUnit.asyncTest('fs.dir (ok - dirEntry)',function(assert){ | ||
fs.dir('testdir').then(ok(assert,truthy),err(assert)); | ||
}); | ||
QUnit.asyncTest('fs.exists (ok - true)',function(assert){ | ||
fs.exists('create.txt').then(function(fileEntry){ | ||
assert.ok(fileEntry); | ||
QUnit.start(); | ||
},err(assert)); | ||
}); | ||
QUnit.asyncTest('fs.dir (error - does not exist)',function(assert){ | ||
fs.dir('does-not-exist').then(err(assert),function(err){ | ||
assert.equal(err.code,1); | ||
QUnit.start(); | ||
QUnit.asyncTest('fs.exists (error - does not exist)',function(assert){ | ||
fs.exists('does-not-exist.txt').then(ok(assert,false),err(assert)); | ||
}); | ||
}); | ||
QUnit.asyncTest('fs.toInternalURL',function(assert){ | ||
var result = fs.toInternalURLSync('create.txt'); | ||
fs.toInternalURL('create.txt').then(ok(assert,result),err(assert)); | ||
}); | ||
QUnit.asyncTest('fs.dir (ok - dirEntry)',function(assert){ | ||
fs.dir('testdir').then(ok(assert,truthy),err(assert)); | ||
}); | ||
QUnit.asyncTest('fs.toURL',function(assert){ | ||
fs.toURL('create.txt').then(function(url){ | ||
assert.equal(url.substr(0,4),'file'); | ||
QUnit.start(); | ||
},err(assert)); | ||
}); | ||
QUnit.asyncTest('fs.dir (error - does not exist)',function(assert){ | ||
fs.dir('does-not-exist').then(err(assert),function(err){ | ||
assert.equal(err.code,1); | ||
QUnit.start(); | ||
}); | ||
}); | ||
/*************************************/ | ||
QUnit.module('List'); | ||
/*************************************/ | ||
QUnit.asyncTest('list (f)',function(assert){ | ||
var files = ['list/1.txt','list/2.txt','list/3.txt', | ||
'list/sub/4.txt','list/sub/sub/5.txt']; | ||
var promises = files.map(function(file){ | ||
return fs.create(file); | ||
QUnit.asyncTest('fs.toInternalURL',function(assert){ | ||
var result = fs.toInternalURLSync('create.txt'); | ||
fs.toInternalURL('create.txt').then(ok(assert,result),err(assert)); | ||
}); | ||
Promise.all(promises) | ||
.then(function(){ | ||
return fs.list('list','f'); | ||
}) | ||
QUnit.asyncTest('fs.toURL',function(assert){ | ||
fs.toURL('create.txt').then(function(url){ | ||
assert.equal(url.substr(0,4),'file'); | ||
QUnit.start(); | ||
},err(assert)); | ||
}); | ||
/*************************************/ | ||
// LIST | ||
/*************************************/ | ||
QUnit.asyncTest('fs.list (f)',function(assert){ | ||
var files = ['list/1.txt','list/2.txt','list/3.txt', | ||
'list/sub/4.txt','list/sub/sub/5.txt']; | ||
var promises = files.map(function(file){ | ||
return fs.create(file); | ||
}); | ||
Promise.all(promises) | ||
.then(function(){ | ||
return fs.list('list','f'); | ||
}) | ||
.then(function(actual){ | ||
assert.deepEqual(actual,[ | ||
"/list/3.txt", | ||
"/list/2.txt", | ||
"/list/1.txt" | ||
]); | ||
QUnit.start(); | ||
},err(assert)); | ||
}); | ||
QUnit.asyncTest('fs.list (d)',function(assert){ | ||
fs.list('list','d') | ||
.then(function(actual){ | ||
assert.deepEqual(actual,[ | ||
"/list/3.txt", | ||
"/list/2.txt", | ||
"/list/1.txt" | ||
"/list/sub" | ||
]); | ||
QUnit.start(); | ||
},err(assert)); | ||
}); | ||
}); | ||
QUnit.asyncTest('list (d)',function(assert){ | ||
fs.list('list','d') | ||
.then(function(actual){ | ||
assert.deepEqual(actual,[ | ||
"/list/sub" | ||
]); | ||
QUnit.start(); | ||
},err(assert)); | ||
}); | ||
QUnit.asyncTest('fs.list (rf)',function(assert){ | ||
fs.list('list','rf') | ||
.then(function(actual){ | ||
assert.deepEqual(actual,[ | ||
"/list/3.txt", | ||
"/list/2.txt", | ||
"/list/1.txt", | ||
"/list/sub/4.txt", | ||
"/list/sub/sub/5.txt", | ||
]); | ||
QUnit.start(); | ||
},err(assert)); | ||
}); | ||
QUnit.asyncTest('list (rf)',function(assert){ | ||
fs.list('list','rf') | ||
.then(function(actual){ | ||
assert.deepEqual(actual,[ | ||
"/list/3.txt", | ||
"/list/2.txt", | ||
"/list/1.txt", | ||
"/list/sub/4.txt", | ||
"/list/sub/sub/5.txt", | ||
]); | ||
QUnit.start(); | ||
},err(assert)); | ||
}); | ||
QUnit.asyncTest('fs.list (rd)',function(assert){ | ||
fs.list('list','rd') | ||
.then(function(actual){ | ||
assert.deepEqual(actual,[ | ||
"/list/sub", | ||
"/list/sub/sub", | ||
]); | ||
QUnit.start(); | ||
},err(assert)); | ||
}); | ||
QUnit.asyncTest('list (rd)',function(assert){ | ||
fs.list('list','rd') | ||
.then(function(actual){ | ||
assert.deepEqual(actual,[ | ||
"/list/sub", | ||
"/list/sub/sub", | ||
]); | ||
QUnit.start(); | ||
},err(assert)); | ||
}); | ||
QUnit.asyncTest('fs.list (fe)',function(assert){ | ||
fs.list('list','fe') | ||
.then(function(actual){ | ||
assert.equal(actual.length,3); | ||
assert.equal(typeof actual[0],'object'); | ||
QUnit.start(); | ||
},err(assert)); | ||
}); | ||
QUnit.asyncTest('list (fe)',function(assert){ | ||
fs.list('list','fe') | ||
.then(function(actual){ | ||
assert.equal(actual.length,3); | ||
assert.equal(typeof actual[0],'object'); | ||
QUnit.start(); | ||
},err(assert)); | ||
}); | ||
QUnit.asyncTest('fs.list (error when not exist)',function(assert){ | ||
fs.list('does-not-exist','r') | ||
.then(err(assert),function(err){ | ||
assert.equal(err.code,1); | ||
QUnit.start(); | ||
}); | ||
}); | ||
QUnit.asyncTest('list (error when not exist)',function(assert){ | ||
fs.list('does-not-exist','r') | ||
.then(err(assert),function(err){ | ||
assert.equal(err.code,1); | ||
QUnit.start(); | ||
/*************************************/ | ||
// WRITE | ||
/*************************************/ | ||
var obj = {hello:'world'}; | ||
var string = JSON.stringify(obj); | ||
QUnit.asyncTest('fs.write',function(assert){ | ||
fs.write('write.txt',string).then(ok(assert,truthy),err(assert)); | ||
}); | ||
}); | ||
/*************************************/ | ||
QUnit.module('Write'); | ||
/*************************************/ | ||
var obj = {hello:'world'}; | ||
var string = JSON.stringify(obj); | ||
/*************************************/ | ||
// READ | ||
/*************************************/ | ||
QUnit.asyncTest('fs.read',function(assert){ | ||
fs.read('write.txt').then(ok(assert,string),err(assert)); | ||
}); | ||
QUnit.asyncTest('fs.write',function(assert){ | ||
fs.write('write.txt',string).then(ok(assert,truthy),err(assert)); | ||
}); | ||
QUnit.asyncTest('fs.readJSON',function(assert){ | ||
fs.readJSON('write.txt').then(function(actual){ | ||
assert.ok(actual); | ||
assert.equal(actual.hello,obj.hello); | ||
QUnit.start(); | ||
},err(assert)); | ||
}); | ||
/*************************************/ | ||
QUnit.module('Read'); | ||
/*************************************/ | ||
QUnit.asyncTest('fs.read',function(assert){ | ||
fs.read('write.txt').then(ok(assert,string),err(assert)); | ||
}); | ||
QUnit.asyncTest('fs.toDataURL (plain text)',function(assert){ | ||
fs.toDataURL('write.txt').then(ok(assert,'data:text/plain;base64,eyJoZWxsbyI6IndvcmxkIn0='),err(assert)); | ||
}); | ||
QUnit.asyncTest('fs.readJSON',function(assert){ | ||
fs.readJSON('write.txt').then(function(actual){ | ||
assert.ok(actual); | ||
assert.equal(actual.hello,obj.hello); | ||
QUnit.start(); | ||
},err(assert)); | ||
}); | ||
QUnit.asyncTest('fs.toDataURL (plain text)',function(assert){ | ||
fs.toDataURL('write.txt').then(ok(assert,'data:text/plain;base64,eyJoZWxsbyI6IndvcmxkIn0='),err(assert)); | ||
}); | ||
// QUnit.asyncTest('fs.toDataURL (json)',function(assert){ | ||
// fs.write('write.json',obj).then(function(){ | ||
// return fs.toDataURL('write.json'); | ||
// }) | ||
// .then( | ||
// ok(assert,'data:application/json;base64,eyJoZWxsbyI6IndvcmxkIn0='), | ||
// err(assert) | ||
// ); | ||
// }); | ||
/*************************************/ | ||
// REMOVE | ||
/*************************************/ | ||
QUnit.asyncTest('fs.remove = true (when file exists)',function(assert){ | ||
fs.remove('create.txt').then(ok(assert,true),err(assert)); | ||
}); | ||
// QUnit.asyncTest('fs.toDataURL (json)',function(assert){ | ||
// fs.write('write.json',obj).then(function(){ | ||
// return fs.toDataURL('write.json'); | ||
// }) | ||
// .then( | ||
// ok(assert,'data:application/json;base64,eyJoZWxsbyI6IndvcmxkIn0='), | ||
// err(assert) | ||
// ); | ||
// }); | ||
QUnit.asyncTest('fs.remove = false (when file does not exists)',function(assert){ | ||
fs.remove('create.txt').then(ok(assert,false),err(assert)); | ||
}); | ||
/*************************************/ | ||
QUnit.module('Remove'); | ||
/*************************************/ | ||
QUnit.asyncTest('fs.remove = true (when file exists)',function(assert){ | ||
fs.remove('create.txt').then(ok(assert,true),err(assert)); | ||
}); | ||
/*************************************/ | ||
// MOVE AND COPY | ||
/*************************************/ | ||
QUnit.asyncTest('fs.move',function(assert){ | ||
fs.move('write.txt','moved.txt') | ||
.then(function(){ | ||
return fs.exists('write.txt'); | ||
}) | ||
.then(function(val){ | ||
assert.equal(val,false,'old file should not exist'); | ||
}) | ||
.then(function(){ | ||
return fs.exists('moved.txt'); | ||
}) | ||
.then(ok(assert,truthy),err(assert)); | ||
}); | ||
QUnit.asyncTest('fs.remove = false (when file does not exists)',function(assert){ | ||
fs.remove('create.txt').then(ok(assert,false),err(assert)); | ||
}); | ||
QUnit.asyncTest('fs.copy',function(assert){ | ||
fs.copy('moved.txt','copied.txt') | ||
.then(function(){ | ||
return fs.exists('moved.txt'); | ||
}) | ||
.then(function(val){ | ||
assert.ok(val,'old file should exist'); | ||
}) | ||
.then(function(){ | ||
return fs.exists('copied.txt'); | ||
}) | ||
.then(ok(assert,truthy),err(assert)); | ||
}); | ||
/*************************************/ | ||
QUnit.module('Move and Copy'); | ||
/*************************************/ | ||
QUnit.asyncTest('fs.move',function(assert){ | ||
fs.move('write.txt','moved.txt') | ||
.then(function(){ | ||
return fs.exists('write.txt'); | ||
}) | ||
.then(function(val){ | ||
assert.equal(val,false,'old file should not exist'); | ||
}) | ||
.then(function(){ | ||
return fs.exists('moved.txt'); | ||
}) | ||
.then(ok(assert,truthy),err(assert)); | ||
}); | ||
/*************************************/ | ||
// DOWNLOAD | ||
/*************************************/ | ||
QUnit.asyncTest('fs.download (ok)',function(assert){ | ||
fs.download('http://data.madebymark.nl/cordova-promise-fs/download.txt','download.txt') | ||
.then(function(){ | ||
return fs.read('download.txt'); | ||
}) | ||
.then(function(val){ | ||
assert.equal(val,'hello world'); | ||
QUnit.start(); | ||
},err(assert)); | ||
}); | ||
QUnit.asyncTest('fs.copy',function(assert){ | ||
fs.copy('moved.txt','copied.txt') | ||
.then(function(){ | ||
return fs.exists('moved.txt'); | ||
}) | ||
.then(function(val){ | ||
assert.ok(val,'old file should exist'); | ||
}) | ||
.then(function(){ | ||
return fs.exists('copied.txt'); | ||
}) | ||
.then(ok(assert,truthy),err(assert)); | ||
}); | ||
QUnit.asyncTest('fs.download (404 returns false - should see 3 retry attempts)',function(assert){ | ||
fs.download('http://data.madebymark.nl/cordova-promise-fs/does-not-exist.txt','download-error.txt',{retry:[0,0]}) | ||
.then(err(assert),function(){ | ||
assert.ok(true); | ||
QUnit.start(); | ||
}); | ||
}); | ||
/*************************************/ | ||
QUnit.module('Download'); | ||
/*************************************/ | ||
QUnit.asyncTest('fs.download (ok)',function(assert){ | ||
fs.download('http://data.madebymark.nl/cordova-promise-fs/download.txt','download.txt') | ||
.then(function(){ | ||
return fs.read('download.txt'); | ||
}) | ||
.then(function(val){ | ||
assert.equal(val,'hello world'); | ||
QUnit.asyncTest('fs.download : progress when starting download',function(assert){ | ||
var called = false; | ||
fs.download('http://data.madebymark.nl/cordova-promise-fs/download.txt', | ||
'download-error.txt', | ||
function(){ | ||
called = true; | ||
}) | ||
.then(function(){ | ||
assert.ok(called,'should call "onprogress"'); | ||
QUnit.start(); | ||
},err(assert)); | ||
}); | ||
/*************************************/ | ||
// Helper methods for promises | ||
var truthy = 'TRUTHY'; | ||
function print(){ | ||
console.log(arguments); | ||
} | ||
function ok(assert,expected){ | ||
return function(actual){ | ||
if(expected === truthy){ | ||
assert.ok(actual); | ||
} else { | ||
assert.equal(actual,expected); | ||
} | ||
QUnit.start(); | ||
},err(assert)); | ||
}); | ||
}; | ||
} | ||
QUnit.asyncTest('fs.download (404 returns false - should see 3 retry attempts)',function(assert){ | ||
fs.download('http://data.madebymark.nl/cordova-promise-fs/does-not-exist.txt','download-error.txt',{retry:[0,0]}) | ||
.then(err(assert),ok(assert,false)); | ||
}); | ||
QUnit.asyncTest('fs.download : progress when starting download',function(assert){ | ||
var called = false; | ||
fs.download('http://data.madebymark.nl/cordova-promise-fs/download.txt', | ||
'download-error.txt', | ||
function(){ | ||
called = true; | ||
}) | ||
.then(function(){ | ||
assert.ok(called,'should call "onprogress"'); | ||
function err(assert){ | ||
return function(err){ | ||
assert.equal(err,'an error'); | ||
QUnit.start(); | ||
},err(assert)); | ||
}); | ||
/*************************************/ | ||
// Helper methods for promises | ||
var truthy = 'TRUTHY'; | ||
function print(){ | ||
console.log(arguments); | ||
} | ||
function ok(assert,expected){ | ||
return function(actual){ | ||
if(expected === truthy){ | ||
assert.ok(actual); | ||
} else { | ||
assert.equal(actual,expected); | ||
} | ||
QUnit.start(); | ||
}; | ||
} | ||
function err(assert){ | ||
return function(err){ | ||
assert.equal(err,'an error'); | ||
QUnit.start(); | ||
}; | ||
} | ||
}; | ||
} | ||
})(); |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
120925
3630
197
0