diff --git a/dist/js/server/file.d.ts b/dist/js/server/file.d.ts index 4c183c7..18e27f8 100644 --- a/dist/js/server/file.d.ts +++ b/dist/js/server/file.d.ts @@ -1,43 +1,42 @@ /** * @summary Identifies language by file extension. Uses 'fortran' by default. - * @param filename {String} - * @param defaultLanguage {String} */ -export function getProgrammingLanguageFromFileExtension( +export declare function getProgrammingLanguageFromFileExtension( filename: string, defaultLanguage?: string, -): any; +): string; /** * @summary Formats a given file size. - * @param size {Number} file size. - * @param decimals {Number} number of decimals to round. + * @param size file size. + * @param decimals number of decimals to round. */ -export function formatFileSize(size: number, decimals?: number): string; +export declare function formatFileSize(size: number, decimals?: number): string; /** Get list of paths for files in a directory and filter by file extensions if provided. - * @param {string} dirPath - Path to current directory, i.e. $PWD - * @param {string[]} fileExtensions - File extensions to filter, e.g. `.yml` - * @param {boolean} resolvePath - whether to resolve the paths of files - * @returns {string[]} - Array of file paths + * @param dirPath - Path to current directory, i.e. $PWD + * @param fileExtensions - File extensions to filter, e.g. `.yml` + * @param resolvePath - whether to resolve the paths of files + * @returns - Array of file paths */ -export function getFilesInDirectory( +export declare function getFilesInDirectory( dirPath: string, fileExtensions?: string[], resolvePath?: boolean, ): string[]; /** * Get list of directories contained in current directory. - * @param {string} currentPath - current directory - * @return {*} + * @param currentPath - current directory */ -export function getDirectories(currentPath: string): any; +export declare function getDirectories(currentPath: string): string[]; /** * Construct object path compatible with lodash.get/lodash.set from file path. * Note: if no root path is provided the file's dirname is taken instead. - * @param {string} filePath - Path to file. - * @param {string} root - Path to a parent directory to construct relative path. - * @return {string} - Object path reflecting file path. + * @param filePath - Path to file. + * @param root - Path to a parent directory to construct relative path. + * @return - Object path reflecting file path. * @example * createObjectPathFromFilePath("/a/b/c/d/e.yml", "/a/b"); * // "['c']['d']['e']" */ -export function createObjectPathFromFilePath(filePath: string, root: string): string; +export declare function createObjectPathFromFilePath(filePath: string, root: string): string; +export declare function createDirIfNotExists(directory: string): Promise; +export declare function cleanDirectory(directory: string): Promise; diff --git a/dist/js/server/file.js b/dist/js/server/file.js index e04fe12..a130c77 100644 --- a/dist/js/server/file.js +++ b/dist/js/server/file.js @@ -5,14 +5,17 @@ var __importDefault = return mod && mod.__esModule ? mod : { default: mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.createObjectPathFromFilePath = +exports.cleanDirectory = + exports.createDirIfNotExists = + exports.createObjectPathFromFilePath = exports.getDirectories = exports.getFilesInDirectory = exports.formatFileSize = exports.getProgrammingLanguageFromFileExtension = void 0; const fs_1 = __importDefault(require("fs")); -const path_1 = __importDefault(require("path")); +const promises_1 = require("node:fs/promises"); +const node_path_1 = __importDefault(require("node:path")); const FILE_EXTENSION_TO_PROGRAMMING_LANGUAGE_MAP = { in: "fortran", sh: "shell", @@ -23,18 +26,21 @@ const FILE_EXTENSION_TO_PROGRAMMING_LANGUAGE_MAP = { }; /** * @summary Identifies language by file extension. Uses 'fortran' by default. - * @param filename {String} - * @param defaultLanguage {String} */ function getProgrammingLanguageFromFileExtension(filename, defaultLanguage = "fortran") { - const fileExt = filename.split(".").pop().toLowerCase(); + var _a; + const fileExt = + (_a = filename.split(".").pop()) === null || _a === void 0 ? void 0 : _a.toLowerCase(); + if (!fileExt) { + return defaultLanguage; + } return FILE_EXTENSION_TO_PROGRAMMING_LANGUAGE_MAP[fileExt] || defaultLanguage; } exports.getProgrammingLanguageFromFileExtension = getProgrammingLanguageFromFileExtension; /** * @summary Formats a given file size. - * @param size {Number} file size. - * @param decimals {Number} number of decimals to round. + * @param size file size. + * @param decimals number of decimals to round. */ function formatFileSize(size, decimals = 2) { if (size === 0) return "0 Bytes"; @@ -44,26 +50,26 @@ function formatFileSize(size, decimals = 2) { } exports.formatFileSize = formatFileSize; /** Get list of paths for files in a directory and filter by file extensions if provided. - * @param {string} dirPath - Path to current directory, i.e. $PWD - * @param {string[]} fileExtensions - File extensions to filter, e.g. `.yml` - * @param {boolean} resolvePath - whether to resolve the paths of files - * @returns {string[]} - Array of file paths + * @param dirPath - Path to current directory, i.e. $PWD + * @param fileExtensions - File extensions to filter, e.g. `.yml` + * @param resolvePath - whether to resolve the paths of files + * @returns - Array of file paths */ function getFilesInDirectory(dirPath, fileExtensions = [], resolvePath = true) { let fileNames = fs_1.default.readdirSync(dirPath); if (fileExtensions.length) { fileNames = fileNames.filter((dirItem) => - fileExtensions.includes(path_1.default.extname(dirItem)), + fileExtensions.includes(node_path_1.default.extname(dirItem)), ); } - if (resolvePath) return fileNames.map((fileName) => path_1.default.resolve(dirPath, fileName)); + if (resolvePath) + return fileNames.map((fileName) => node_path_1.default.resolve(dirPath, fileName)); return fileNames; } exports.getFilesInDirectory = getFilesInDirectory; /** * Get list of directories contained in current directory. - * @param {string} currentPath - current directory - * @return {*} + * @param currentPath - current directory */ function getDirectories(currentPath) { return fs_1.default @@ -75,18 +81,45 @@ exports.getDirectories = getDirectories; /** * Construct object path compatible with lodash.get/lodash.set from file path. * Note: if no root path is provided the file's dirname is taken instead. - * @param {string} filePath - Path to file. - * @param {string} root - Path to a parent directory to construct relative path. - * @return {string} - Object path reflecting file path. + * @param filePath - Path to file. + * @param root - Path to a parent directory to construct relative path. + * @return - Object path reflecting file path. * @example * createObjectPathFromFilePath("/a/b/c/d/e.yml", "/a/b"); * // "['c']['d']['e']" */ function createObjectPathFromFilePath(filePath, root) { - const dirname = path_1.default.dirname(filePath); - const extension = path_1.default.extname(filePath); - const basename = path_1.default.basename(filePath, extension); - const parentDirs = root ? path_1.default.relative(root, dirname).split(path_1.default.sep) : []; + const dirname = node_path_1.default.dirname(filePath); + const extension = node_path_1.default.extname(filePath); + const basename = node_path_1.default.basename(filePath, extension); + const parentDirs = root + ? node_path_1.default.relative(root, dirname).split(node_path_1.default.sep) + : []; return [...parentDirs, basename].map((item) => `['${item}']`).join(""); } exports.createObjectPathFromFilePath = createObjectPathFromFilePath; +async function createDirIfNotExists(directory) { + try { + await (0, promises_1.access)(directory); + } catch (err) { + await (0, promises_1.mkdir)(directory, { recursive: true }); + } +} +exports.createDirIfNotExists = createDirIfNotExists; +async function cleanDirectory(directory) { + const files = await (0, promises_1.readdir)(directory, { withFileTypes: true }); + for (let i = 0; i < files.length; i++) { + const file = files[i]; + if (file.isDirectory()) { + // eslint-disable-next-line no-await-in-loop + await (0, promises_1.rm)(node_path_1.default.join(directory, file.name), { + recursive: true, + force: true, + }); + } else { + // eslint-disable-next-line no-await-in-loop + await (0, promises_1.rm)(node_path_1.default.join(directory, file.name)); + } + } +} +exports.cleanDirectory = cleanDirectory;