From 75512665713733199f85b51530c017ac86f24885 Mon Sep 17 00:00:00 2001 From: weareoutman Date: Thu, 29 Oct 2020 12:39:56 +0800 Subject: [PATCH] fix: refine index hash --- package.json | 1 + src/server/utils/getIndexHash.spec.ts | 12 +++++++++++- src/server/utils/getIndexHash.ts | 19 ++++++++++++++++++- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index ec91ef35..0bd93503 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,7 @@ { "name": "@easyops-cn/docusaurus-search-local", "version": "0.9.3", + "description": "An offline/local search plugin for Docusaurus v2.", "repository": "https://github.com/easyops-cn/docusaurus-search-local", "homepage": "https://github.com/easyops-cn/docusaurus-search-local", "scripts": { diff --git a/src/server/utils/getIndexHash.spec.ts b/src/server/utils/getIndexHash.spec.ts index 6f1273b5..ee4478fe 100644 --- a/src/server/utils/getIndexHash.spec.ts +++ b/src/server/utils/getIndexHash.spec.ts @@ -9,6 +9,16 @@ const mockConsoleWarn = jest .spyOn(console, "warn") .mockImplementation(() => void 0); +jest.mock( + "../../../../package.json", + () => ({ + version: "0.0.0", + }), + { + virtual: true, + } +); + (klawSync as jest.MockedFunction).mockImplementation( (root, options) => { let files: string[] = []; @@ -47,7 +57,7 @@ const mockConsoleWarn = jest describe("getIndexHash", () => { test.each<[Partial, string | null, number]>([ [{ hashed: false }, null, 0], - [{ hashed: true, indexDocs: true, docsDir: "/tmp/docs" }, "a387bd69", 0], + [{ hashed: true, indexDocs: true, docsDir: "/tmp/docs" }, "87def35c", 0], [{ hashed: true, indexBlog: true, blogDir: "/tmp/blog" }, null, 0], [ { hashed: true, indexDocs: true, docsDir: "/does-not-exist/docs" }, diff --git a/src/server/utils/getIndexHash.ts b/src/server/utils/getIndexHash.ts index 26d9d88b..4a1b5340 100644 --- a/src/server/utils/getIndexHash.ts +++ b/src/server/utils/getIndexHash.ts @@ -1,7 +1,9 @@ import fs from "fs"; +import path from "path"; import crypto from "crypto"; import klawSync from "klaw-sync"; import { ProcessedPluginOptions } from "../../shared/interfaces"; +import { debugInfo } from "./debug"; export function getIndexHash(config: ProcessedPluginOptions): string | null { if (!config.hashed) { @@ -35,10 +37,25 @@ export function getIndexHash(config: ProcessedPluginOptions): string | null { if (files.length > 0) { const md5sum = crypto.createHash("md5"); + + // The version of this plugin should be counted in hash, + // since the index maybe changed between versions. + + // eslint-disable-next-line @typescript-eslint/no-var-requires + const pluginVersion = require(path.resolve( + __dirname, + "../../../../package.json" + )).version; + debugInfo("using @easyops-cn/docusaurus-search-local v%s", pluginVersion); + md5sum.update(pluginVersion, "utf8"); + for (const item of files) { md5sum.update(fs.readFileSync(item.path)); } - return md5sum.digest("hex").substring(0, 8); + + const indexHash = md5sum.digest("hex").substring(0, 8); + debugInfo("the index hash is %j", indexHash); + return indexHash; } return null; }