cordova-plugin-file icon indicating copy to clipboard operation
cordova-plugin-file copied to clipboard

Native electron support

Open zorn-v opened this issue 5 years ago • 28 comments

Platforms affected

Electron

Motivation and Context

Use native nodejs functions for filesystem instead of writing files to chrome IndexedDB of electron app. Closes https://github.com/apache/cordova-plugin-file/issues/334 https://github.com/apache/cordova-plugin-file/issues/312

Testing

https://github.com/zorn-v/cordova-electron-file-test

Checklist

  • [x] nodeIntegration set to true after plugin install
  • [x] resolveLocalFileSystemURL should resolve correct paths
  • [x] readEntries
  • [x] getFile
  • [x] getFileMetadata
  • [x] getMetadata
  • [x] setMetadata
  • [x] write
  • [x] readAsText
  • [x] readAsDataURL
  • [x] readAsBinaryString
  • [x] readAsArrayBuffer
  • [x] removeRecursively
  • [x] getDirectory
  • [x] getParent
  • [x] copyTo
  • [x] moveTo
  • [x] resolveLocalFileSystemURI
  • [x] truncate

zorn-v avatar Jan 19 '20 06:01 zorn-v

Currently I don't know how to execute native nodejs require for require('electron') because cordova override it

zorn-v avatar Jan 19 '20 07:01 zorn-v

Done

zorn-v avatar Jan 24 '20 08:01 zorn-v

Hi All is there any possibility this will ho in near future into release? It would be realy helpfull for us. As of now we are implementing it manually in application code what is sort of anti-pattern in regards to cordova architecture to make native calls in plugin layer.

@zorn-v Good job with this attempt. We would be happy to use your contribution.

miroslavvojtus avatar Mar 26 '20 09:03 miroslavvojtus

@miroslavvojtus

You can use cordova plugin add https://github.com/zorn-v/cordova-plugin-file.git#electron Or change it in package.json etc. to git+https://github.com/zorn-v/cordova-plugin-file.git#electron and run something like rm -rf plugins/cordova-plugin-file && npx cordova prepare

zorn-v avatar Mar 26 '20 09:03 zorn-v

Thanks @zorn-v , I'll check that, as I'm in the same situation as @miroslavvojtus and probably others..

cyberplant avatar Apr 02 '20 00:04 cyberplant

Thanks @zorn-v , I'll check that, as I'm in the same situation as @miroslavvojtus and probably others..

I was not able to make it work, so I decided to go back to just electron and forget about cordova (and mobile devices...).

cyberplant avatar Apr 30 '20 02:04 cyberplant

@miroslavvojtus

You can use cordova plugin add https://github.com/zorn-v/cordova-plugin-file.git#electron Or change it in package.json etc. to git+https://github.com/zorn-v/cordova-plugin-file.git#electron and run something like rm -rf plugins/cordova-plugin-file && npx cordova prepare

I tested this and it works like a charm. The only error I get is when I get the filewriter and use the truncate function. It throws a "missing command error". If it isn't used, everything works as expected

pjoriginal avatar May 20 '20 15:05 pjoriginal

The only error I get is when I get the filewriter and use the truncate function. It throws a "missing command error"

I'm just copy FileProxy.js from browser platform and implement all functions via native node. There was not truncate, but it is not problem to add it.

zorn-v avatar May 21 '20 03:05 zorn-v

Tested but not working; i have removed last cordova-plugin-file and added https://github.com/zorn-v/cordova-plugin-file.git#electron

plugin-file

antwal avatar Jun 19 '20 13:06 antwal

You doing something wrong. Try to remove node_modules, platforms, plugins and run npm i && npx cordova prepare

zorn-v avatar Jun 20 '20 12:06 zorn-v

Try to remove node_modules, platforms, plugins and run npm i && npx cordova prepare

I'm using cordova 10.0.0 with cordova-electron 2.0.0 on Linux. When the plugin gets loaded it throws an error:

 Uncaught TypeError: Cannot read property 'app' of undefined

Source: src/electron/FileProxy.js:35

so it's this line:

const app = nodeRequire('electron').remote.app;

I've checked the result of console.log(nodeRequire('electron')) and it does not contain no properties like remote, app or anything similar. There is a webFrame property but it does not contain any functions that are used by the plugin.

Is this happening because I'm using a wrong cordova or cordova-electron version?

Qualphey avatar Dec 19 '20 17:12 Qualphey

Seems in electron 10 remote property is disabled by default https://github.com/electron/electron/blob/master/docs/breaking-changes.md#default-changed-enableremotemodule-defaults-to-false I will add fix for that

zorn-v avatar Dec 21 '20 22:12 zorn-v

Try to reinstall plugin and check again

zorn-v avatar Dec 21 '20 22:12 zorn-v

Now I can't fetch the plugin anymore:

