{"id":21997725,"url":"https://github.com/osfunapps/os-file-handler-npm","last_synced_at":"2026-04-14T23:32:32.318Z","repository":{"id":98762352,"uuid":"206500101","full_name":"osfunapps/os-file-handler-npm","owner":"osfunapps","description":"This module contains fundamental files manipulation functions to implement in an npm project.","archived":false,"fork":false,"pushed_at":"2021-02-07T14:42:40.000Z","size":17,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-09T05:41:40.728Z","etag":null,"topics":["filehandling","files","nodejs","npm","osfunapps"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/osfunapps.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-09-05T07:18:03.000Z","updated_at":"2021-02-07T14:42:42.000Z","dependencies_parsed_at":"2023-06-15T15:15:48.108Z","dependency_job_id":null,"html_url":"https://github.com/osfunapps/os-file-handler-npm","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/osfunapps/os-file-handler-npm","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osfunapps%2Fos-file-handler-npm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osfunapps%2Fos-file-handler-npm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osfunapps%2Fos-file-handler-npm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osfunapps%2Fos-file-handler-npm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/osfunapps","download_url":"https://codeload.github.com/osfunapps/os-file-handler-npm/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osfunapps%2Fos-file-handler-npm/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31819718,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-14T18:05:02.291Z","status":"ssl_error","status_checked_at":"2026-04-14T18:05:01.765Z","response_time":153,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["filehandling","files","nodejs","npm","osfunapps"],"created_at":"2024-11-29T22:17:53.300Z","updated_at":"2026-04-14T23:32:32.301Z","avatar_url":"https://github.com/osfunapps.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Introduction\n------------\n\nThis module contains fundamental fileHandler scripts to implement in every npm project, to make life\neasier instead of just remember everything :)\n\n## Installation\nInstall via npm:\n    \n    npm i os-file-handler\n\n\n## Usage       \nRequire tools:\n```js\n/**\n * Will recursively create a directory, and all of the directories to it's path, if needed\n */\ncreateDir: function (dirPath) {\n    fs.mkdirSync(dirPath, {recursive: true});\n},\n\n/**\n * Will return the path to the parent of a file\n */\ngetParentDirPath: function (filePath) {\n    return path.dirname(filePath);\n},\n\n/**\n * Will check if a file is a directory\n */\n\nisDir: function (filePath) {\n    return new Promise(async function (resolve, reject) {\n        fs.lstat(filePath, (err, stats) =\u003e {\n            let dir = stats.isDirectory()\n            resolve(dir)\n        });\n    }.bind())\n},\n\n/**\n * Will return the inner dirs of a current dir\n */\ngetDirs: async function (filePath) {\n    let dirContent = self.getDirContent(filePath)\n    let dirsList = [];\n    for (let i = 0; i \u003c dirContent.length; i++) {\n        let pathh = self.joinPath(filePath, dirContent[i])\n        if (await self.isDir(pathh)) {\n            dirsList.push(dirContent[i])\n        }\n    }\n    return dirsList\n},\n\n/**\n * Will check if file (or directory) exists\n */\nisFileOrDirExists: function (path) {\n    try {\n        return fs.existsSync(path);\n    } catch (err) {\n        return false\n    }\n},\n\n/**\n * Will remove a file from a directory\n */\nremoveFile: function (filePath) {\n    try {\n        fs.unlinkSync(filePath);\n    } catch (e) {\n        //if no file exist, no problem\n    }\n},\n\n\n/**\n * Will remove a list of files\n */\nremoveFiles: function (filePaths) {\n    for (let i = 0; i \u003c filePaths.length; i++) {\n        self.removeFile(filePaths[i])\n    }\n},\n\n/**\n * Will copy a directory\n */\ncopyDir: function (src, dst) {\n    const fs = require('fs-extra')\n    fs.copySync(src, dst)\n},\n\n/**\n * Will return the content of a directory (only immediate children).\n */\ngetDirContent: function (dirPath, ignoreFiles = ['.DS_Store']) {\n    let files = fs.readdirSync(dirPath);\n    return files.filter(file =\u003e {\n        for (let i = 0; i \u003c ignoreFiles.length; i++) {\n            if (file === ignoreFiles[i]) {\n                return false\n            }\n        }\n        return true\n    })\n},\n\n/**\n * Will return the full paths of files and/or dirs in a directory.\n *\n * @param dirPath -\u003e the path to the directory\n * @param recursive -\u003e set to true if you want to look in inner directories as well\n * @param collectFiles -\u003e set to true if you want files in the output list\n * @param collectDirs -\u003e set to true if you want dirs in the output list\n * @param ignoreFiles -\u003e set the list of files you wish to ignore\n */\ngetDirContentFullPaths: function (dirPath, recursive = false, collectFiles = true, collectDirs = true, ignoreFiles = ['.DS_Store']) {\n    return new Promise(async function (resolve, reject) {\n        let files = [];\n        if (recursive) {\n            files = await self.findFilesInPath(dirPath, '*', '.*', collectFiles, collectDirs, ignoreFiles)\n        } else {\n            fs.readdirSync(dirPath).forEach(file =\u003e {\n                    let fullPath = path.join(dirPath, file);\n                    files.push(fullPath);\n                }\n            );\n            files = await filterFiles(files, collectFiles, collectDirs, ignoreFiles)\n        }\n        resolve(files)\n    }.bind())\n},\n\n\n/**\n * Will search for files in a given path\n */\nfindFilesInPath: function (dirPath, fileName = '*', fileExtension = '.*', collectFiles = true, collectDirs = true, ignoreFiles = ['.DS_Store']) {\n    return new Promise(function (resolve) {\n\n        const glob = require('glob');\n        let searchQuery = dirPath + '/**/' + fileName + fileExtension;\n        glob(searchQuery, {}, async function (err, files) {\n            resolve(await filterFiles(files, collectFiles, collectDirs, ignoreFiles))\n        })\n    }.bind())\n},\n\n\n/**\n * Will remove a directory (and all of it's content)\n */\nremoveDir: function (path) {\n    return new Promise(async function (resolve, reject) {\n        let deleteFolderRecursive = function (path) {\n            if (fs.existsSync(path)) {\n                fs.readdirSync(path).forEach(function (file, index) {\n                    var curPath = self.joinPath(path, file);\n                    if (fs.lstatSync(curPath).isDirectory()) { // recurse\n                        deleteFolderRecursive(curPath);\n                    } else { // delete file\n                        fs.unlinkSync(curPath);\n                    }\n                });\n                fs.rmdirSync(path);\n            }\n        };\n        deleteFolderRecursive(path)\n        resolve()\n    }.bind())\n},\n\n/**\n * Will remove a bunch of directories (and all of their content)\n */\nremoveDirs: function (dirList) {\n    return new Promise(async function (resolve, reject) {\n        for (let i = 0; i \u003c dirList.length; i++) {\n            await self.removeDir(dirList[i])\n        }\n        resolve()\n    }.bind())\n},\n\n\n/**\n * Will copy a file to a given destination.\n */\ncopyFile: async function (src, dest) {\n    return new Promise(async function (resolve, reject) {\n\n        let parentDir = self.getParentDirPath(dest);\n        if (!self.isDirExists(parentDir)) {\n            self.createDir(parentDir)\n        }\n        await mCopyFile(src, dest)\n\n        resolve()\n    }.bind());\n},\n\n\n/**\n * Will copy a list of files to a given destination.\n */\ncopyFiles: async function (filePathsList = [], destDir) {\n    return new Promise(async function (resolve, reject) {\n\n        let parentDir = self.getParentDirPath(destDir);\n        if (!self.isDirExists(parentDir)) {\n            self.createDir(parentDir)\n        }\n        for (let i = 0; i \u003c filePathsList.length; i++) {\n            let fileName = await self.getFileNameFromPath(filePathsList[i])\n            let destPath = self.joinPath(destDir, fileName)\n            await mCopyFile(filePathsList[i], destPath)\n        }\n\n        resolve()\n    }.bind());\n},\n\n\n/**\n * Will check if dir exists\n */\nisDirExists: function (path) {\n    return fs.existsSync(path)\n},\n\n\n/**\n * Will turn a list of file paths to file names\n */\nfilesPathListToFileNamesList: function (filePathList) {\n    let fileNamesList = [];\n    for (let i = 0; i \u003c filePathList.length; i++) {\n        fileNamesList[i] = self.getFileNameFromPath(filePathList[i])\n    }\n    return fileNamesList\n},\n\n/**\n * Will return the file name from a given path\n */\ngetFileNameFromPath: function (path, withExtension = true) {\n    let fName = path.replace(/^.*[\\\\\\/]/, '');\n    if (!withExtension) {\n        return self.stripExtension(fName)\n    } else {\n        return fName\n    }\n},\n\n/**\n * Will return the dir name from a given path\n */\ngetDirNameFromPath: function (dirPath) {\n    return path.basename(dirPath)\n},\n\n/**\n * Will strip the extension from a file\n */\nstripExtension: function (file) {\n    return file.split('.').slice(0, -1).join('.')\n},\n\n/**\n * Will join the paths of dirs\n */\njoinPath: function (...paths) {\n    return path.join(...paths)\n},\n\n/**\n * Will rename a file\n */\nrenameFile: function (filePath, newFileNameWithPath) {\n    return new Promise(async function (resolve, reject) {\n        fs.rename(filePath, newFileNameWithPath, function (err) {\n            if (err) {\n                console.log('ERROR: ' + err);\n            }\n            resolve()\n        });\n    }.bind())\n},\n\n/**\n * Will return the size of a file\n */\ngetFileSize: function (filePath, inMB = false, inKB = false, inBytes = false) {\n    const stats = fs.statSync(filePath);\n    const fileSizeInBytes = stats.size;\n    //Convert the file size to megabytes (optional)\n    if (inMB) return fileSizeInBytes / 1000000.0;\n    if (inKB) return fileSizeInBytes / 1000.0;\n    if (inBytes) return fileSizeInBytes;\n    return fileSizeInBytes\n},\n\n/**\n * Will filter a list of files by size.\n *\n * @param filePathsArr -\u003e the files list\n * @param checkInMB -\u003e set true to check in mb\n * @param checkInKb -\u003e set true to check in kb\n * @param checkInBytes -\u003e set true to check in bytes\n * @param biggerThanSize -\u003e set an int here if you want to check for bigger than\n * @param smallerThanSize -\u003e set an int here if you want to check for snmalle than\n * @return {Array} -\u003e a list of all of the file paths which correspond to the characteristics set\n */\nfilterFilesBySize: function (filePathsArr,\n                             checkInMB = false,\n                             checkInKb = false,\n                             checkInBytes = false,\n                             biggerThanSize = -1,\n                             smallerThanSize = -1) {\n\n    let resLst = [];\n    for (let i = 0; i \u003c filePathsArr.length; i++) {\n        let fileSize = self.getFileSize(filePathsArr[i], checkInMB, checkInKb, checkInBytes);\n\n        if (biggerThanSize !== -1) {\n            if (fileSize \u003e biggerThanSize) {\n                if (smallerThanSize !== -1) {\n                    if (fileSize \u003c smallerThanSize) {\n                        resLst.push(filePathsArr[i]);\n                        continue\n                    }\n                } else {\n                    resLst.push(filePathsArr[i]);\n                    continue\n                }\n            }\n        }\n\n        if (smallerThanSize !== -1) {\n            if (fileSize \u003c smallerThanSize) {\n                if (biggerThanSize !== -1) {\n                    if (fileSize \u003e biggerThanSize) {\n                        resLst.push(filePathsArr[i]);\n\n                    }\n                } else {\n                    resLst.push(filePathsArr[i]);\n\n                }\n            }\n        }\n\n    }\n    return resLst\n},\n\n/**\n * Will run a file\n */\nrunFile: function (filePath) {\n    let platform = '';\n    switch (process.platform) {\n        case 'darwin' :\n            platform = 'open';\n            break\n        case 'win32' :\n            platform = 'start';\n            break;\n        case 'win64' :\n            platform = 'start';\n            break;\n        default :\n            platform = 'xdg-open';\n            break;\n    }\n\n    const sys = require('sys');\n    let exec = require('child_process').exec;\n\n    exec(platform + ' ' + filePath);\n},\n\n/**\n * Will return the relative file path of a file from an absolute path.\n *\n * To get the absolute path of a file: __filename\n */\nrelativeFilePathFromAbsolutePath: function (absolutePath) {\n    return path.relative(process.cwd(), absolutePath)\n}\n\n\n};\n\n// Will do a filtration for a list of files by properties\nasync function filterFiles(filesArr, collectFiles = true, collectDirs = true, ignoreFiles = ['.DS_Store']) {\n    for (let i = filesArr.length - 1; i \u003e= 0; i--) {\n\n        // remove ignored\n        for (let j = 0; j \u003c ignoreFiles.length; j++) {\n            if (await self.getFileNameFromPath(filesArr[i]) === ignoreFiles[j]) {\n                filesArr.splice(i, 1)\n            }\n        }\n\n        // remove dirs\n        if (await self.isDir(filesArr[i])) {\n            if (!collectDirs) {\n                filesArr.splice(i, 1)\n            }\n        } else {\n            // remove files\n            if (!collectFiles) {\n                filesArr.splice(i, 1)\n            }\n        }\n\n    }\n    return filesArr\n}\n\n// Will copy a file\nasync function mCopyFile(src, dest) {\n    return new Promise(async function (resolve, reject) {\n        fs.copyFile(src, dest, (err) =\u003e {\n            if (err) {\n                throw err;\n            } else {\n                resolve()\n            }\n        });\n    }.bind());\n}\n```\nAnd more...\n\n\n## Links -\u003e see more tools\n* [os-tools-npm](https://github.com/osfunapps/os-tools-npm) -\u003e This module contains fundamental functions to implement in an npm project\n* [os-file-handler-npm](https://github.com/osfunapps/os-file-handler-npm) -\u003e This module contains fundamental files manipulation functions to implement in an npm project\n* [os-file-stream-handler-npm](https://github.com/osfunapps/os-file-stream-handler-npm) -\u003e This module contains read/write and more advanced operations on files\n* [os-xml-handler-npm](https://github.com/osfunapps/os-xml-handler-npm) -\u003e This module will build, read and manipulate an xml file. Other handy stuff is also available, like search for specific nodes\n\n[GitHub - osfunappsapps](https://github.com/osfunapps)\n\n\n\n## Licence\nISC","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fosfunapps%2Fos-file-handler-npm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fosfunapps%2Fos-file-handler-npm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fosfunapps%2Fos-file-handler-npm/lists"}