@cardsjd/storage 0.0.6 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
Binary file
@@ -0,0 +1,81 @@
1
+ //this file is used in cordova environment only
2
+ export default class FileManager {
3
+ deviceready() {
4
+ return new Promise(function (resolve, reject) {
5
+ document.addEventListener("deviceready", resolve, false);
6
+ setTimeout(function () { reject(new Error('deviceready has not fired after 5 seconds.')); }, 5100);
7
+ });
8
+ };
9
+ writeFile(fileName, data) {
10
+ return new Promise(function (resolve, reject) {
11
+ if (typeof cordova === 'undefined' || typeof cordova.file === 'undefined') {
12
+ reject('cordova.file not ready, cannot save to local file');
13
+ return;
14
+ }
15
+ var folder = cordova.file.dataDirectory;
16
+ //deviceready-------- no fired yet
17
+ if (!folder) {
18
+ reject('cordova.file.dataDirectory not found, cannot save to local file.');
19
+ return;
20
+ }
21
+ window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function (directoryEntry) {
22
+ directoryEntry.getFile(fileName, { create: true }, function (fileEntry) {
23
+ fileEntry.createWriter(function (fileWriter) {
24
+ fileWriter.onwriteend = function (e) {
25
+ // for real-world usage, you might consider passing a success callback
26
+ resolve('Write file "' + fileName + '" completed.');
27
+ };
28
+
29
+ fileWriter.onerror = function (e) {
30
+ // you could hook this up with our global error handler, or pass in an error callback
31
+ reject('Write failed: ' + e.toString());
32
+ };
33
+
34
+ var blob = new Blob([data], { type: 'text/plain' });
35
+ fileWriter.write(blob);
36
+ }, function (e) { reject(e) });
37
+ }, function (e) { reject(e) });
38
+ }, function (e) { reject(e) });
39
+ });
40
+ };
41
+
42
+ errorHandler(filename) {
43
+ };
44
+
45
+ readFile(fileName) {
46
+ return new Promise(function (resolve, reject) {
47
+ if (typeof cordova === 'undefined') {
48
+ reject('cordova not found');
49
+ return;
50
+ }
51
+ this.deviceready().then(function () {
52
+ if (typeof cordova.file === 'undefined') {
53
+ reject('cordova.file plugin not found');
54
+ return;
55
+ }
56
+ var folder = cordova.file.dataDirectory;
57
+ //deviceready-------- no fired yet
58
+ if (!folder) {
59
+ reject('cordova.file.dataDirectory not found, cannot save to local file.');
60
+ return;
61
+ }
62
+ window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function (directoryEntry) {
63
+ directoryEntry.getFile(fileName, { create: true }, function (fileEntry) {
64
+ fileEntry.file(function (file) {
65
+ var reader = new FileReader();
66
+
67
+ reader.onloadend = function (e) {
68
+ console.log("FileManager: Successful file read: " + this.result + ": " + fileEntry.fullPath);
69
+ resolve(this.result);
70
+ };
71
+
72
+ reader.readAsText(file);
73
+
74
+ }, function (e) { reject(e) });
75
+ }, function (e) { reject(e) });
76
+ }, function (e) { reject(e) });
77
+ });
78
+ }.bind(this));
79
+ };
80
+
81
+ }
package/lib/storage.js CHANGED
@@ -1,66 +1,97 @@
1
- import { Filesystem, Directory, Encoding } from '@capacitor/filesystem';
1
+ import {
2
+ Directory, Encoding, Filesystem,
3
+ } from '@capacitor/filesystem';
2
4
 
