1234567891011121314151617181920212223242526272829303132333435 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.readFile = exports.writeFile = void 0;
- const fs = require("fs");
- const json = require("@discoveryjs/json-ext");
- const loggers_1 = require("./loggers");
- const logger = (0, loggers_1.getLogger)('json');
- const writeFile = (path, obj) => new Promise((resolve, reject) => {
- const renameSync = (oldPath, newPath) => {
- try {
- fs.renameSync(oldPath, newPath);
- return newPath;
- }
- catch (err) {
- reject(err);
- }
- };
- let backupPath;
- if (fs.statSync(path).size > 0) {
- backupPath = renameSync(path, `${path}.bak`);
- }
- json.stringifyStream(obj)
- .on('error', err => { logger.error(err); if (fs.existsSync(backupPath))
- renameSync(backupPath, path); resolve(false); })
- .pipe(fs.createWriteStream(path))
- .on('error', err => { reject(err); })
- .on('finish', () => { if (fs.existsSync(backupPath))
- fs.unlinkSync(`${path}.bak`); resolve(true); });
- });
- exports.writeFile = writeFile;
- const readFile = (path) => new Promise((resolve, reject) => {
- json.parseChunked(fs.createReadStream(path)
- .on('error', err => { reject(err); })).then(resolve);
- });
- exports.readFile = readFile;
|