"Linted progress so far on bookmark watcher"

This commit is contained in:
Cris Stringfellow 2021-12-31 14:32:02 +08:00
parent 485adf7a10
commit 5d0016d258
2 changed files with 68 additions and 19 deletions

View File

@ -1,13 +1,16 @@
module.exports = { module.exports = {
"env": { "env": {
"es2021": true, "es2021": true,
"node": true "node": true
}, },
"extends": "eslint:recommended", "extends": "eslint:recommended",
"parserOptions": { "parserOptions": {
"ecmaVersion": 13, "ecmaVersion": 13,
"sourceType": "module" "sourceType": "module"
}, },
"rules": { "ignorePatterns": [
} "build/**/*.js"
],
"rules": {
}
}; };

View File

@ -26,12 +26,21 @@ const PLAT_TABLE = {
'darwin': 'macos', 'darwin': 'macos',
'linux': 'nix' 'linux': 'nix'
}; };
const PROFILE_REGEX = /^(Default|Profile \d+)$/i; //const PROFILE_DIR_NAME_REGEX = /^(Default|Profile \d+)$/i;
const isProfile = name => PROFILE_REGEX.test(name); //const isProfileDir = name => PROFILE_DIR_NAME_REGEX.test(name);
const BOOKMARK_FILE_NAME_REGEX = /^(Bookmark|Bookmark.bak)/i;
const isBookmarkFile = name => BOOKMARK_FILE_NAME_REGEX.test(name);
//const addWatchers = [];
//const deleteWatchers = [];
findDefaultChromeProfile(); test();
async function test() {
for await ( const event of watchBookmarks() ) {
console.log({event});
}
}
function findDefaultChromeProfile() { export function watchBookmarks() {
const rootDir = getProfileRootDir(); const rootDir = getProfileRootDir();
if ( !fs.existsSync(rootDir) ) { if ( !fs.existsSync(rootDir) ) {
@ -43,28 +52,63 @@ function findDefaultChromeProfile() {
path.resolve(rootDir, '**', 'book*') path.resolve(rootDir, '**', 'book*')
]; ];
console.log({bookmarkWatchGlobs}); DEBUG && console.log({bookmarkWatchGlobs});
const notify = notifier();
const observer = watch(bookmarkWatchGlobs, CHOK_OPTS); const observer = watch(bookmarkWatchGlobs, CHOK_OPTS);
observer.on('ready', () => { observer.on('ready', () => {
console.log(`Ready to watch`); DEBUG && console.log(`Ready to watch`);
}); });
observer.on('all', (event, path) => { observer.on('all', (event, path) => {
console.log(event, path); DEBUG && console.log(event, path);
const name = path.basename(path);
if ( isBookmarkFile(name) ) {
notify.next({event, path});
}
}); });
observer.on('error', error => { observer.on('error', error => {
console.warn(`Watcher error`, error); console.warn(`Bookmark file watcher error`, error);
}); });
process.on('SIGINT', shutdown); process.on('SIGINT', shutdown);
process.on('SIGHUP', shutdown); process.on('SIGHUP', shutdown);
process.on('SIGUSR1', shutdown); process.on('SIGUSR1', shutdown);
return notify;
async function* notifier() {
while(true) {
// change is pushed in
const change = yield;
// change is taken out
yield change;
}
}
async function shutdown() { async function shutdown() {
console.log('Shutdown'); console.log('Shutdown');
await observer.close(); await observer.close();
console.log('No longer observing.'); console.log('No longer observing.');
} }
/*
function onAddBookmark(func) {
if ( typeof func !== "function" ) {
throw new TypeError(`Only functions can be added to listen to the 'AddBookmark' event`);
}
addWatchers.push(func);
}
function onDeleteBookmark(func) {
if ( typeof func !== "function" ) {
throw new TypeError(`Only functions can be added to listen to the 'DeleteBookmark' event`);
}
deleteWatchers.push(func);
}
*/
} }
function getProfileRootDir() { function getProfileRootDir() {
@ -108,6 +152,7 @@ function getProfileRootDir() {
return rootDir; return rootDir;
} }
/*
function* profileDirectoryEnumerator(maxN = 9999) { function* profileDirectoryEnumerator(maxN = 9999) {
let index = 0; let index = 0;
while(index <= maxN) { while(index <= maxN) {
@ -115,3 +160,4 @@ function* profileDirectoryEnumerator(maxN = 9999) {
yield profileDirName; yield profileDirName;
} }
} }
*/