@rr0/cms 0.3.58 → 0.3.59

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.
@@ -1,5 +1,6 @@
1
1
  import fs from "fs";
2
2
  import { writeFile } from "@javarome/fileutil";
3
+ import path from "path";
3
4
  /**
4
5
  * Saves the index file collected by the SearchCommand.
5
6
  */
@@ -24,17 +25,15 @@ export class SearchIndexStep {
24
25
  try {
25
26
  existingIndex = JSON.parse(fs.readFileSync(this.fileName, { encoding: this.encoding }));
26
27
  const newPages = newIndex.pages;
27
- const existingPages = existingIndex.pages;
28
- for (let newPage of newPages) {
29
- const alreadyIndexedPage = existingPages.find(existingPage => existingPage.url === newPage.url);
30
- if (alreadyIndexedPage) {
31
- Object.assign(alreadyIndexedPage, newPage);
32
- }
33
- else {
34
- existingPages.push(newPage);
35
- }
28
+ const contentRoot = path.dirname(path.dirname(this.fileName));
29
+ const pagesByUrl = new Map(existingIndex.pages
30
+ .filter(page => fs.existsSync(path.resolve(contentRoot, page.url)))
31
+ .map(page => [page.url, page]));
32
+ for (const newPage of newPages) {
33
+ pagesByUrl.set(newPage.url, newPage);
36
34
  }
37
- existingPages.sort((pageInfo1, pageInfo2) => pageInfo1.title > pageInfo2.title ? 1 : pageInfo1.title < pageInfo2.title ? -1 : 0);
35
+ existingIndex.pages = Array.from(pagesByUrl.values());
36
+ existingIndex.pages.sort((pageInfo1, pageInfo2) => pageInfo1.title > pageInfo2.title ? 1 : pageInfo1.title < pageInfo2.title ? -1 : 0);
38
37
  }
39
38
  catch (e) {
40
39
  if (e.errno !== -2) {
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,47 @@
1
+ import { describe, expect, test } from "@javarome/testscript";
2
+ import fs from "fs";
3
+ import os from "os";
4
+ import path from "path";
5
+ import { SearchIndexStep } from "./SearchIndexStep.js";
6
+ describe("SearchIndexStep", () => {
7
+ test("removes missing and duplicate pages while updating the current pages", async () => {
8
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "rr0-search-index-"));
9
+ try {
10
+ const searchDir = path.join(root, "search");
11
+ fs.mkdirSync(searchDir);
12
+ fs.writeFileSync(path.join(root, "current.html"), "");
13
+ const fileName = path.join(searchDir, "index.json");
14
+ const existingIndex = {
15
+ pages: [
16
+ { title: "Old current title", url: "current.html", time: "" },
17
+ { title: "Duplicate current title", url: "current.html", time: "" },
18
+ { title: "Moved page", url: "old/path.html", time: "" }
19
+ ],
20
+ words: {}
21
+ };
22
+ fs.writeFileSync(fileName, JSON.stringify(existingIndex));
23
+ const newIndex = {
24
+ pages: [
25
+ { title: "Current title", url: "current.html", time: "" },
26
+ { title: "New page", url: "new.html", time: "" }
27
+ ],
28
+ words: {}
29
+ };
30
+ const visitor = { index: newIndex };
31
+ const context = {
32
+ setVar() { },
33
+ log() { },
34
+ warn() { }
35
+ };
36
+ await new SearchIndexStep(fileName, visitor).execute(context);
37
+ const savedIndex = JSON.parse(fs.readFileSync(fileName, "utf8"));
38
+ expect(savedIndex.pages).toEqual([
39
+ { title: "Current title", url: "current.html", time: "" },
40
+ { title: "New page", url: "new.html", time: "" }
41
+ ]);
42
+ }
43
+ finally {
44
+ fs.rmSync(root, { recursive: true, force: true });
45
+ }
46
+ });
47
+ });
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@rr0/cms",
3
3
  "type": "module",
4
4
  "author": "Jérôme Beau <rr0@rr0.org> (https://rr0.org)",
5
- "version": "0.3.58",
5
+ "version": "0.3.59",
6
6
  "description": "RR0 Content Management System (CMS)",
7
7
  "exports": "./dist/index.js",
8
8
  "types": "./dist/index.d.ts",