"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();
|