Skip to content

Commit

Permalink
Merge 7551266 into 768de8c
Browse files Browse the repository at this point in the history
  • Loading branch information
weareoutman committed Oct 29, 2020
2 parents 768de8c + 7551266 commit c95f705
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
12 changes: 11 additions & 1 deletion src/server/utils/getIndexHash.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof klawSync>).mockImplementation(
(root, options) => {
let files: string[] = [];
Expand Down Expand Up @@ -47,7 +57,7 @@ const mockConsoleWarn = jest
describe("getIndexHash", () => {
test.each<[Partial<ProcessedPluginOptions>, 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" },
Expand Down
19 changes: 18 additions & 1 deletion src/server/utils/getIndexHash.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit c95f705

Please sign in to comment.