mkdir-if-not-exists.js 735 Bytes
const MODULE_NAME = 'MKDIR-IF-NOT-EXISTS';

const fs = require('fs').promises;

const logger = require('tektrans-logger');

const mkdir = async (xid, dirname) => {
    try {
        logger.verbose(`${MODULE_NAME} EAE923FB: Making directory`, { xid, dirname });
        await fs.mkdir(dirname, { recursive: true });
    } catch (e) {
        logger.warn(`${MODULE_NAME} 1E8EBB84: Exception on making directory`, {
            xid, dirname, eCode: e.code, eMessage: e.message,
        });
    }
};

module.exports = async (xid, dirname) => {
    try {
        await fs.stat(dirname);
    } catch (e) {
        logger.verbose(`${MODULE_NAME} 1DB9EDAB: Directory not exists`, { xid, dirname });
        await mkdir(xid, dirname);
    }
};