noob-ethereum
Version:
A simple Ethereum library
46 lines (45 loc) • 1.82 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.exportToTextFile = exports.exportToJSONFile = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
/**
* Create JSON file from provided data and save to a directory of your choice
* @param {any} obj - Any data that can be stringified
* @param {string} filename - my_file
* @param {string} pathFromRoot - /src/destination_folder
*/
function exportToJSONFile(obj, filename, pathFromRoot) {
const json = JSON.stringify(obj);
const filePath = path_1.default.join(process.cwd(), pathFromRoot.slice(0));
fs_1.default.writeFile(`${filePath}/${filename}.json`, json, (err) => {
if (err) {
console.log(`Error writing file: ${filename}.json`, err);
}
else {
console.log(`Successfully created ${filename}.json in ${filePath}`);
}
});
}
exports.exportToJSONFile = exportToJSONFile;
/**
* Create Text file from provided data and save to a directory of your choice
* @param {any} obj - data (could be a buffer)
* @param {string} filename - my_file
* @param {string} pathFromRoot - /src/destination_folder
*/
function exportToTextFile(obj, filename, pathFromRoot) {
const filePath = path_1.default.join(process.cwd(), pathFromRoot.slice(0));
fs_1.default.writeFile(`${filePath}/${filename}.txt`, obj, (err) => {
if (err) {
console.log(`Error writing file: ${filename}.txt`, err);
}
else {
console.log(`Successfully created ${filename}.txt in ${filePath}`);
}
});
}
exports.exportToTextFile = exportToTextFile;