Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit 0148fff

Browse files
authored
Merge pull request #381 from spezifant/master
Development (#1)
2 parents 2ddb95e + 75bd345 commit 0148fff

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 4.1.2 (2019-07-29)
2+
- [FIXED] Plugins can now be loaded from outside of the 'plugins/' directory
3+
14
# 4.1.1 (2019-06-17)
25
- [FIXED] Remove unnecessary `npm-cli-login` dependency.
36

lib/client.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const async = require('async');
1717
const concat = require('concat-stream');
1818
const debug = require('debug')('cloudant:client');
1919
const EventRelay = require('./eventrelay.js');
20+
const path = require('path');
2021
const PassThroughDuplex = require('./passthroughduplex.js');
2122
const pkg = require('../package.json');
2223
const utils = require('./clientutils.js');
@@ -101,7 +102,7 @@ class CloudantClient {
101102
var pluginName = Object.keys(plugin)[0];
102103

103104
try {
104-
Plugin = require('../plugins/' + pluginName);
105+
Plugin = require(self._buildPluginPath(pluginName));
105106
} catch (e) {
106107
throw new Error(`Failed to load plugin - ${e.message}`);
107108
}
@@ -119,7 +120,7 @@ class CloudantClient {
119120
}
120121

121122
try {
122-
Plugin = require('../plugins/' + plugin);
123+
Plugin = require(self._buildPluginPath(plugin));
123124
} catch (e) {
124125
throw new Error(`Failed to load plugin - ${e.message}`);
125126
}
@@ -147,6 +148,21 @@ class CloudantClient {
147148
});
148149
}
149150

151+
_buildPluginPath(name) {
152+
// Only a plugin name was provided: use plugin directory
153+
if (path.basename(name) === name) {
154+
return '../plugins/' + name;
155+
}
156+
157+
// An absolute path was provided
158+
if (path.isAbsolute(name)) {
159+
return name;
160+
}
161+
162+
// A relative path was provided
163+
return path.join(process.cwd(), name);
164+
}
165+
150166
_initClient(client) {
151167
if (typeof client !== 'undefined') {
152168
debug('Using custom client.');

test/client.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
const assert = require('assert');
1919
const Client = require('../lib/client.js');
2020
const nock = require('./nock.js');
21+
const path = require('path');
2122
const stream = require('stream');
2223
const testPlugin = require('./fixtures/testplugins.js');
2324
const uuidv4 = require('uuid/v4'); // random
@@ -86,6 +87,29 @@ describe('CloudantClient', function() {
8687
});
8788

8889
describe('plugin support', function() {
90+
it('get plugin path by name', function() {
91+
var cloudantClient = new Client();
92+
assert.equal(cloudantClient._buildPluginPath('dummy-plugin'), '../plugins/dummy-plugin');
93+
});
94+
95+
it('get plugin path by relative path', function() {
96+
var cloudantClient = new Client();
97+
assert.equal(cloudantClient._buildPluginPath('./dummy-plugin'), path.join(process.cwd(), 'dummy-plugin'));
98+
});
99+
100+
it('get plugin path by absolute path', function() {
101+
var cloudantClient = new Client();
102+
assert.equal(cloudantClient._buildPluginPath('/plugins/dummy-plugin'), '/plugins/dummy-plugin');
103+
});
104+
105+
it('load plugin from custom path', function() {
106+
let ownPlugin = 'test/fixtures/testplugin.js';
107+
var cloudantClient = new Client({
108+
plugins: [ ownPlugin ]
109+
});
110+
assert.equal(cloudantClient._plugins.length, 1);
111+
});
112+
89113
it('adds cookie authentication plugin if no other plugins are specified', function() {
90114
var cloudantClient = new Client();
91115
assert.equal(cloudantClient._plugins.length, 1);

test/fixtures/testplugin.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright © 2017, 2018 IBM Corp. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
'use strict';
15+
16+
const BasePlugin = require('../../plugins/base.js');
17+
18+
class TestPlugin extends BasePlugin {
19+
}
20+
21+
TestPlugin.id = 'test';
22+
23+
module.exports = TestPlugin;

0 commit comments

Comments
 (0)