Skip to content

Commit

Permalink
feat: Verify that files on disk exist before trying to open them + be…
Browse files Browse the repository at this point in the history
…tter error messages when files do not exist

Implements ptarmiganlabs#81
  • Loading branch information
Göran Sander committed Oct 14, 2022
1 parent 39afc11 commit ff74012
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
33 changes: 32 additions & 1 deletion src/globals.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const winston = require('winston');
const upath = require('upath');
const { promises: Fs } = require('fs');
require('winston-daily-rotate-file');

// Get app version from package.json file
Expand Down Expand Up @@ -42,11 +43,41 @@ const setLoggingLevel = (newLevel) => {
logTransports.find((transport) => transport.name === 'console').level = newLevel;
};

// Check file existence
async function exists(pathToCheck) {
try {
await Fs.access(pathToCheck);
return true;
} catch {
return false;
}
}

const verifyFileExists = (file) =>
// eslint-disable-next-line no-async-promise-executor, no-unused-vars
new Promise(async (resolve, reject) => {
try {
logger.debug(`Checking if file ${file} exists`);

const fileExists = await exists(file);

if (fileExists === true) {
resolve(true);
} else {
resolve(false);
}
} catch (err) {
logger.error(`Error while checking if file ${file} exists: ${JSON.stringify(err, null, 2)}`);
resolve(false);
}
});

module.exports = {
logger,
appVersion,
getLoggingLevel,
setLoggingLevel,
execPath,
isPkg
isPkg,
verifyFileExists,
};
31 changes: 26 additions & 5 deletions src/lib/enigma.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const WebSocket = require('ws');
const fs = require('fs-extra');
const path = require('path');

const { logger, execPath } = require('../globals');
const { logger, execPath, verifyFileExists } = require('../globals');

/**
* Helper function to read the contents of the certificate files:
Expand All @@ -18,10 +18,31 @@ const readCert = (filename) => fs.readFileSync(filename);
* @param {*} command
* @returns
*/
const setupEnigmaConnection = (options) => {
const setupEnigmaConnection = async (options) => {
logger.debug('Prepping for Enigma connection...');

// eslint-disable-next-line global-require
logger.verbose('Verify that cert files exists');

const fileCert = path.resolve(execPath, options.authCertFile);
const fileCertKey = path.resolve(execPath, options.authCertKeyFile);

const fileCertExists = await verifyFileExists(fileCert);
if (fileCertExists === false) {
logger.error(`Missing certificate key file ${fileCert}. Aborting`);
process.exit(1);
} else {
logger.verbose(`Certificate key file ${fileCert} found`);
}

const fileCertKeyExists = await verifyFileExists(fileCertKey);
if (fileCertKeyExists === false) {
logger.error(`Missing certificate key file ${fileCertKey}. Aborting`);
process.exit(1);
} else {
logger.verbose(`Certificate key file ${fileCertKey} found`);
}

// eslint-disable-next-line global-require, import/no-dynamic-require
const qixSchema = require(`enigma.js/schemas/${options.schemaVersion}`);

return {
Expand All @@ -35,8 +56,8 @@ const setupEnigmaConnection = (options) => {
}),
createSocket: (url) =>
new WebSocket(url, {
key: readCert(path.resolve(execPath, options.authCertKeyFile)),
cert: readCert(path.resolve(execPath, options.authCertFile)),
key: readCert(fileCertKey),
cert: readCert(fileCert),
headers: {
'X-Qlik-User': `UserDirectory=${options.authUserDir};UserId=${options.authUserId}`,
},
Expand Down
13 changes: 11 additions & 2 deletions src/lib/import-masteritem-excel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const enigma = require('enigma.js');
const xlsx = require('node-xlsx').default;

const { setupEnigmaConnection } = require('./enigma');
const { logger, setLoggingLevel, isPkg, execPath } = require('../globals');
const { logger, setLoggingLevel, isPkg, execPath, verifyFileExists } = require('../globals');

/**
* Find of column's positioon (zero based) given a column name.
Expand Down Expand Up @@ -36,6 +36,15 @@ const importMasterItemFromExcel = async (options) => {
logger.info(`Import master items from definitions in Excel file "${options.file}"`);
logger.debug(`Options: ${JSON.stringify(options, null, 2)}`);

// Verify Master items Excel file exists
const excelFileExists = await verifyFileExists(options.file);
if (excelFileExists === false) {
logger.error(`Missing master item Excel file ${options.file}. Aborting`);
process.exit(1);
} else {
logger.verbose(`Master item Excel file ${options.file} found`);
}

// Parse Excel file
const workSheetsFromFile = xlsx.parse(options.file);

Expand Down Expand Up @@ -66,7 +75,7 @@ const importMasterItemFromExcel = async (options) => {
const colPosMasterItemTag = getColumnPos(options, options.colMasterItemTag, sheet.data[0]);

// Configure Enigma.js
const configEnigma = setupEnigmaConnection(options);
const configEnigma = await setupEnigmaConnection(options);

const session = enigma.create(configEnigma);
if (options.logLevel === 'silly') {
Expand Down

0 comments on commit ff74012

Please sign in to comment.