diff --git a/.eslintrc.js b/.eslintrc.js index 6c58a645..626868ca 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -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: [ { diff --git a/lib/transports/httpTransport.js b/lib/transports/httpTransport.js index 07fdaf95..72cc4d21 100644 --- a/lib/transports/httpTransport.js +++ b/lib/transports/httpTransport.js @@ -17,7 +17,7 @@ const http = require('http'); -const httpCall = (config, xmlInput, done) => { +function httpCall(config, xmlInput, done) { const { database = '*LOCAL', username = null, @@ -85,6 +85,6 @@ const httpCall = (config, xmlInput, done) => { }); request.write(queryString); request.end(); -}; +} exports.httpCall = httpCall; diff --git a/lib/transports/idbTransport.js b/lib/transports/idbTransport.js index 979fec24..e421a458 100644 --- a/lib/transports/idbTransport.js +++ b/lib/transports/idbTransport.js @@ -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, @@ -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); @@ -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 @@ -109,6 +112,6 @@ const idbCall = (config, xmlInput, cb) => { }); }); }); -}; +} exports.idbCall = idbCall; diff --git a/lib/transports/odbcTransport.js b/lib/transports/odbcTransport.js index b5592727..7786a9f4 100644 --- a/lib/transports/odbcTransport.js +++ b/lib/transports/odbcTransport.js @@ -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, diff --git a/lib/transports/sshTransport.js b/lib/transports/sshTransport.js index 243e264e..450feaf1 100644 --- a/lib/transports/sshTransport.js +++ b/lib/transports/sshTransport.js @@ -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;