UNPKG

the-logs

Version:

Stop using console.log and use this simple logs API for your daily proccess.

42 lines (41 loc) 1.94 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FileWriter = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const utils_1 = require("../utils"); class FileWriter { constructor(_props) { this._props = _props; this.buildStream = () => { const { filePath = process.cwd() } = this._props; if (!fs_1.default.existsSync(filePath)) fs_1.default.mkdirSync(filePath, { recursive: true }); const logPath = path_1.default.resolve(filePath, '.log'); const props = { encoding: 'utf8', flags: 'a+', mode: 666 }; return fs_1.default.createWriteStream(logPath, props); }; this.stream = this.buildStream(); this.stream.write('\n'); } log(params, props) { return __awaiter(this, void 0, void 0, function* () { const message = utils_1.MessageAsString(params, props); this.stream.write(message); this.stream.write('\n'); }); } } exports.FileWriter = FileWriter;