Skip to content
Permalink
Browse files
Merge pull request #709 from raphinesse/next-gen-requireCordovaModule
- Throw error if non-cordova module is required (Resolves #689)
- Remove outdated compatibility layer from 2015
- Lazily load Context.cordova to avoid cyclical dependency
- Use const for imports
  • Loading branch information
erisu committed Mar 12, 2019
2 parents d4df8a9 + 198c2a2 commit 929a6ed22336479e9d8c03560849d15f2a9ef01a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 62 deletions.
@@ -18,7 +18,7 @@
*/

const rewire = require('rewire');
const events = require('cordova-common').events;
const { CordovaError } = require('cordova-common');

describe('hooks/Context', () => {
let Context;
@@ -27,17 +27,32 @@ describe('hooks/Context', () => {
Context = rewire('../../src/hooks/Context');
});

describe('requireCordovaModule', () => {
let warnSpy, requireCordovaModule;
describe('cordova', () => {
let context;

beforeEach(() => {
requireCordovaModule = Context.prototype.requireCordovaModule;
warnSpy = jasmine.createSpy('warnSpy');
events.on('warn', warnSpy);
spyOn(Context.prototype, 'requireCordovaModule');
context = new Context();
});

afterEach(() => {
events.removeListener('warn', warnSpy);
it('is only loaded when accessed', () => {
expect(context.requireCordovaModule).not.toHaveBeenCalled();
});

it('is set to require("cordova-lib").cordova', () => {
const cordova = Symbol('cordova');
context.requireCordovaModule.and.returnValue({ cordova });

expect(context.cordova).toBe(cordova);
expect(context.requireCordovaModule).toHaveBeenCalledWith('cordova-lib');
});
});

describe('requireCordovaModule', () => {
let requireCordovaModule;

beforeEach(() => {
requireCordovaModule = Context.prototype.requireCordovaModule;
});

it('correctly resolves cordova-* dependencies', () => {
@@ -73,34 +88,22 @@ describe('hooks/Context', () => {
Context.__set__({ require: requireSpy });
});

it('maps some old paths to their new equivalent', () => {
const ConfigParser = Symbol('ConfigParser');
const xmlHelpers = Symbol('xmlHelpers');
requireSpy.and.returnValue({ ConfigParser, xmlHelpers });

expect(requireCordovaModule('cordova-lib/src/configparser/ConfigParser')).toBe(ConfigParser);
expect(requireCordovaModule('cordova-lib/src/util/xml-helpers')).toBe(xmlHelpers);
expect(requireSpy.calls.allArgs()).toEqual([
['cordova-common'], ['cordova-common']
]);
});

it('correctly handles module names that start with "cordova-lib"', () => {
requireCordovaModule('cordova-libre');
expect(requireSpy).toHaveBeenCalledWith('cordova-libre');
});

it('emits a warning if non-cordova module is requested', () => {
requireCordovaModule('q');

expect(requireSpy).toHaveBeenCalledWith('q');
expect(warnSpy).toHaveBeenCalledTimes(1);

const message = warnSpy.calls.argsFor(0)[0];
expect(message).toContain('requireCordovaModule');
expect(message).toContain('non-cordova module');
expect(message).toContain('deprecated');
expect(message).toContain('"q"');
it('throws if non-cordova module is requested', () => {
const expectErrorOnRequire = m =>
expect(() => requireCordovaModule(m))
.toThrowError(CordovaError, /non-cordova module/);

expectErrorOnRequire('q');
expectErrorOnRequire('.');
expectErrorOnRequire('..');
expectErrorOnRequire('./asd');
expectErrorOnRequire('../qwe');
expectErrorOnRequire('/foo');
});
});

@@ -17,8 +17,8 @@
under the License.
*/

var path = require('path');
var events = require('cordova-common').events;
const path = require('path');
const { CordovaError } = require('cordova-common');

/**
* Creates hook script context
@@ -33,19 +33,12 @@ function Context (hook, opts) {
// For example context.opts.plugin = Object is done, then it affects by reference
this.opts = Object.assign({}, opts);
this.cmdLine = process.argv.join(' ');
this.cordova = require('../cordova/cordova');
}

// As per CB-9834 we need to maintain backward compatibility and provide a compat layer
// for plugins that still require modules, factored to cordova-common.
var compatMap = {
'../configparser/ConfigParser': function () {
return require('cordova-common').ConfigParser;
},
'../util/xml-helpers': function () {
return require('cordova-common').xmlHelpers;
}
};
// Lazy-load cordova to avoid cyclical dependency
Object.defineProperty(this, 'cordova', {
get () { return this.requireCordovaModule('cordova-lib').cordova; }
});
}

/**
* Requires the specified Cordova module.
@@ -61,31 +54,20 @@ Context.prototype.requireCordovaModule = function (modulePath) {
const [pkg, ...pkgPath] = modulePath.split('/');

if (!pkg.match(/^cordova-[^/]+/)) {
events.emit('warn',
throw new CordovaError(
`Using "requireCordovaModule" to load non-cordova module ` +
`"${modulePath}" is deprecated. Instead, add this module to ` +
`"${modulePath}" is not supported. Instead, add this module to ` +
`your dependencies and use regular "require" to load it.`
);
}

if (pkg !== 'cordova-lib') return require(modulePath);

// We can only resolve `cordova-lib` by name if this module is installed as
// a dependency of the current main module (e.g. when running `cordova`).
// To handle `cordova-lib` paths correctly in all other cases too, we
// resolve them to real paths before requiring them.
var resolvedPath = path.resolve(__dirname, '../..', ...pkgPath);
var relativePath = path.relative(__dirname, resolvedPath).replace(/\\/g, '/');
events.emit('verbose', 'Resolving module name for ' + modulePath + ' => ' + relativePath);

var compatRequire = compatMap[relativePath];
if (compatRequire) {
events.emit('warn', 'The module "' + path.basename(relativePath) + '" has been factored ' +
'into "cordova-common". Consider update your plugin hooks.');
return compatRequire();
}

return require(relativePath);
// require them using relative paths.
return pkg === 'cordova-lib'
? require(path.posix.join('../..', ...pkgPath))
: require(modulePath);
};

module.exports = Context;

0 comments on commit 929a6ed

Please sign in to comment.