Skip to content

Commit c02b799

Browse files
authored
refactor: Handle optional dependencies better (#306)
1 parent 6805b67 commit c02b799

File tree

5 files changed

+36
-25
lines changed

5 files changed

+36
-25
lines changed

.eslintrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ module.exports = {
1010
// Tweak rules set by airbnb config
1111
// We need to allow use of console.log for verbose mode
1212
'no-console': 'off',
13+
// airbnb config forbids optionalDependencies
14+
// https://github.com/airbnb/javascript/blob/c5bee75b1b358a3749f1a6d38ee6fad73de28e29/packages/eslint-config-airbnb-base/rules/imports.js#L95
15+
"import/no-extraneous-dependencies": [ "error", { "optionalDependencies": true }]
1316
},
1417
overrides: [
1518
{

lib/transports/httpTransport.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
const http = require('http');
1919

20-
const httpCall = (config, xmlInput, done) => {
20+
function httpCall(config, xmlInput, done) {
2121
const {
2222
database = '*LOCAL',
2323
username = null,
@@ -85,6 +85,6 @@ const httpCall = (config, xmlInput, done) => {
8585
});
8686
request.write(queryString);
8787
request.end();
88-
};
88+
}
8989

9090
exports.httpCall = httpCall;

lib/transports/idbTransport.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,18 @@
1515
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1616
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1717

18-
const idbCall = (config, xmlInput, cb) => {
19-
const {
20-
dbconn, dbstmt, IN, CLOB, CHAR, SQL_ATTR_DBC_SYS_NAMING, SQL_FALSE,
21-
// idb-connector is an optional dependency, since users may not use this transport
22-
// thus we can't globally require it
23-
// eslint-disable-next-line max-len
24-
// eslint-disable-next-line global-require, import/no-extraneous-dependencies, import/no-unresolved
25-
} = require('idb-connector');
18+
let idb = null;
19+
20+
try {
21+
// eslint-disable-next-line global-require
22+
idb = require('idb-connector');
23+
} catch (e) {
24+
if (e.code !== 'MODULE_NOT_FOUND') {
25+
throw e;
26+
}
27+
}
2628

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

42-
conn.setConnAttr(SQL_ATTR_DBC_SYS_NAMING, SQL_FALSE);
45+
conn.setConnAttr(idb.SQL_ATTR_DBC_SYS_NAMING, idb.SQL_FALSE);
4346

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

6164
const parameters = [
62-
[ipc, IN, CHAR],
63-
[ctl, IN, CHAR],
64-
[xmlInput, IN, CLOB],
65+
[ipc, idb.IN, idb.CHAR],
66+
[ctl, idb.IN, idb.CHAR],
67+
[xmlInput, idb.IN, idb.CLOB],
6568
];
6669

6770
// Before returning to caller, we must clean up
@@ -109,6 +112,6 @@ const idbCall = (config, xmlInput, cb) => {
109112
});
110113
});
111114
});
112-
};
115+
}
113116

114117
exports.idbCall = idbCall;

lib/transports/odbcTransport.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,18 @@
1515
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1616
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1717

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

20+
try {
21+
// eslint-disable-next-line global-require
22+
odbc = require('odbc');
23+
} catch (e) {
24+
if (e.code !== 'MODULE_NOT_FOUND') {
25+
throw e;
26+
}
27+
}
28+
29+
function odbcCall(config, xmlInput, done) {
2330
const {
2431
host = 'localhost',
2532
username = null,

lib/transports/sshTransport.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1616
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1717

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

20+
function sshCall(config, xmlIn, done) {
2321
const {
2422
verbose = false,
2523
} = config;

0 commit comments

Comments
 (0)