zents
Version:
ZenTS is a Node.js & TypeScript MVC-Framework for building rich web applications, released as free and open-source software under the MIT License. It is designed for building web applications with modern tools and design patterns.
100 lines • 3.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fs = void 0;
const path_1 = require("path");
const app_root_path_1 = require("app-root-path");
const config_1 = require("../config/config");
const logger_1 = require("../log/logger");
const fs_1 = require("fs");
const readDirRecursiveGenerator_1 = require("./readDirRecursiveGenerator");
const illegalRegEx = /[/?<>\\:*|"]/g;
// eslint-disable-next-line no-control-regex
const controlRegEx = /[\x00-\x1f\x80-\x9f]/g;
const reservedRegEx = /^\.+$/;
const windowsReservedRegEx = /^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\..*)?$/i;
const windowsTrailingRegEx = /[. ]+$/;
class fs {
static async exists(pathToDirOrFile) {
let success = true;
try {
await fs_1.promises.access(pathToDirOrFile);
}
catch (error) {
success = false;
}
return success;
}
static async readDir(dir, recursive = true) {
const files = [];
if (recursive) {
for await (const file of readDirRecursiveGenerator_1.readDirRecursive(dir)) {
files.push(file);
}
}
else {
const dirContent = await fs_1.promises.readdir(dir, {
withFileTypes: true,
});
for (const content of dirContent) {
if (content.isFile()) {
files.push(path_1.resolve(dir, content.name));
}
}
}
return files;
}
static async writeJson(filePath, json) {
let success = true;
try {
await fs_1.promises.writeFile(filePath, JSON.stringify(json));
}
catch (e) {
success = false;
logger_1.log.error(e);
}
return success;
}
static async readJson(filePath) {
let json = null;
try {
const content = await fs_1.promises.readFile(filePath, {
encoding: 'utf-8',
});
json = JSON.parse(content);
}
catch (e) {
logger_1.log.error(e);
}
return json;
}
static resolveZenPath(key) {
const basePath = !this.isDev() ? config_1.config.paths.base.dist : config_1.config.paths.base.src;
const paths = config_1.config.paths;
return path_1.join(basePath, paths[key]);
}
static resolveZenFileExtension(filename) {
if (!filename) {
return !this.isDev() ? '.js' : '.ts';
}
return !this.isDev() ? `${filename}.js` : `${filename}.ts`;
}
static sanitizeFilename(filename) {
const sanitized = filename
.replace(illegalRegEx, '')
.replace(controlRegEx, '')
.replace(reservedRegEx, '')
.replace(windowsReservedRegEx, '')
.replace(windowsTrailingRegEx, '');
return sanitized.substring(0, 255);
}
static appDir() {
return app_root_path_1.path;
}
static isDev() {
return process.env.NODE_ENV === 'development' || this.isTsNode;
}
}
exports.fs = fs;
// @ts-ignore
fs.isTsNode = !!process[Symbol.for('ts-node.register.instance')];
//# sourceMappingURL=FS.js.map