You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ra...@apache.org on 2020/10/02 19:31:37 UTC
[cordova-electron] branch master updated: fix(Api): do not depend
on globals (#169)
This is an automated email from the ASF dual-hosted git repository.
raphinesse pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-electron.git
The following commit(s) were added to refs/heads/master by this push:
new e796fe3 fix(Api): do not depend on globals (#169)
e796fe3 is described below
commit e796fe37f1e6cc0735d5d32f785bd6bd20a835df
Author: Raphael von der GrĂ¼n <ra...@gmail.com>
AuthorDate: Fri Oct 2 21:29:59 2020 +0200
fix(Api): do not depend on globals (#169)
The use of globals in the bridge Api broke a few use-cases:
- loading the bridge Api from two different locations
- requiring `cordova-electron` without setting the globals first
This change refactors the run module back to an "extension method" of
Api. That is, it has to be called with `this` set to an Api instance.
This pattern is also used for `build` and `run` and it is pretty common
among the other platforms as well.
---
bin/templates/cordova/Api.js | 6 ------
lib/Api.js | 2 +-
lib/run.js | 4 ++--
tests/spec/unit/lib/run.spec.js | 13 +++++++++----
4 files changed, 12 insertions(+), 13 deletions(-)
diff --git a/bin/templates/cordova/Api.js b/bin/templates/cordova/Api.js
index 2d2cd99..175ff23 100644
--- a/bin/templates/cordova/Api.js
+++ b/bin/templates/cordova/Api.js
@@ -17,12 +17,6 @@
under the License.
*/
-const { resolve } = require('path');
-
-// Setting up some global defaults to share accross all files
-global.cdvPlatformPath = resolve(__dirname, '..');
-global.cdvProjectPath = resolve(__dirname, '../../..');
-
try {
module.exports = require('cordova-electron');
} catch (error) {
diff --git a/lib/Api.js b/lib/Api.js
index 0751a64..57e0d23 100644
--- a/lib/Api.js
+++ b/lib/Api.js
@@ -342,7 +342,7 @@ class Api {
}
run (runOptions) {
- return require('./run').run(runOptions);
+ return require('./run').run.call(this, runOptions);
}
clean (cleanOptions) {
diff --git a/lib/run.js b/lib/run.js
index ee983c6..451973e 100644
--- a/lib/run.js
+++ b/lib/run.js
@@ -21,12 +21,12 @@ const electron = require('electron');
const execa = require('execa');
const path = require('path');
-module.exports.run = (args = {}) => {
+module.exports.run = function (args = {}) {
const electonArgs = args.argv || [];
// Add the path to the main process as the last Electron argument to pass into execa
electonArgs.push(
- path.join(global.cdvPlatformPath, 'www/cdv-electron-main.js')
+ path.join(this.locations.www, 'cdv-electron-main.js')
);
const child = execa(electron, electonArgs);
diff --git a/tests/spec/unit/lib/run.spec.js b/tests/spec/unit/lib/run.spec.js
index 65b4d61..5e54b1f 100644
--- a/tests/spec/unit/lib/run.spec.js
+++ b/tests/spec/unit/lib/run.spec.js
@@ -22,6 +22,12 @@ const path = require('path');
const rootDir = path.resolve(__dirname, '../../../..');
+const apiStub = Object.freeze({
+ locations: Object.freeze({ www: 'FAKE_WWW' })
+});
+
+const expectedPathToMain = path.join(apiStub.locations.www, 'cdv-electron-main.js');
+
const run = rewire(path.join(rootDir, 'lib/run'));
describe('Run', () => {
@@ -29,7 +35,6 @@ describe('Run', () => {
it('should run electron with cdv-electron-main.js.', () => {
const execaSpy = jasmine.createSpy('execa');
const onSpy = jasmine.createSpy('on');
- const expectedPathToMain = path.join(rootDir, 'bin/templates/www/cdv-electron-main.js');
run.__set__('electron', 'electron-require');
spyOn(process, 'exit');
@@ -38,7 +43,7 @@ describe('Run', () => {
on: onSpy.and.callThrough()
}));
- run.run();
+ run.run.call(apiStub);
expect(execaSpy).toHaveBeenCalledWith('electron-require', [expectedPathToMain]);
expect(onSpy).toHaveBeenCalled();
@@ -54,7 +59,7 @@ describe('Run', () => {
const onSpy = jasmine.createSpy('on');
const expectedElectronArguments = [
'--inspect-brk=5858',
- path.join(rootDir, 'bin/templates/www/cdv-electron-main.js')
+ expectedPathToMain
];
run.__set__('electron', 'electron-require');
@@ -64,7 +69,7 @@ describe('Run', () => {
on: onSpy.and.callThrough()
}));
- run.run({ argv: ['--inspect-brk=5858'] });
+ run.run.call(apiStub, { argv: ['--inspect-brk=5858'] });
expect(execaSpy).toHaveBeenCalledWith('electron-require', expectedElectronArguments);
expect(onSpy).toHaveBeenCalled();
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org