{"version":3,"file":"updateFileIfRequired.cjs","sources":["../../src/fs/updateFileIfRequired.js"],"sourcesContent":["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":["options","file","encoding","newData","data","verbose","cb","callback","fs","readFile","err","oldData","code","console","log","writeFile"],"mappings":";;6BAmBA,SAA8BA,SAC1B,MACIC,KAAOD,QAAQC,KACfC,SAAWF,QAAQE,UAAY,OAC/BC,QAAUH,QAAQI,KAClBC,QAAUL,QAAQK,UAAW,EAC7BC,GAAKN,QAAQO,UAAY,WAAa;AAC1CC,GAAGC,SAASR,KAAMC,SAAU,SAAUQ,IAAKC,SAEvC,GAAID,KAAoB,WAAbA,IAAIE,KAAf,CACI,GAAIP,QAAS,CACTQ,QAAQC,IAAI,mCAAqCb;AACjDY,QAAQC,IAAIJ,IAChB,CACAJ,GAAGI,IAAK,aAEZ,MAEIP,UAAYQ,QACZL,GAAG,KAAM,4BAETE,GAAGO,UAAUd,KAAME,QAASD,SAAU,SAAUQ,KAC5C,GAAIA,IAAK,CACL,GAAIL,QAAS,CACTQ,QAAQC,IAAI,kCAAoCb;AAChDY,QAAQC,IAAIJ,IAChB,CACAJ,GAAGI,IAAK,cACZ,MACIJ,GAAG,KAAM,eAEjB,EAER,EACJ"}