{"id":21997734,"url":"https://github.com/osfunapps/os-file-stream-handler-npm","last_synced_at":"2026-04-18T00:01:38.939Z","repository":{"id":98762339,"uuid":"206502573","full_name":"osfunapps/os-file-stream-handler-npm","owner":"osfunapps","description":"This module contains read/write and more advanced operations on files","archived":false,"fork":false,"pushed_at":"2021-02-07T14:44:00.000Z","size":12,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-23T04:45:15.780Z","etag":null,"topics":["files","filestream","npm","osfunapps","readfiles","writefilesync"],"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:31:10.000Z","updated_at":"2021-02-07T14:44:02.000Z","dependencies_parsed_at":"2023-05-25T05:30:26.552Z","dependency_job_id":null,"html_url":"https://github.com/osfunapps/os-file-stream-handler-npm","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/osfunapps/os-file-stream-handler-npm","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osfunapps%2Fos-file-stream-handler-npm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osfunapps%2Fos-file-stream-handler-npm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osfunapps%2Fos-file-stream-handler-npm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osfunapps%2Fos-file-stream-handler-npm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/osfunapps","download_url":"https://codeload.github.com/osfunapps/os-file-stream-handler-npm/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osfunapps%2Fos-file-stream-handler-npm/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31950891,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-17T17:29:20.459Z","status":"ssl_error","status_checked_at":"2026-04-17T17:28:47.801Z","response_time":62,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: 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":["files","filestream","npm","osfunapps","readfiles","writefilesync"],"created_at":"2024-11-29T22:17:54.028Z","updated_at":"2026-04-18T00:01:38.920Z","avatar_url":"https://github.com/osfunapps.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Introduction\n------------\n\nThis module contains read/write and more advanced operations on files\n\n## Installation\nInstall via npm:\n```js\nnpm i os-file-stream-handler\n```\n\n## Usage       \nRequire fsh:\n```js\nvar fsh = require(\"os-file-stream-handler\")\n```\n## Functions and signatures:\n```js\n\n\n/**\n* Will read a file into an array of lines\n*\n* @param filePath -\u003e the path to the file\n*/\nreadFile: function (filePath) {\n    return new Promise(function (resolve, reject) {\n\n        let lineReader = require('readline').createInterface({\n            input: fs.createReadStream(filePath)\n        });\n        let lines = [];\n        lineReader.on('line', function (line) {\n            lines.push(line)\n        });\n        lineReader.on('close', function () {\n            resolve(lines);\n        });\n    }.bind())\n},\n\n/**\n * Will read a file with encoding to a string\n *\n * @param filePath -\u003e the path to the file\n * @param encoding -\u003e the desired encoding\n */\nreadFileWithEncoding: function (filePath, encoding='utf8') {\n    return fs.readFileSync(filePath, encoding);\n},\n\n/**\n * Will read a file to json\n */\nfileToJson: async function (filePath) {\n    const fileStr = await self.readFileWithEncoding(filePath);\n    return JSON.parse(fileStr);\n},\n\n\n/**\n * Will save text or, a bunch of lines in an array, into a file\n *\n * @param text -\u003e optional text to save\n * @param filePath -\u003e the path to the file\n */\nwriteFileSync: function (text = null, filePath) {\n    const fh = require('os-file-handler');\n    const parentDir = fh.getParentDir(filePath);\n    fh.createDir(parentDir);\n    fs.writeFileSync(filePath, text);\n},\n\n/**\n * Will save a json into a file\n *\n * @param jsonObj\n * @param filePath\n * @returns {Promise\u003cvoid\u003e}\n * @constructor\n */\nJSONObjectToFile: async function (jsonObj, filePath) {\n\n    // stringify JSON Object\n    const jsonContent = JSON.stringify(jsonObj);\n\n    fs.writeFile(filePath, jsonContent, 'utf8', function (err) {\n        if (err) {\n            console.log(\"An error occured while writing JSON Object to File.\");\n            return console.log(err);\n        }\n\n        console.log(\"JSON file has been saved.\");\n    });\n},\n\n/**\n * Will read a range of lines from a file.\n * Will start from lineToStart and stop at the first occurrence of lineToStop, as long as lineToStart found.\n *\n * @param filePath -\u003e the path to the file\n * @param lineToStart -\u003e the line to start the reading\n * @param lineToStop -\u003e the line to stop the reading (we will start to look for this line only after\n * the lineToStart has been found)\n * @param includeFirstLine -\u003e set to true to include the first line in the reading\n * @param includeLastLine -\u003e set to true to include the last line in the reading\n * @param isLineIdentical -\u003e true if the line should hold the exact match. False to if the line contains\n * @param caseSensitive -\u003e case sensitive toggler (if you set isLineIdentical to true, it of course, doesn't matter)\n */\nreadFileInRange: function (filePath,\n                           lineToStart,\n                           lineToStop,\n                           includeFirstLine = true,\n                           includeLastLine = true,\n                           isLineIdentical = true,\n                           caseSensitive = true) {\n    return new Promise(function (resolve, reject) {\n        let caseSensitiveFunc = returnSelf;\n        if (!caseSensitive) {\n            caseSensitiveFunc = textToLowerCase;\n            lineToStart = caseSensitiveFunc(lineToStart);\n            lineToStop = caseSensitiveFunc(lineToStop)\n        }\n\n        let compareFunc = isContains;\n        if (isLineIdentical) {\n            compareFunc = isMatch\n        }\n        let lineReader = require('readline').createInterface({\n            input: fs.createReadStream(filePath)\n        });\n\n        let startFound = false;\n        let lines = [];\n        let closed = false;\n        lineReader.on('line', function (line) {\n            let cLine = caseSensitiveFunc(line);\n            if (!startFound) {\n                if (compareFunc(cLine, lineToStart)) {\n                    startFound = true;\n                    if (includeFirstLine) {\n                        lines.push(line)\n                    }\n                }\n            } else {\n                if (compareFunc(cLine, lineToStop)) {\n                    if (includeLastLine) {\n                        lines.push(line)\n                    }\n                    resolve(lines);\n                    closeLineReader(lineReader);\n                    closed = true;\n                } else {\n                    lines.push(line)\n                }\n            }\n\n        });\n        lineReader.on('close', function () {\n            if(!closed) {\n                closeLineReader(lineReader);\n                resolve(undefined)\n            }\n\n        });\n    }.bind())\n},\n\n\n/**\n * Will check if a file contains text. If it does, return true else, false.\n *\n * @param filePath -\u003e the path to the file\n * @param text -\u003e the text which you'd like to look for\n * @param isLineIdentical -\u003e true if the line should hold the exact match. False to if the line contains\n * @param caseSensitive -\u003e case sensitive toggler (if you set isLineIdentical to true, it of course, doesn't matter)\n */\nisFileContainsText: function (filePath, text, isLineIdentical = true, caseSensitive = true) {\n    return new Promise(function (resolve, reject) {\n\n        let caseSensitiveFunc = returnSelf;\n        if (!caseSensitive) {\n            caseSensitiveFunc = textToLowerCase;\n            text = caseSensitiveFunc(text)\n        }\n\n        let compareFunc = isContains;\n        if (isLineIdentical) {\n            compareFunc = isMatch\n        }\n        let lineReader = require('readline').createInterface({\n            input: fs.createReadStream(filePath)\n        });\n\n        lineReader.on('line', function (line) {\n            line = caseSensitiveFunc(line);\n            if (compareFunc(line, text)) {\n                resolve(true);\n                closeLineReader(lineReader)\n            }\n        });\n        lineReader.on('close', function () {\n            resolve(false)\n        });\n    }.bind())\n},\n\n/**\n * Will return the file paths contains a text from a bunch of files.\n *\n * @param filePaths -\u003e an array carrying the list of files to look upon\n * @param text -\u003e the text which you'd like to look for\n * @param isLineIdentical -\u003e true if the line should hold the exact match. False to if the line contains\n * @param caseSensitive -\u003e case sensitive toggler (if you set isLineIdentical to true, it of course, doesn't matter)\n * @return Promise -\u003e a promise carrying an array to all of the files found (an empty array if nothing's found)\n */\ngetFilesContainsText: function (filePaths = [], text, isLineIdentical = true, caseSensitive = true) {\n    return new Promise(async function (resolve, reject) {\n\n        let filesFound = [];\n        for (let i = 0; i \u003c filePaths.length; i++) {\n            let found = await self.isFileContainsText(filePaths[i], text, isLineIdentical, caseSensitive);\n            if (found) {\n                filesFound.push(filePaths[i])\n            }\n        }\n        resolve(filesFound)\n    }.bind())\n},\n\n/**\n * Will look for expression in a file (or in array of lines) and return all of the occurrences\n * in which the expression exists.\n *\n * For example, if you have a file contains:\n *\n * package in.remotify.www.hathwayremote;\n * import android.annotation.SuppressLint;\n * import android.content.Context;\n * import android.content.SharedPreferences.Editor;\n * import android.hardware.ConsumerIrManager;\n * import android.hardware.Gyroscope;\n * import android.os.Bundle;\n *\n * and you want to scrape all of the lines which has the expression 'hardware', using this\n * function will return the array:\n * ['import android.hardware.ConsumerIrManager;', 'import android.hardware.Gyroscope;']\n *\n *\n * @param filePath -\u003e optional file path instead of just an array of lines\n * @param lines -\u003e optional strings in an array instead of a file\n * @param lineToFocusOn -\u003e the line which includes the string you want to scrape\n * @param strContainsStart -\u003e a string marking the start of the expression to return\n * @param includeAllOfStart -\u003e true to include all of the start expression in the selection,\n * false to exclude it and return only the expression after it\n * @param strContainsEnd -\u003e a string marking the end of the expression to return\n * @param includeAllOfEnd -\u003e true to include all of the end expression in the selection,\n * false to exclude it and return only the expression after it\n */\nfindExpression: function (filePath,\n                          lines = [],\n                          lineToFocusOn,\n                          strContainsStart,\n                          includeAllOfStart = false,\n                          strContainsEnd = null,\n                          includeAllOfEnd = false,) {\n    return new Promise(async function (resolve, reject) {\n\n        let txtLines = lines;\n        if (lines.length === 0) {\n            txtLines = self.readFile(filePath)\n        }\n\n        let matches = [];\n        // run on all of the lines in a java files\n        for (let j = 0; j \u003c txtLines.length; j++) {\n            let line = txtLines[j];\n            if (line.includes(strContainsStart)) {\n                let startIdx = line.indexOf(strContainsStart);\n                if (!includeAllOfStart) {\n                    startIdx += strContainsStart.length\n                }\n\n                let endIdx = line.indexOf(strContainsEnd, startIdx + 1);\n                if (includeAllOfEnd) {\n                    endIdx += strContainsEnd.length\n                }\n\n                let ans = \"\";\n                if (strContainsEnd !== null) {\n                    ans = line.substring(startIdx, endIdx);\n                    matches.push(ans);\n                } else {\n                    ans = line.substring(startIdx, line.length);\n                    matches.push(ans)\n                }\n            }\n        }\n        resolve(matches)\n    })\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## Licence\nISC","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fosfunapps%2Fos-file-stream-handler-npm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fosfunapps%2Fos-file-stream-handler-npm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fosfunapps%2Fos-file-stream-handler-npm/lists"}