UNPKG

vrem

Version:

An open-source automatic time-tracker

71 lines (70 loc) 2.69 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.autoTimeLogsFolder = void 0; const fs_extra_1 = __importDefault(require("fs-extra")); const path_1 = __importDefault(require("path")); const constants_1 = __importDefault(require("../constants")); const tracker_utils_1 = require("../tracker/tracker_utils"); const colors_1 = __importDefault(require("../colors")); const appFolder = constants_1.default.appFolder; exports.autoTimeLogsFolder = path_1.default.resolve(appFolder, 'auto_time_logs'); function* getLogEntriesFromFile(filePath) { if (!fs_extra_1.default.existsSync(filePath)) { return; } // const rl = readline.createInterface({ // input: fs.createReadStream(filePath), // crlfDelay: Infinity // }); const lines = fs_extra_1.default.readFileSync(filePath, 'utf8').split('\n'); //console.log(filePath, lines.length); for (const line of lines) { try { if (line) yield JSON.parse(line.trim()); } catch (e) { } } } function* getAllLogEntries() { const files = fs_extra_1.default.readdirSync(exports.autoTimeLogsFolder).filter(fileName => { return /^\d\d\d\d-\d\d-\d\d\.txt$/.test(fileName); }).sort(); for (const file of files) { for (const entry of getLogEntriesFromFile(path_1.default.resolve(exports.autoTimeLogsFolder, file))) { yield entry; } } } function getAvailableName(startName) { let index = 0; let name; do { name = startName + (index ? '_' + index : ''); index++; } while (fs_extra_1.default.existsSync(name)); return name; } function migrate0(db) { if (!fs_extra_1.default.pathExistsSync(exports.autoTimeLogsFolder)) return false; console.info(colors_1.default.cyan(`\nStarting migration 0...`)); let errorCount = 0; db.transaction(() => { for (const entry of getAllLogEntries()) { try { (0, tracker_utils_1.addToLogs)(entry, false); } catch (e) { errorCount++; } } }).immediate(); fs_extra_1.default.renameSync(exports.autoTimeLogsFolder, getAvailableName(exports.autoTimeLogsFolder + '__processed')); console.info(colors_1.default[errorCount ? 'yellow' : 'green'](`Migration 0 finished. ${errorCount ? 'With ' + errorCount + ' errors.' : ''}`)); return true; } exports.default = migrate0;