Skip to content

Commit

Permalink
refactor: Handle optional dependencies better (#306)
Browse files Browse the repository at this point in the history
  • Loading branch information
abmusse committed Dec 8, 2020
1 parent 6805b67 commit c02b799
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 25 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module.exports = {
// Tweak rules set by airbnb config
// We need to allow use of console.log for verbose mode
'no-console': 'off',
// airbnb config forbids optionalDependencies
// https://github.com/airbnb/javascript/blob/c5bee75b1b358a3749f1a6d38ee6fad73de28e29/packages/eslint-config-airbnb-base/rules/imports.js#L95
"import/no-extraneous-dependencies": [ "error", { "optionalDependencies": true }]
},
overrides: [
{
Expand Down
4 changes: 2 additions & 2 deletions lib/transports/httpTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

const http = require('http');

const httpCall = (config, xmlInput, done) => {
function httpCall(config, xmlInput, done) {
const {
database = '*LOCAL',
username = null,
Expand Down Expand Up @@ -85,6 +85,6 @@ const httpCall = (config, xmlInput, done) => {
});
request.write(queryString);
request.end();
};
}

exports.httpCall = httpCall;
33 changes: 18 additions & 15 deletions lib/transports/idbTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

const idbCall = (config, xmlInput, cb) => {
const {
dbconn, dbstmt, IN, CLOB, CHAR, SQL_ATTR_DBC_SYS_NAMING, SQL_FALSE,
// idb-connector is an optional dependency, since users may not use this transport
// thus we can't globally require it
// eslint-disable-next-line max-len
// eslint-disable-next-line global-require, import/no-extraneous-dependencies, import/no-unresolved
} = require('idb-connector');
let idb = null;

try {
// eslint-disable-next-line global-require
idb = require('idb-connector');
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
}

function idbCall(config, xmlInput, cb) {
const {
database = '*LOCAL',
username = null,
Expand All @@ -37,9 +40,9 @@ const idbCall = (config, xmlInput, cb) => {
let xmlOutput = '';
const sql = `call ${xslib}.iPLUGR512K(?,?,?)`;
// eslint-disable-next-line new-cap
const conn = new dbconn();
const conn = new idb.dbconn();

conn.setConnAttr(SQL_ATTR_DBC_SYS_NAMING, SQL_FALSE);
conn.setConnAttr(idb.SQL_ATTR_DBC_SYS_NAMING, idb.SQL_FALSE);

if (typeof verbose === 'boolean') {
conn.debug(verbose);
Expand All @@ -56,12 +59,12 @@ const idbCall = (config, xmlInput, cb) => {
return;
}
// eslint-disable-next-line new-cap
const stmt = new dbstmt(conn);
const stmt = new idb.dbstmt(conn);

const parameters = [
[ipc, IN, CHAR],
[ctl, IN, CHAR],
[xmlInput, IN, CLOB],
[ipc, idb.IN, idb.CHAR],
[ctl, idb.IN, idb.CHAR],
[xmlInput, idb.IN, idb.CLOB],
];

// Before returning to caller, we must clean up
Expand Down Expand Up @@ -109,6 +112,6 @@ const idbCall = (config, xmlInput, cb) => {
});
});
});
};
}

exports.idbCall = idbCall;
15 changes: 11 additions & 4 deletions lib/transports/odbcTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

function odbcCall(config, xmlInput, done) {
// odbc is an optional dependency, since users may not use this transport
// eslint-disable-next-line global-require, import/no-extraneous-dependencies
const odbc = require('odbc');
let odbc = null;

try {
// eslint-disable-next-line global-require
odbc = require('odbc');
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
}

function odbcCall(config, xmlInput, done) {
const {
host = 'localhost',
username = null,
Expand Down
6 changes: 2 additions & 4 deletions lib/transports/sshTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

function sshCall(config, xmlIn, done) {
// ssh2 is an optional dependency, since users may not use this transport
// eslint-disable-next-line global-require, import/no-extraneous-dependencies
const { Client } = require('ssh2');
const { Client } = require('ssh2');

function sshCall(config, xmlIn, done) {
const {
verbose = false,
} = config;
Expand Down

0 comments on commit c02b799

Please sign in to comment.