Discovered plugin "cordova-plugin-file". Adding it to the project
Failed to fetch plugin git+https://github.com/zorn-v/cordova-plugin-file.git#electron via registry.
Probably this is either a connection problem, or plugin spec is incorrect.
Check your connection and plugin name/version/URL.
CordovaError: CordovaError: Could not determine package name from output:
up to date, audited 421 packages in 6s

Same Check your connection and plugin name/version/URL error message when trying to add the plugin from file system. Is there something wrong with my configuration?

Qualphey avatar Dec 23 '20 20:12 Qualphey

I've figured out that fetch was failing because I've upgraded nodejs to v15.5.0. After downgrading it back to v14.12.0 it started working again. So I've managed to add the new version of your plugin, but unfortunately it is still dumping Error: exec proxy not found for :: File :: requestFileSystem.

Qualphey avatar Dec 23 '20 22:12 Qualphey

I update deps (cordova to 10 and cordova-electron to 2) in https://github.com/zorn-v/cordova-electron-file-test for test and all works fine.

https://github.com/apache/cordova-plugin-file/pull/371#issuecomment-646986189

zorn-v avatar Dec 24 '20 03:12 zorn-v

Yeah, the test does work! Thank you and happy holidays! ;)

Qualphey avatar Dec 24 '20 16:12 Qualphey

Any update on this?

pjoriginal avatar Apr 20 '21 16:04 pjoriginal

I think never. Just use my branch

zorn-v avatar Apr 21 '21 19:04 zorn-v

So I've managed to add the new version of your plugin, but unfortunately it is still dumping Error: exec proxy not found for :: File :: requestFileSystem.

I have the same problem. How can i fix that @zorn-v ?

RodriSanchez1 avatar Jun 04 '21 17:06 RodriSanchez1

I have the same problem. How can i fix that @zorn-v ?

Just as I said before https://github.com/apache/cordova-plugin-file/pull/371#issuecomment-646986189

zorn-v avatar Jun 10 '21 12:06 zorn-v

Hi,

I'm getting FileProxy.js:26 Electron Node.js integration is disabled, you can not use cordova-file-plugin without it...

with electron config:

{
  "browserWindow": {
    "width": 1536,
    "height": 864,
    "webPreferences": {
      "nodeIntegration": true
    }
  }
}

Tried https://github.com/apache/cordova-plugin-file/pull/371#issuecomment-646986189 and rm -rf ~/.config/Electron. same message, only width height changes.

Any ideas?

platform: linux cordova: 11.0 cordova-electron: 3.0.0 cordova-plugin-file: github:zorn-v/cordova-plugin-file#electron

eamanola avatar Mar 30 '22 07:03 eamanola

Any ideas?

Maybe config reads from another place, maybe electron does not accept disabled node integration (I doubt) any more. Lazy to check all versions.

zorn-v avatar Mar 30 '22 14:03 zorn-v

not too familiar with cordova or electron, the config seems to be the correct one thou. width and height do work. no worries, thanks anyway.

eamanola avatar Mar 30 '22 16:03 eamanola

https://github.com/electron/electron/issues/23506 Seems this is related, taking into account that cordova-electron: 3.0.0 use electron v14 https://cordova.apache.org/announcements/2021/09/06/cordova-electron-release-3.0.0.html

zorn-v avatar Apr 02 '22 19:04 zorn-v

Might try and follow this PR as a reference to update your PR.

Or create a new PR that is dedicated to the Cordova-Electron 3.0 support. Might be better to separate so if there is anyone who wants to continue to fork your current file pr for the older releases of Cordova-Electron.

https://github.com/apache/cordova-plugin-device/pull/135

In your PR, I noticed it enforced users to use nodeIntegration with a hookscript. The hookscript modified core template files which it should not, IMO. If the user uninstalled the plugin for any reason, then the modified core files is not reverted. They would have to reinstall the platform to revert those changes.

For a side question, why not use fs-extra instead of rimraf?

https://nodejs.libhunt.com/compare-rimraf-vs-node-fs-extra

fs-extra appears to be more popular and well maintained by the community and with faster release cycles.

Anyways, with the Cordova plugin support in Cordova-Electron 3.0, you can now make an npm package that will be installed during the plugin install. You can set dependencies to the subpackage. We no longer need to commit the third-party libraries into the repo, which is something we try to avoid. It uses context isolation as well.

erisu avatar Apr 03 '22 02:04 erisu

Might try and follow this PR as a reference to update your PR.

Or create a new PR that is dedicated to the Cordova-Electron 3.0 support. Might be better to separate so if there is anyone who wants to continue to fork your current file pr for the older releases of Cordova-Electron.

Is some sense in it ? My PR here already about 2 years without merge )

zorn-v avatar Apr 15 '22 23:04 zorn-v

@zorn-v @erisu Hi guys, when do you plan to merge latest changes? Thanks

marek8623 avatar Apr 05 '23 16:04 marek8623