Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running 'cordova prepare electron' on an clean source control electron project with supported plugin fails to write to package.json. #241

Open
3 tasks done
Serivy opened this issue Jul 19, 2022 · 0 comments

Comments

@Serivy
Copy link

Serivy commented Jul 19, 2022

Bug Report

Problem

In a Cordova project which has been retrieved from source control (eg, no platforms or plugin directory) cordova prepare electron when using a plugin which supports the new electron 3.0 plugin model throws an error trying to write to package.json.

What is expected to happen?

Cordova successfully prepares the electron platform with the cordova-plugin-device plugin.

What does actually happen?

npx cordova prepare electron
npx: installed 495 in 39.075s
Discovered platform "electron". Adding it to the project
Using cordova-fetch for cordova-electron@^3.1.0
Adding electron project...
Creating Cordova project for cordova-electron:
Path: C:\dev\testusecase\myApp\platforms\electron
Name: myApp
Discovered plugin "cordova-plugin-device". Adding it to the project
Installing "cordova-plugin-device" for electron
Error during processing of action! Attempting to revert...
Failed to install 'cordova-plugin-device': Error: Uh oh!
ENOENT: no such file or directory, open 'C:\dev\testusecase\myApp\platforms\electron\www\package.json'
at Object.openSync (fs.js:497:3)
at Object.readFileSync (fs.js:393:35)
at Object.install (C:\dev\testusecase\myApp\node_modules\cordova-electron\lib\handler.js:131:44) at C:\dev\testusecase\myApp\node_modules\cordova-electron\lib\Api.js:212:31
at ActionStack.process (C:\dev\testusecase\myApp\node_modules\cordova-common\src\ActionStack.js:56:25)
at Api.addPlugin (C:\dev\testusecase\myApp\node_modules\cordova-electron\lib\Api.js:132:24)
at handleInstall (C:\Users\m.hipper\AppData\Roaming\npm-cache_npx\32496\node_modules\cordova\node_modules\cordova-lib\src\plugman\install.js:561:10)
at C:\Users\m.hipper\AppData\Roaming\npm-cache_npx\32496\node_modules\cordova\node_modules\cordova-lib\src\plugman\install.js:344:28
at processTicksAndRejections (internal/process/task_queues.js:95:5)
Uh oh!
ENOENT: no such file or directory, open 'C:\dev\testusecase\myApp\platforms\electron\www\package.json'
Error: spawn C:\Windows\system32\cmd.exe ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:274:19)
at onErrorNT (internal/child_process.js:469:16)
at processTicksAndRejections (internal/process/task_queues.js:82:21)

Information

I found the issue working on a native file system plugin for electron and was able to reproduce it with cordova-plugin-device since it was upgraded. When you install electron and then add the plugin everything works fine because the package json is already there. In the situation of source control and build servers however it fails because prepare which also installs plugins have a race condition on the package.json file inside electron\www (Different to the root project package.json).

While steps are listed below, i also have a repo with the issue: https://github.com/Serivy/cordova-electron-plugin-issue

Work around

I have a work around which is to add a hook before the plugin is installed to create an empty package.json file {} which the plugin install then populates, followed by the main meta data.
config.xml
<hook src="before_plugin_install.js" type="before_plugin_install" />

before_plugin_install.js

const fs = require('fs');
const path = require('path');

module.exports = async function(context) {
    if (context.opts.cordova.platforms.indexOf('electron') >= 0 && context.opts.cordova.plugins.indexOf('cordova-plugin-device') >= 0) { 
        const electronPackage = path.resolve("./platforms/electron/www/package.json");
        const electronFolder = path.dirname(electronPackage);
        try {
            fs.statSync(electronFolder);
        } catch (e) {
            fs.mkdirSync(electronFolder, { recursive: true });
        }

        try {
            let stat = fs.statSync(electronPackage);

        } catch (e) {
            fs.writeFileSync(electronPackage, "{}");
        }
    }
}

Command or Code

Create the app and add plugin

npx cordova create myApp org.apache.cordova.myApp myApp
cd myapp
npm install cordova --save-dev
npx cordova plugin add cordova-plugin-device
npx cordova platform add electron

Reset like it would get it from source control in the .gitignore

npx rimraf ./plugins && npx rimraf ./platforms

Prepare the platform like in build servers

npx cordova prepare electron

Environment, Platform, Device

Cordova electron application with electron plugin (eg: cordova-plugin-device )

I saw a similar comment here #211 (comment) but its on a separate kind of issue..

Version information

"cordova": "^11.0.0",
"cordova-electron": "^3.1.0",
"cordova-plugin-device": "^2.1.0"
Operating system: Windows 10

Checklist

  • I searched for existing GitHub issues
  • I updated all Cordova tooling to most recent version
  • I included all the necessary information above
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant