@mdgf11/filesystem-lib 2.2.19 → 2.2.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,16 +22,16 @@ export function writeFilesystemUpdate(update, source) {
22
22
  let log_path = SOURCE_TO_PATH[source];
23
23
  const timestamp = formatUpdateDate(update.date_end);
24
24
  fs.mkdirSync(log_path, { recursive: true });
25
- const updates_file_path = `${log_path}/log_${timestamp}.json`;
25
+ const updates_file_path = `${log_path}/${timestamp}_log.json`;
26
26
  removeOldUpdate(log_path);
27
27
  fs.writeFileSync(updates_file_path, JSON.stringify(update, null, 2), { encoding: "utf-8" });
28
28
  // Write to global All dir (never deleted — full history of all sources)
29
29
  fs.mkdirSync(ALL_UPDATE_DIR, { recursive: true });
30
- fs.writeFileSync(`${ALL_UPDATE_DIR}/log_${source.replace(/[^a-zA-Z0-9]/g, "_")}_${timestamp}.json`, JSON.stringify(update, null, 2), { encoding: "utf-8" });
30
+ fs.writeFileSync(`${ALL_UPDATE_DIR}/${timestamp}_log_${source.replace(/[^a-zA-Z0-9]/g, "_")}.json`, JSON.stringify(update, null, 2), { encoding: "utf-8" });
31
31
  }
32
32
  export function writeDocumentEvent(event) {
33
33
  const timestamp = formatUpdateDate(new Date(event.date));
34
- const filename = `event_${event.event}_${event.uuid}_${timestamp}.json`;
34
+ const filename = `${timestamp}_event_${event.event}_${event.uuid}.json`;
35
35
  const content = JSON.stringify(event, null, 2);
36
36
  // Write to source-specific dir (e.g. Updates/Juris/) — keeps all individual events
37
37
  const sourceDir = SOURCE_TO_PATH[event.source];
@@ -50,16 +50,14 @@ export function loadLastFilesystemUpdate(source) {
50
50
  const old_log_path = SOURCE_TO_PATH[source];
51
51
  if (!fs.existsSync(old_log_path))
52
52
  return empty_update;
53
- const files = fs.readdirSync(old_log_path);
54
- for (const file of files) {
55
- const fullPath = path.join(old_log_path, file);
56
- if (fs.statSync(fullPath).isFile() && file.toLowerCase().includes("log")) {
57
- const jsonString = fs.readFileSync(fullPath, 'utf-8');
58
- const parsed = JSON.parse(jsonString);
59
- return parsed;
60
- }
61
- }
62
- return empty_update;
53
+ const files = fs.readdirSync(old_log_path)
54
+ .filter(f => fs.statSync(path.join(old_log_path, f)).isFile() && f.toLowerCase().includes("log"))
55
+ .sort();
56
+ if (files.length === 0)
57
+ return empty_update;
58
+ const fullPath = path.join(old_log_path, files[files.length - 1]);
59
+ const jsonString = fs.readFileSync(fullPath, 'utf-8');
60
+ return JSON.parse(jsonString);
63
61
  }
64
62
  function formatUpdateDate(d = new Date()) {
65
63
  const pad = (n) => n.toString().padStart(2, "0");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mdgf11/filesystem-lib",
3
- "version": "2.2.19",
3
+ "version": "2.2.20",
4
4
  "description": "Library to extend usage of jurisprudencia-document",
5
5
  "license": "ISC",
6
6
  "author": "Miguel Fonseca",
@@ -29,19 +29,19 @@ export function writeFilesystemUpdate(update: FilesystemUpdate, source: Supporte
29
29
  const timestamp = formatUpdateDate(update.date_end);
30
30
 
31
31
  fs.mkdirSync(log_path, { recursive: true });
32
- const updates_file_path = `${log_path}/log_${timestamp}.json`;
32
+ const updates_file_path = `${log_path}/${timestamp}_log.json`;
33
33
 
34
34
  removeOldUpdate(log_path);
35
35
  fs.writeFileSync(updates_file_path, JSON.stringify(update, null, 2), { encoding: "utf-8" });
36
36
 
37
37
  // Write to global All dir (never deleted — full history of all sources)
38
38
  fs.mkdirSync(ALL_UPDATE_DIR, { recursive: true });
39
- fs.writeFileSync(`${ALL_UPDATE_DIR}/log_${source.replace(/[^a-zA-Z0-9]/g, "_")}_${timestamp}.json`, JSON.stringify(update, null, 2), { encoding: "utf-8" });
39
+ fs.writeFileSync(`${ALL_UPDATE_DIR}/${timestamp}_log_${source.replace(/[^a-zA-Z0-9]/g, "_")}.json`, JSON.stringify(update, null, 2), { encoding: "utf-8" });
40
40
  }
41
41
 
42
42
  export function writeDocumentEvent(event: DocumentUpdateEvent): void {
43
43
  const timestamp = formatUpdateDate(new Date(event.date));
44
- const filename = `event_${event.event}_${event.uuid}_${timestamp}.json`;
44
+ const filename = `${timestamp}_event_${event.event}_${event.uuid}.json`;
45
45
  const content = JSON.stringify(event, null, 2);
46
46
 
47
47
  // Write to source-specific dir (e.g. Updates/Juris/) — keeps all individual events
@@ -66,16 +66,15 @@ export function loadLastFilesystemUpdate(source: SupportedUpdateSources): Filesy
66
66
  if (!fs.existsSync(old_log_path))
67
67
  return empty_update;
68
68
 
69
- const files = fs.readdirSync(old_log_path);
70
- for (const file of files) {
71
- const fullPath = path.join(old_log_path, file);
72
- if (fs.statSync(fullPath).isFile() && file.toLowerCase().includes("log")) {
73
- const jsonString = fs.readFileSync(fullPath, 'utf-8');
74
- const parsed: FilesystemUpdate = JSON.parse(jsonString);
75
- return parsed;
76
- }
77
- }
78
- return empty_update;
69
+ const files = fs.readdirSync(old_log_path)
70
+ .filter(f => fs.statSync(path.join(old_log_path, f)).isFile() && f.toLowerCase().includes("log"))
71
+ .sort();
72
+
73
+ if (files.length === 0) return empty_update;
74
+
75
+ const fullPath = path.join(old_log_path, files[files.length - 1]);
76
+ const jsonString = fs.readFileSync(fullPath, 'utf-8');
77
+ return JSON.parse(jsonString) as FilesystemUpdate;
79
78
  }
80
79
 
81
80
  function formatUpdateDate(d: Date = new Date()): string {