-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add check command #3
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { jest, describe, test, expect } from '@jest/globals' | ||
import { generateImportBookmarkMarkup } from '../generate-bookmarks.js'; | ||
import { checkBookmarkBody } from '../check-bookmarks.js'; | ||
|
||
describe("Checking if bookmarks are up to date", () => { | ||
describe("checkBookmarkBody", () => { | ||
test("Should return true for same config in different dates", () => { | ||
const config = { | ||
label: 'Bookmarks config', | ||
description: 'Bookmarks config description', | ||
bookmarks: [{ | ||
label: 'Link', | ||
description: 'Bookmark', | ||
href: 'https://www.testlink.com' | ||
}] | ||
}; | ||
Date.now = jest.fn(() => 1487076708000); | ||
const originalGeneratedBookmark = generateImportBookmarkMarkup(config); | ||
Date.now = jest.fn(() => 1487076709000); | ||
const laterGeneratedBookmark = generateImportBookmarkMarkup(config); | ||
for (let i = 0; i < originalGeneratedBookmark.length; i++) { | ||
expect(checkBookmarkBody(originalGeneratedBookmark[i].body, laterGeneratedBookmark[i].body)).toBeTruthy; | ||
} | ||
}); | ||
test("Should return false for different configs", () => { | ||
const config1 = { | ||
label: 'Bookmarks config 1', | ||
description: 'Bookmarks config description', | ||
bookmarks: [{ | ||
label: 'Link', | ||
description: 'Bookmark 1', | ||
href: 'https://www.testlink.com' | ||
}] | ||
}; | ||
const config2 = { | ||
label: 'Bookmarks config 2', | ||
description: 'Bookmarks config description', | ||
bookmarks: [{ | ||
label: 'Link', | ||
description: 'Bookmark 2', | ||
href: 'https://www.testlink.com' | ||
}] | ||
}; | ||
Date.now = jest.fn(() => 1487076708000); | ||
const generatedBookmark1 = generateImportBookmarkMarkup(config1); | ||
const generatedBookmark2 = generateImportBookmarkMarkup(config2); | ||
for (let i = 0; i < generatedBookmark1.length; i++) { | ||
expect(checkBookmarkBody(generatedBookmark1[i].body, generatedBookmark2[i].body)).toBeFalsy; | ||
} | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { fetchBookmarkConfig } from "./load-bookmarks.js"; | ||
import { readFileSync } from "fs"; | ||
import { trimTrailingSlash } from "./save-bookmarks.js"; | ||
import { generateImportBookmarkMarkup } from "./generate-bookmarks.js" | ||
|
||
const checkBookmarks = async (path, directory) => { | ||
const { type, body } = await fetchBookmarkConfig(path); | ||
console.log(`Fetching ${type} bookmarks from ${path}`); | ||
const generatedBookmarks = generateImportBookmarkMarkup(body); | ||
return generatedBookmarks.every((bookmark) => checkSingleBookmark(bookmark, directory)); | ||
}; | ||
|
||
const sanitizeDynamicData = (bookmarkBody) => { | ||
// Sanitize dates | ||
const dateRegexes = [ | ||
/(ADD_DATE=")\d+(")/g, | ||
/(LAST_MODIFIED=")\d+(")/g, | ||
/(last updated on ).*( using \[Bookworms\])/g | ||
] | ||
return dateRegexes.reduce((body, regex) => body.replaceAll(regex, "$10$2"), bookmarkBody); | ||
} | ||
|
||
const checkBookmarkBody = (existingBookmarkBody, generatedBookmarkBody) => { | ||
return sanitizeDynamicData(existingBookmarkBody) === sanitizeDynamicData(generatedBookmarkBody); | ||
} | ||
|
||
const checkSingleBookmark = (generatedBookmark, directory) => { | ||
const filename = `${trimTrailingSlash(directory)}/${generatedBookmark.filename}`; | ||
let existingBookmark = null; | ||
try { | ||
existingBookmark = readFileSync(filename, 'utf8'); | ||
} catch (e) { | ||
console.log(`Failed to read file ${filename}`); | ||
return false; | ||
} | ||
return checkBookmarkBody(existingBookmark, generatedBookmark.body); | ||
} | ||
|
||
export { checkBookmarks, checkSingleBookmark, sanitizeDynamicData, checkBookmarkBody }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,13 @@ import * as generateBookmarks from "./generate-bookmarks.js"; | |
import * as saveBookmarks from "./save-bookmarks.js"; | ||
import * as mergeBookmarks from "./merge-bookmarks.js"; | ||
import * as convertBookmarks from "./convert-bookmarks.js"; | ||
import * as checkBookmarks from "./check-bookmarks.js"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should also add this to the developer readme my to explain how the functions can be used to be consistent with other modules. Its just a quick note for people who might want to import into other projects There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added documentation. |
||
|
||
export { | ||
loadBookmarks, | ||
generateBookmarks, | ||
saveBookmarks, | ||
mergeBookmarks, | ||
convertBookmarks, | ||
checkBookmarks, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if its worth documenting here why you might want to use this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added some documentation, hope that's what you meant.