Skip to content
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

Merged
merged 3 commits into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
generateBookmarks,
mergeBookmarks,
convertBookmarks,
checkBookmarks,
} from "../src/index.js";

const getCommand = {
Expand Down Expand Up @@ -66,6 +67,23 @@ const convertCommand = {
},
};

const checkCommand = {
command: "check <bookmarks>",
desc: "Local path of exported browser bookmarks",
handler: async (argv) => {
console.log("CHECKING BOOKMARKS");
const checkResult = await checkBookmarks.checkBookmarks(
argv.bookmarks,
argv.directory
);
if (!checkResult) {
console.log("Bookmarks are not up to date");
process.exit(1);
}
console.log("Bookmarks are up to date");
},
};

yargs(hideBin(process.argv))
.option("d", {
alias: "directory",
Expand All @@ -81,4 +99,5 @@ yargs(hideBin(process.argv))
.command(getCommand)
.command(mergeCommand)
.command(convertCommand)
.command(checkCommand)
.help().argv;
14 changes: 14 additions & 0 deletions docs/ADVANCED-USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,17 @@ $ npx convert ./browsers.html -f="./my-bookmarks.yaml"
$ ls
my-bookmarks.yaml
```

## Check

Check whether current generated files are up to date with the yaml file.
Copy link
Collaborator

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?

Copy link
Author

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.

This can be used either in a git hook or in your CI to make sure the output files have been changed after a change to the yaml file.

The `check` command works in a similar way to `get` with the same options.

```BASH
$ npx bookworms check ./my-bookmarks.yaml
```

This will print whether the generated bookmark files are up to date.
The process will also exit with exit code 0 if the files are up to date, and 1 if not.
9 changes: 9 additions & 0 deletions docs/DEVELOPERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ A collection of helpers for converting browsers `HTML` export of bookmarks into
- `convertHTMLtoYAML(path)` - Pass `HTML` to this function and it will return `YAML` that matches Bookmarks schema
- `createBookmarks(path, directory, filename)` - Generate `YAML` from browsers `HTML` export of bookmarks and store to disk based on option

#### `checkBoomkarks`

A collection of helpers for checking whether the output files are up to date with the yaml file.

- `checkBookmarks(path, directory)` - load bookmarks from `path`, and check whether the files generated from it are equivalent to the output files residing in `directory`
- `sanitizeDynamicData(bookmarkBody)` - sanitizes dynamic data from the bookmark body, to be clean of data which is generated dynamically unrelated to the yaml. Currently, the dynamic data in the output is creation dates.
- `checkBookmarkBody(existingBookmarkBody, generatedBookmarkBody)` - returns whether 2 output bookmarks files are equivalent (equal except dynamic data).
- `checkSingleBookmark(generatedBookmark, directory)` - checks whether a bookmark object (with a `filename` and `body`) is equivalent to the output bookmark file with the same name in `directory`.

## Contributing to Bookworms

If you want to add features to Bookworms you will need to know how to work with the project.
Expand Down
52 changes: 52 additions & 0 deletions src/_tests_/check-bookmarks.test.js
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;
}
});
});
});
4 changes: 4 additions & 0 deletions src/_tests_/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
saveBookmarks,
mergeBookmarks,
convertBookmarks,
checkBookmarks,
} from "../index";

describe("Checking modules are exported", () => {
Expand All @@ -22,4 +23,7 @@ describe("Checking modules are exported", () => {
test("should return convertBookmarks", () => {
expect(convertBookmarks).toBeDefined();
});
test("should return checkBookmarks", () => {
expect(checkBookmarks).toBeDefined();
});
});
39 changes: 39 additions & 0 deletions src/check-bookmarks.js
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 };
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Copy link
Collaborator

@robinglen robinglen Jan 12, 2022

Choose a reason for hiding this comment

The 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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added documentation.


export {
loadBookmarks,
generateBookmarks,
saveBookmarks,
mergeBookmarks,
convertBookmarks,
checkBookmarks,
};