{"version":3,"file":"index.cjs","sources":["../../src/fs/index.js","../../src/fs/readFileLineByLineAsync.js","../../src/fs/updateFileIfRequired.js"],"sourcesContent":["import { readFileLineByLineAsync } from './readFileLineByLineAsync.js';\nimport { updateFileIfRequired } from './updateFileIfRequired.js';\n\nconst fs = {\n    readFileLineByLineAsync,\n    updateFileIfRequired\n};\n\nexport { fs };\n","import { createReadStream } from 'node:fs';\nimport readline from 'node:readline';\n\n// http://stackoverflow.com/questions/16010915/parsing-huge-logfiles-in-node-js-read-in-line-by-line/23695940#23695940\n\nexport const readFileLineByLineAsync = function ({\n    filePath,\n    onBegin,\n    onLine,\n    filterWhenOnLineReturnsTruthy = false,\n    abortWhenOnLineReturnsFalsy = false,\n    onProgress,\n    onError,\n    onEnd\n}) {\n    return new Promise((resolve) => {\n        try {\n            if (onBegin) {\n                onBegin();\n            }\n\n            let lineNumber = 1; // The file line number starts at 1\n            let countOfOnLineReturnedTruthy = 0;\n\n            const filteredResults = [];\n\n            const getStatus = ({ errored = false, aborted = false, completed = false } = {}) => {\n                const status = {\n                    lastLineNumberRead: (lineNumber - 1) || null,\n                    countOfOnLineReturnedTruthy\n                };\n\n                if (filterWhenOnLineReturnsTruthy) {\n                    status.filteredResults = filteredResults;\n                }\n                if (errored) {\n                    status.errored = true;\n                }\n                if (aborted) {\n                    status.aborted = true;\n                }\n                if (completed) {\n                    status.completed = true;\n                }\n\n                return status;\n            };\n\n            try {\n                const fileStream = createReadStream(filePath);\n                const rl = readline.createInterface({\n                    input: fileStream,\n                    crlfDelay: Infinity\n                });\n\n                // Handle errors on the file stream\n                fileStream.on('error', (err) => {\n                    if (onError) {\n                        onError(err);\n                    }\n                    resolve([err, getStatus({ errored: true })]);\n                });\n\n                // Process the file line by line\n                (async () => {\n                    try {\n                        for await (const line of rl) {\n                            const result = onLine(line, lineNumber);\n\n                            if (result) {\n                                if (filterWhenOnLineReturnsTruthy) {\n                                    filteredResults.push({\n                                        lineNumber,\n                                        line\n                                    });\n                                }\n                                countOfOnLineReturnedTruthy++;\n                            }\n                            lineNumber++;\n\n                            if (onProgress) {\n                                onProgress(getStatus());\n                            }\n\n                            if (!result && abortWhenOnLineReturnsFalsy) {\n                                // Clean up resources\n                                rl.close();\n                                fileStream.destroy();\n\n                                if (onEnd) {\n                                    onEnd();\n                                }\n                                resolve([null, getStatus({ aborted: true })]);\n                                return;\n                            }\n                        }\n\n                        if (onEnd) {\n                            onEnd();\n                        }\n                        resolve([null, getStatus({ completed: true })]);\n                        return;\n                    } catch (readError) {\n                        if (onError) {\n                            onError(readError);\n                        }\n                        resolve([readError, getStatus({ errored: true })]);\n                        return;\n                    }\n                })();\n            } catch (setupError) {\n                if (onError) {\n                    onError(setupError);\n                }\n                resolve([setupError, getStatus({ errored: true })]);\n                return;\n            }\n        } catch (err) {\n            if (onError) {\n                onError(err);\n            }\n            resolve([err, { errored: true }]);\n            return;\n        }\n    });\n};\n","import fs from 'node:fs';\n\n/**\n * This callback type is called `requestCallback` and is displayed as a global symbol.\n *\n * @callback callback\n * @param {object} err - null / Error (if there)\n * @param {string} status - 'read-error' / 'write-error' / 'file-updated' / 'file-update-not-required'\n */\n\n/**\n * @param {object} options\n * @param {string} options.file - Path of the file to be updated\n * @param {string} options.encoding - Encoding of the data\n * @param {string} options.data - The new data to be written\n * @todo Add support for other than string data-types for options.data\n * @param {boolean} options.verbose - To log the messages or not\n * @param {callback} options.callback - The function to be called on completion\n */\nfunction updateFileIfRequired(options) {\n    const\n        file = options.file,\n        encoding = options.encoding || 'utf8',\n        newData = options.data,\n        verbose = options.verbose || false,\n        cb = options.callback || function () {};\n    fs.readFile(file, encoding, function (err, oldData) {\n        // err.code is 'ENOENT' when the file doesn't exist\n        if (err && err.code !== 'ENOENT') {\n            if (verbose) {\n                console.log('Error in reading data for file: ' + file);\n                console.log(err);\n            }\n            cb(err, 'read-error');\n            return;\n        }\n\n        if (newData === oldData) {\n            cb(null, 'file-update-not-required');\n        } else {\n            fs.writeFile(file, newData, encoding, function (err) {\n                if (err) {\n                    if (verbose) {\n                        console.log('Error in writing data to file: ' + file);\n                        console.log(err);\n                    }\n                    cb(err, 'write-error');\n                } else {\n                    cb(null, 'file-updated');\n                }\n            });\n        }\n    });\n}\n\nexport { updateFileIfRequired };\n"],"names":["fs","readFileLineByLineAsync","filePath","onBegin","onLine","filterWhenOnLineReturnsTruthy","abortWhenOnLineReturnsFalsy","onProgress","onError","onEnd","Promise","resolve","lineNumber","countOfOnLineReturnedTruthy","filteredResults","getStatus","errored","aborted","completed","status","lastLineNumberRead","fileStream","createReadStream","rl","readline","createInterface","input","crlfDelay","Infinity","on","err","line","result","push","close","destroy","readError","setupError","updateFileIfRequired","options","file","encoding","newData","data","verbose","cb","callback","readFile","oldData","code","console","log","writeFile"],"mappings":";;AAGK,MAACA,GAAK,CACPC,wBCCmC,UAAUC,SAC7CA,SAAQC,QACRA,QAAOC,OACPA,OAAMC,8BACNA,+BAAgC,EAAKC,4BACrCA,6BAA8B,EAAKC,WACnCA,WAAUC,QACVA,QAAOC,MACPA,QAEA,OAAO,IAAIC,QAASC,UAChB,IACQR,SACAA;AAGJ,IAAIS,WAAa,EACbC,4BAA8B;AAElC,MAAMC,gBAAkB,GAElBC,UAAY,EAAGC,iBAAU,EAAOC,iBAAU,EAAOC,qBAAY,GAAU,MACzE,MAAMC,OAAS,CACXC,mBAAqBR,WAAa,GAAM,KACxCC;AAGAR,gCACAc,OAAOL,gBAAkBA;AAEzBE,UACAG,OAAOH,SAAU;AAEjBC,UACAE,OAAOF,SAAU;AAEjBC,YACAC,OAAOD,WAAY;AAGvB,OAAOC;AAGX,IACI,MAAME,WAAaC,KAAAA,iBAAiBpB,UAC9BqB,GAAKC,SAASC,gBAAgB,CAChCC,MAAOL,WACPM,UAAWC;AAIfP,WAAWQ,GAAG,QAAUC,MAChBtB,SACAA,QAAQsB;AAEZnB,QAAQ,CAACmB,IAAKf,UAAU,CAAEC,SAAS,QAIvC,WACI,IACI,UAAW,MAAMe,QAAQR,GAAI,CACz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