Skip to content

Commit 301a8e7

Browse files
committed
chore: electron support initial
1 parent 17b4795 commit 301a8e7

14 files changed

Lines changed: 1249 additions & 130 deletions

dist/virtualfs-debug.js

Lines changed: 449 additions & 17 deletions
Large diffs are not rendered by default.

dist/virtualfs-debug.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/virtualfs.js

Lines changed: 108 additions & 61 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/virtualfs.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
const Constants = {
2525
MOUNT_DEVICE_NAME: 'nativeFsAccess',
26+
ELECTRON_DEVICE_NAME: 'electron',
2627
TAURI_DEVICE_NAME: 'tauri',
2728
TAURI_WS_DEVICE_NAME: 'tauriWS',
2829
KIND_FILE: 'file',

src/fslib.js

Lines changed: 84 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
const {ERR_CODES, Errors} = require('./errno');
2525
const {NativeFS} = require('./fslib_native');
2626
const {TauriFS} = require('./fslib_tauri');
27+
const {ElectronFS} = require('./fslib_electron');
2728
const {NodeTauriFS} = require('./fslib_node_ws');
2829
const {FilerFSModified} = require('./fslib_filer');
2930
const {Constants} = require('./constants');
@@ -100,6 +101,12 @@ const fileSystemLib = {
100101
openTauriFileSaveDialogueAsync: function (options) {
101102
return TauriFS.openTauriFileSaveDialogueAsync(options);
102103
},
104+
openElectronFilePickerAsync: function (options) {
105+
return ElectronFS.openElectronFilePickerAsync(options);
106+
},
107+
openElectronFileSaveDialogueAsync: function (options) {
108+
return ElectronFS.openElectronFileSaveDialogueAsync(options);
109+
},
103110
getTauriPlatformPath: function (virtualPath) {
104111
if(TauriFS.isTauriPath(virtualPath) || TauriFS.isTauriSubPath(virtualPath)) {
105112
return TauriFS.getTauriPlatformPath(virtualPath);
@@ -112,7 +119,12 @@ const fileSystemLib = {
112119
readdir: function (...args) { // (path, options, callback)
113120
let path = args[0];
114121
if(TauriFS.isTauriPath(path) || TauriFS.isTauriSubPath(path)) {
115-
return TauriFS.readdir(...args);
122+
if(window.__TAURI__) {
123+
return TauriFS.readdir(...args);
124+
}
125+
if(window.__ELECTRON__) {
126+
return ElectronFS.readdir(...args);
127+
}
116128
}
117129
if(Mounts.isMountPath(path) || Mounts.isMountSubPath(path)) {
118130
return NativeFS.readdir(...args);
@@ -126,7 +138,12 @@ const fileSystemLib = {
126138
return callback(new Errors.EINVAL(`Error Invalid path for stat: ${path}`));
127139
}
128140
if(TauriFS.isTauriSubPath(path)) {
129-
return TauriFS.stat(...args);
141+
if(window.__TAURI__) {
142+
return TauriFS.stat(...args);
143+
}
144+
if(window.__ELECTRON__) {
145+
return ElectronFS.stat(...args);
146+
}
130147
}
131148
if(Mounts.isMountSubPath(path)) {
132149
return NativeFS.stat(...args);
@@ -136,7 +153,12 @@ const fileSystemLib = {
136153
readFile: function (...args) { // (path, options, callback)
137154
let path = args[0];
138155
if(TauriFS.isTauriSubPath(path)) {
139-
return TauriFS.readFile(...args);
156+
if(window.__TAURI__) {
157+
return TauriFS.readFile(...args);
158+
}
159+
if(window.__ELECTRON__) {
160+
return ElectronFS.readFile(...args);
161+
}
140162
} else if(Mounts.isMountSubPath(path)) {
141163
return NativeFS.readFile(...args);
142164
}
@@ -165,7 +187,12 @@ const fileSystemLib = {
165187
}
166188

167189
if(TauriFS.isTauriSubPath(path)) {
168-
return TauriFS.writeFile(...args);
190+
if(window.__TAURI__) {
191+
return TauriFS.writeFile(...args);
192+
}
193+
if(window.__ELECTRON__) {
194+
return ElectronFS.writeFile(...args);
195+
}
169196
}
170197
fileSystemLib.stat(path, (err)=>{
171198
if(err && err.code === ERR_CODES.ERROR_CODES.ENOENT){
@@ -195,7 +222,12 @@ const fileSystemLib = {
195222
}
196223

197224
if(TauriFS.isTauriSubPath(path)) {
198-
return TauriFS.mkdirs(...args);
225+
if(window.__TAURI__) {
226+
return TauriFS.mkdirs(...args);
227+
}
228+
if(window.__ELECTRON__) {
229+
return ElectronFS.mkdirs(...args);
230+
}
199231
}
200232
if(Mounts.isMountSubPath(path)) {
201233
return NativeFS.mkdir(...args);
@@ -238,7 +270,12 @@ const fileSystemLib = {
238270
// in windows, we should be able to rename "a.txt" to "A.txt". Since windows is case-insensitive,
239271
// the below stat(A.txt) will return a stat for "a.txt" which is not what we want.
240272
if(TauriFS.isTauriSubPath(oldPath) && TauriFS.isTauriSubPath(newPath)) {
241-
return TauriFS.rename(oldPath, newPath, callbackInterceptor);
273+
if(window.__TAURI__) {
274+
return TauriFS.rename(oldPath, newPath, callbackInterceptor);
275+
}
276+
if(window.__ELECTRON__) {
277+
return ElectronFS.rename(oldPath, newPath, callbackInterceptor);
278+
}
242279
} else if(Mounts.isMountSubPath(oldPath) && Mounts.isMountSubPath(newPath)) {
243280
return NativeFS.renameSameNameDiffCase(oldPath, newPath, callbackInterceptor);
244281
}
@@ -250,7 +287,12 @@ const fileSystemLib = {
250287
return ;
251288
}
252289
if(TauriFS.isTauriSubPath(oldPath) && TauriFS.isTauriSubPath(newPath)) {
253-
return TauriFS.rename(oldPath, newPath, callbackInterceptor);
290+
if(window.__TAURI__) {
291+
return TauriFS.rename(oldPath, newPath, callbackInterceptor);
292+
}
293+
if(window.__ELECTRON__) {
294+
return ElectronFS.rename(oldPath, newPath, callbackInterceptor);
295+
}
254296
} else if(Mounts.isMountSubPath(oldPath) && Mounts.isMountSubPath(newPath)) {
255297
return NativeFS.rename(oldPath, newPath, callbackInterceptor);
256298
}
@@ -273,7 +315,12 @@ const fileSystemLib = {
273315
callbackInterceptor(new Errors.EPERM('Mount root directory cannot be deleted.'));
274316
return ;
275317
} else if(TauriFS.isTauriSubPath(path)) {
276-
return TauriFS.unlink(path, callbackInterceptor);
318+
if(window.__TAURI__) {
319+
return TauriFS.unlink(path, callbackInterceptor);
320+
}
321+
if(window.__ELECTRON__) {
322+
return ElectronFS.unlink(path, callbackInterceptor);
323+
}
277324
}
278325

279326
fileSystemLib.stat(path, (err, stat)=>{
@@ -314,8 +361,14 @@ const fileSystemLib = {
314361
// fall back to global copy here and will use node Tauri web socket fs adapter as and when it becomes available.
315362
if(Mounts.isMountSubPath(src) && Mounts.isMountSubPath(dst)) {
316363
return NativeFS.copy(src, dst, callbackInterceptor);
317-
} else if(TauriFS.canCopy() && TauriFS.isTauriSubPath(src) && TauriFS.isTauriSubPath(dst)) {
318-
return TauriFS.copy(src, dst, callbackInterceptor);
364+
} else if(TauriFS.isTauriSubPath(src) && TauriFS.isTauriSubPath(dst)) {
365+
if(window.__TAURI__ && TauriFS.canCopy()) {
366+
return TauriFS.copy(src, dst, callbackInterceptor);
367+
}
368+
if(window.__ELECTRON__ && ElectronFS.canCopy()) {
369+
return ElectronFS.copy(src, dst, callbackInterceptor);
370+
}
371+
return globalCopy(src, dst, callbackInterceptor);
319372
} else {
320373
return globalCopy(src, dst, callbackInterceptor);
321374
}
@@ -324,6 +377,9 @@ const fileSystemLib = {
324377
if(window.__TAURI__){
325378
return fileSystemLib.openTauriFileSaveDialogueAsync(options);
326379
}
380+
if(window.__ELECTRON__){
381+
return fileSystemLib.openElectronFileSaveDialogueAsync(options);
382+
}
327383
throw new Errors.ENOSYS('Phoenix fs showSaveDialog function not yet supported.');
328384
},
329385
watchAsync: function (path, gitIgnorePaths="") {
@@ -367,7 +423,12 @@ const fileSystemLib = {
367423
if (!recursive) {
368424
fileSystemLib.mkdir(path, mode, callback);
369425
} else if(TauriFS.isTauriSubPath(path)) {
370-
return TauriFS.mkdirs(path, mode, true, callback);
426+
if(window.__TAURI__) {
427+
return TauriFS.mkdirs(path, mode, true, callback);
428+
}
429+
if(window.__ELECTRON__) {
430+
return ElectronFS.mkdirs(path, mode, true, callback);
431+
}
371432
} else {
372433
_mkdir_p(fileSystemLib, path, mode, callback);
373434
}
@@ -385,10 +446,20 @@ const fileSystemLib = {
385446
return NodeTauriFS.getNodeWSEndpoint();
386447
},
387448
forceUseNodeWSEndpoint: function (use) {
388-
return TauriFS.forceUseNodeWSEndpoint(use);
449+
if(window.__TAURI__) {
450+
return TauriFS.forceUseNodeWSEndpoint(use);
451+
}
452+
if(window.__ELECTRON__) {
453+
return ElectronFS.forceUseNodeWSEndpoint(use);
454+
}
389455
},
390456
preferNodeWSEndpoint: function (use) {
391-
return TauriFS.preferNodeWSEndpoint(use);
457+
if(window.__TAURI__) {
458+
return TauriFS.preferNodeWSEndpoint(use);
459+
}
460+
if(window.__ELECTRON__) {
461+
return ElectronFS.preferNodeWSEndpoint(use);
462+
}
392463
},
393464
BYTE_ARRAY_ENCODING: Constants.BYTE_ARRAY_ENCODING,
394465
MOUNT_POINT_ROOT: Constants.MOUNT_POINT_ROOT,

0 commit comments

Comments
 (0)