all files / commons/utils/ fileUtil.js

100% Statements 41/41
70% Branches 7/10
100% Functions 16/16
100% Lines 41/41
5 statements, 1 function, 2 branches Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105                          72×                           33× 33× 33× 33× 33× 33× 33×               33×                       72× 72× 182×   72×                 33× 33× 33× 33× 33×       33×                
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var fs = require("fs");
var fse = require("fs-extra");
var path = require("path");
/**
 * Class which holds helper tools when working with files.
 *
 * @export
 * @class FileUtil
 */
var FileUtil = (function () {
    function FileUtil() {
    }
    /**
     * Normalize folder path
     * @param {string} filePath path to be normalized
     * @returns {*} normalized path
     */
    FileUtil.prototype.normalizePath = function (filePath) {
        return path.isAbsolute(filePath) ? (path.normalize(filePath)) :
            "" + process.cwd() + (process.platform !== 'win32' ? '/' : '\\') + path.normalize(filePath);
    };
    /**
     * Check if a filePath represents the path for a directory or a file
     * @param {string} filePath - relative or absolute file path
     * @returns {boolean} check if path is dir
     */
    FileUtil.prototype.isDirectory = function (filePath) {
        var normalizedFilePath = this.normalizePath(filePath);
        return Boolean(normalizedFilePath.substring(normalizedFilePath.lastIndexOf('/'), normalizedFilePath.length).indexOf('.'));
    };
    /**
     * Create directory if not exist
     * @param {string} filePath path where the directory should be created.
     * @returns {Promise} directory created
     */
    FileUtil.prototype.createDirectory = function (filePath) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var normalizedFilePath;
            return tslib_1.__generator(this, function (_a) {
                normalizedFilePath = this.normalizePath(filePath);
                return [2 /*return*/, new Promise(function (resolve, reject) {
                        fs.stat(normalizedFilePath, function (err, stat) {
                            Iif (err) {
                                /* istanbul ignore next */
                                fse.ensureDir(normalizedFilePath, function (ensureDirErr) {
                                    if (ensureDirErr) {
                                        reject(ensureDirErr);
                                    }
                                    else {
                                        resolve(true);
                                    }
                                });
                            }
                            else {
                                resolve(Boolean(stat.isDirectory()));
                            }
                        });
                    })];
            });
        });
    };
    /**
     * Join list of paths
     * @param {Array<string>} paths array of paths that need to be merged
     * @returns {string} merged path
     */
    FileUtil.prototype.joinPaths = function () {
        var paths = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            paths[_i] = arguments[_i];
        }
        return path.join.apply(path, paths);
    };
    /**
     * Writes content into file.
     *
     * @param {string} filePath - destination file
     * @param {string} content - content to be written
     * @returns {Promise} file written
     */
    FileUtil.prototype.writeFile = function (filePath, content) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            return tslib_1.__generator(this, function (_a) {
                return [2 /*return*/, new Promise(function (resolve, reject) {
                        fs.writeFile(filePath, content, { encoding: 'UTF-8' }, function (err) {
                            Iif (err) {
                                /* istanbul ignore next */
                                reject(err);
                            }
                            else {
                                resolve();
                            }
                        });
                    })];
            });
        });
    };
    return FileUtil;
}());
exports.FileUtil = FileUtil;
exports.default = new FileUtil();