@mieweb/wikigdrive
Version:
Google Drive to MarkDown synchronization
84 lines (83 loc) • 2.57 kB
JavaScript
export const LOG_NAME = '.wgd-local-log.csv';
export class LocalLog {
constructor(transformFileService) {
Object.defineProperty(this, "transformFileService", {
enumerable: true,
configurable: true,
writable: true,
value: transformFileService
});
Object.defineProperty(this, "rows", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
}
async load() {
this.rows = [];
if (!await this.transformFileService.exists(LOG_NAME)) {
return;
}
const content = await this.transformFileService.readFile(LOG_NAME) || '';
const rows = content.split('\n').map(row => row.trim()).filter(row => !!row);
rows.shift();
for (const row of rows) {
const cells = row.split(';');
this.append({
filePath: cells[0],
mtime: parseInt(cells[1]),
id: cells[2],
type: cells[3],
event: cells[4]
});
}
}
append(row) {
if (row.id === 'TO_FILL') {
return;
}
if (row.event === 'touched') {
if (this.findLastFile(row.id)) {
return;
}
}
if (!row.mtime) {
row.mtime = +new Date();
}
this.rows.push(row);
}
getLogs() {
return this.rows;
}
async save() {
const content = 'filePath;mtime;id;type;event';
const rowsContent = this.rows.map(row => {
return `${row.filePath};${row.mtime};${row.id};${row.type};${row.event}`;
}).join('\n');
await this.transformFileService.writeFile(LOG_NAME, content + '\n' + rowsContent);
}
findLastFile(id) {
for (let rowNo = this.rows.length - 1; rowNo >= 0; rowNo--) {
const row = this.rows[rowNo];
if (row.id === id) {
return row;
}
}
return null;
}
findLastFileByPath(filePath) {
for (let rowNo = this.rows.length - 1; rowNo >= 0; rowNo--) {
const row = this.rows[rowNo];
if (row.filePath === filePath) {
return row;
}
}
return null;
}
async remove(filePath) {
const originalLength = this.rows.length;
this.rows = this.rows.filter(logRow => logRow.filePath !== filePath);
return originalLength !== this.rows.length;
}
}