UNPKG

@softkit/i18n

Version:

This library is a simple wrapper based on [nestjs-i18n](https://nestjs-i18n.com/)

61 lines 2.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getFiles = exports.getDirectories = exports.isDirectory = exports.filterAsync = exports.mapAsync = exports.exists = void 0; const tslib_1 = require("tslib"); const promises_1 = require("node:fs/promises"); const node_path_1 = tslib_1.__importDefault(require("node:path")); const i18n_module_1 = require("../i18n.module"); const exists = async (path) => { return !!(await (0, promises_1.stat)(path)); }; exports.exists = exists; function mapAsync(array, callbackfn) { return Promise.all(array.map(callbackfn)); } exports.mapAsync = mapAsync; async function filterAsync(array, callbackfn) { const filterMap = await mapAsync(array, callbackfn); return array.filter((_, index) => filterMap[index]); } exports.filterAsync = filterAsync; const isDirectory = async (source) => { const stats = await (0, promises_1.lstat)(source); return stats.isDirectory(); }; exports.isDirectory = isDirectory; const getDirectories = async (source) => { const dirs = await (0, promises_1.readdir)(source); return filterAsync(dirs.map((name) => node_path_1.default.join(source, name)), exports.isDirectory); }; exports.getDirectories = getDirectories; const getFiles = async (dirPath, pattern, includeSubfolders) => { const dirs = await (0, promises_1.readdir)(dirPath, { withFileTypes: true, }); const files = []; const deepFiles = []; for (const f of dirs) { try { if (typeof f === 'string') { if ((await (0, exports.exists)(node_path_1.default.join(dirPath, f))) && pattern.test(f)) { files.push(f); } } else if (f.isFile() && pattern.test(f.name)) { files.push(f); } else if (includeSubfolders && f.isDirectory()) { deepFiles.push(...(await (0, exports.getFiles)(node_path_1.default.join(dirPath, f.name), pattern, includeSubfolders))); } } catch { i18n_module_1.logger.warn(`Could not load and read file ${JSON.stringify(f)}`); } } return [ ...files.map((f) => node_path_1.default.join(dirPath, typeof f === 'string' ? f : f.name)), ...deepFiles, ]; }; exports.getFiles = getFiles; //# sourceMappingURL=file.js.map