3
5
  export default class Storage {
4
- constructor(_options) {
5
- }
6
+ constructor(_options) {
7
+ }
6
8
 
7
- async load(key) {
8
- let data = null;
9
+ async load(key) {
10
+ let data = null;
9
11
 
10
- let dataRaw = null;
11
- //read from local storage
12
- try {
13
- console.log('Storage: read - localStorage');
14
- if (localStorage && localStorage.getItem) {
15
- dataRaw = localStorage.getItem(key);
16
-
17
- }
18
- } catch (e) {
19
- console.log('Error in load: ' + e);
20
- }
21
- //read file from App Data folder
22
- if (dataRaw === null) {
23
- try {
24
- const contents = await Filesystem.readFile({
25
- path: key + ".json",
26
- directory: Directory.Data,
27
- encoding: Encoding.UTF8,
28
- });
29
- if (contents && contents.data) {
30
- dataRaw = contents.data;
31
- }
32
- } catch (e) {
33
- console.log('Error in load: ' + e);
34
- }
35
- }
12
+ let dataRaw = null;
13
+ //read from local storage
14
+ try {
15
+ console.log('Storage: read - localStorage');
16
+ if (localStorage && localStorage.getItem) {
17
+ dataRaw = localStorage.getItem(key);
36
18
 
37
- if (dataRaw !== null && dataRaw != '{}') {
38
- try {
39
- data = JSON.parse(dataRaw);
40
- } catch (e) {
41
- data = null; //error in the above string(in this case,yes)!
42
- }
19
+ }
20
+ } catch (e) {
21
+ console.log('Error in load: ' + e);
22
+ }
43
23
 
44
- }
24
+ //read file from App Data folder
25
+ if (dataRaw === null) {
26
+ try {
27
+ const contents = await Filesystem.readFile({
28
+ path: key + '.json',
29
+ directory: Directory.Data,
30
+ encoding: Encoding.UTF8,
31
+ });
32
+ if (contents && contents.data) {
33
+ dataRaw = contents.data;
34
+ }
35
+ } catch (e) {
36
+ console.log('Error in load: ' + e);
37
+ }
38
+ }
45
39
 
46
- return data;
47
- }
40
+ if (dataRaw !== null && dataRaw != '{}') {
41
+ try {
42
+ data = JSON.parse(dataRaw);
43
+ } catch (e) {
44
+ data = null; //error in the above string(in this case,yes)!
45
+ }
48
46
 
49
- async save(key, data) {
50
- const jsonData = JSON.stringify(data);
47
+ }
51
48
 
52
- try {
53
- console.log('Storage: save - localStorage');
54
- localStorage[key] = jsonData;
55
- } catch (e) {
56
- console.warn('save: ' + e);
57
- }
58
- //read file from App Data folder
59
- await Filesystem.writeFile({
60
- path: key + '.json',
61
- data: jsonData,
62
- directory: Directory.Data,
63
- encoding: Encoding.UTF8,
64
- });
65
- }
66
- }
49
+ return data;
50
+ }
51
+
52
+ async save(key, data) {
53
+ const jsonData = JSON.stringify(data);
54
+
55
+ try {
56
+ console.log('Storage: save - localStorage');
57
+ localStorage[key] = jsonData;
58
+ } catch (e) {
59
+ console.warn('save: ' + e);
60
+ }
61
+
62
+ //read file from App Data folder
63
+ await Filesystem.writeFile({
64
+ path: key + '.json',
65
+ data: jsonData,
66
+ directory: Directory.Data,
67
+ encoding: Encoding.UTF8,
68
+ });
69
+ }
70
+
71
+ async delete(key) {
72
+ let localStorageError = null;
73
+
74
+ try {
75
+ console.log('Storage: delete - localStorage');
76
+ globalThis.localStorage?.removeItem(key);
77
+ } catch (e) {
78
+ localStorageError = e;
79
+ }
80
+
81
+ try {
82
+ await Filesystem.deleteFile({
83
+ path: key + '.json',
84
+ directory: Directory.Data,
85
+ });
86
+ } catch (e) {
87
+ const message = String(e?.message || e);
88
+ if (!/does not exist|not found/i.test(message)) {
89
+ throw e;
90
+ }
91
+ }
92
+
93
+ if (localStorageError) {
94
+ throw localStorageError;
95
+ }
96
+ }
97
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@cardsjd/storage",
3
3
  "private": false,
4
- "version": "0.0.6",
4
+ "version": "0.0.9",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
7
7
  "files": [
@@ -9,9 +9,10 @@
9
9
  ],
10
10
  "scripts": {
11
11
  "lint": "eslint lib --report-unused-disable-directives --max-warnings 0",
12
- "lint:fix": "npm run lint -- --fix"
12
+ "lint:fix": "npm run lint -- --fix",
13
+ "test": "vitest run"
13
14
  },
14
- "dependencies": {
15
- "@capacitor/preferences": "^5.0.7"
15
+ "peerDependencies": {
16
+ "@capacitor/filesystem": ">=7 <9"
16
17
  }
17
18
  }