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

build adapter from custom source path #753

Merged
merged 4 commits into from
Oct 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 63 additions & 19 deletions loaders/adapterLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
* */

'use strict';


const fs = require('fs');
const blockLoader = require('block-loader');
const getAdapters = require('./getAdapters');

const adapters = getAdapters();
const files = fs.readdirSync('src/adapters').map((file) => file.replace(/\.[^/.]+$/, ''));
const adapterNames = adapters.map(getNames).filter(getUniques);
const adapterNames = adapters.filter(getStandardAdapters).filter(getUniques);
//adapters loaded from `srcPath`
const customAdapters = adapters.map(getCustomAdapters).filter(adapter => {
//filter undefined
return !!adapter;
});
const aliases = adapters.filter(getAliases);
const videoAdapters = adapters.filter(getVideoAdapters).map(getNames);

Expand All @@ -35,30 +38,34 @@ function insertAdapters() {
return '';
}

const inserts = adapterNames.map(name => {
let inserts = adapterNames.map(name => {
if (files.includes(name)) {
return name;
} else {
console.log(`Prebid Warning: no adapter found for ${name}, continuing.`);
}
});

if (!inserts.length) {
console.log('Prebid Warning: no matching adapters found for config, no adapters will be' +
' loaded.');
return '';
}

return inserts.map(name => {
inserts = inserts.map(name => {
return `var ${adapterName(name)} = require('./adapters/${name}.js');
exports.registerBidAdapter(new ${adapterName(name)}${useCreateNew(name)}(), '${name}');\n`;
})
.concat(customAdapters.map(adapter => {
return `let ${adapter.name} = require('${adapter.srcPath}');
exports.registerBidAdapter(new ${adapter.name}, '${adapter.name}');\n`;
}))
.concat(aliases.map(adapter => {
const name = Object.keys(adapter)[0];
const name = getNameStr(adapter);
return `exports.aliasBidAdapter('${name}','${adapter[name].alias}');\n`;
}))
.concat(`exports.videoAdapters = ${JSON.stringify(videoAdapters)};`)
.join('');

if (!inserts.length) {
console.log('No matching adapters found for config, no adapters will be loaded.');
return '';
}
return inserts;
}

/**
Expand All @@ -67,8 +74,11 @@ function insertAdapters() {
* @returns {string}
*/
function adapterName(adapter) {
const result = adapter.split('');
return result[0].toUpperCase() + result.join('').substr(1) + 'Adapter';
if (adapter) {
const result = adapter.split('');
return result[0].toUpperCase() + result.join('').substr(1) + 'Adapter';
}
return '';
}

/**
Expand Down Expand Up @@ -100,7 +110,7 @@ function getUniques(value, index, self) {
*/
function getNames(adapter) {
// if `length` then `adapter` is a string, otherwise an object
return adapter.length ? adapter : Object.keys(adapter)[0];
return adapter.length ? adapter : getNameStr(adapter);
}

/**
Expand All @@ -109,17 +119,51 @@ function getNames(adapter) {
* @returns {*}
*/
function getAliases(adapter) {
const name = Object.keys(adapter)[0];
const name = getNameStr(adapter);
return adapter && name && adapter[name].alias;
}

/**
* Returns adapter objects that support video
*/
function getVideoAdapters(adapter) {
const name = Object.keys(adapter)[0];
return adapter && name && adapter[name].supportedMediaTypes
&& adapter[name].supportedMediaTypes.includes('video');
const name = getNameStr(adapter);
return adapter && name && adapter[name].supportedMediaTypes &&
adapter[name].supportedMediaTypes.includes('video');
}

function getNameStr(adapter) {
return Object.keys(adapter)[0];
}

function getStandardAdapters(adapter) {
if (typeof adapter === 'string') {
return adapter;
}
}

function getCustomAdapters(adapter) {
const srcPath = getSrcPath(adapter);
if (srcPath === '') {
return;
}
if (!fileExists(srcPath)) {
console.warn(`Not able to locate adapter at: ${srcPath} continuing`);
return;
}
return {
name: getNames(adapter),
srcPath: srcPath
};
}

function getSrcPath(adapter) {
const name = getNameStr(adapter);
return name && adapter[name].srcPath ? adapter[name].srcPath : '';
}

function fileExists(path) {
return fs.existsSync(path);
}

module.exports = blockLoader(options);
3 changes: 3 additions & 0 deletions test/fixtures/allAdapters.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions test/spec/loaders/adapterLoader_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

const proxyquire = require('proxyquire');
const allAdapters = require('../../fixtures/allAdapters');
const expect = require('chai').expect;
require('../../../loaders/adapterLoader');

const defaultAdapters = ["aardvark","adblade","adbutler","adequant","adform","admedia","aol","appnexus","appnexusAst","getintent","hiromedia","indexExchange","kruxlink","komoona","openx","piximedia","pubmatic","pulsepoint","rubicon","sekindo","sonobi","sovrn","springserve","triplelift","yieldbot","nginad","brightcom","wideorbit","jcm","underdogmedia","memeglobal","centro","roxot",{"appnexus":{"alias":"brealtime"}},{"appnexus":{"alias":"pagescience"}},{"appnexus":{"alias":"defymedia"}},{"appnexusAst":{"supportedMediaTypes":["video"]}}];

const input = `/** INSERT ADAPTERS - DO NOT EDIT OR REMOVE */
/** END INSERT ADAPTERS */`;

describe('adapterLoader.js', () => {
it('should replace with the default set of adapters', () => {
const getAdapterStub = () => defaultAdapters;
const loader = proxyquire('../../../loaders/adapterLoader', {'./getAdapters' : getAdapterStub});
let output = loader(input);
expect(output).to.equal(allAdapters.getAllAdaptersString());

});

it('should return custom adapter list if file exists', () => {
const customAdapter = [{customAdapterName :{srcPath: '/somepath/customAdapterName.js'}}];
const getAdapterStub = () => customAdapter;
const loader = proxyquire('../../../loaders/adapterLoader', {'fs': {existsSync : ()=> true }, './getAdapters' : getAdapterStub});
let output = loader(input);
const expected = 'let customAdapterName = require(\'/somepath/customAdapterName.js\');\n exports.registerBidAdapter(new customAdapterName, \'customAdapterName\');\nexports.videoAdapters = [];';
expect(output).to.equal(expected);
});

it('should ignore custom adapters that that do not exist', () => {
const customAdapter = ['appnexus', {customAdapterName :{srcPath: '/somepath/customAdapterName.js'}}];
const getAdapterStub = () => customAdapter;
const loader = proxyquire('../../../loaders/adapterLoader', {'fs': {existsSync : ()=> false }, './getAdapters' : getAdapterStub});
let output = loader(input);
const expected = 'var AppnexusAdapter = require(\'./adapters/appnexus.js\');\n exports.registerBidAdapter(new AppnexusAdapter.createNew(), \'appnexus\');\nexports.videoAdapters = [];';
expect(output).to.equal(expected);
});

});