You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.4 KiB
60 lines
1.4 KiB
const path = require("path");
|
|
const fs = require("fs");
|
|
|
|
function loadTags(directoryPath) {
|
|
return new Promise((a, r) =>
|
|
fs.readFile(
|
|
path.join(directoryPath, "meta.json"),
|
|
"utf8",
|
|
async (err, data) => {
|
|
if (err) {
|
|
if (!err.message.includes("ENOENT")) return r(err);
|
|
await saveTags(directoryPath, {});
|
|
}
|
|
|
|
const tagCache = JSON.parse(data);
|
|
a(tagCache);
|
|
}
|
|
)
|
|
);
|
|
}
|
|
|
|
function saveTags(directoryPath, tags = {}) {
|
|
return new Promise((a, r) =>
|
|
fs.writeFile(
|
|
path.join(directoryPath, "meta.json"),
|
|
JSON.stringify(tags),
|
|
"utf8",
|
|
err => {
|
|
if (err) return r(err);
|
|
a({});
|
|
}
|
|
)
|
|
);
|
|
}
|
|
|
|
async function updateTagsForNote(directoryPath, { title, tags = [] }) {
|
|
if (!title || title === "") {
|
|
return Promise.reject();
|
|
}
|
|
|
|
const allTags = await loadTags(directoryPath);
|
|
allTags[title] = [...tags];
|
|
await saveTags(directoryPath, allTags);
|
|
}
|
|
|
|
async function getTagsForNote(directoryPath, { title }) {
|
|
if (!title || title === "") {
|
|
return Promise.reject();
|
|
}
|
|
|
|
const allTags = await loadTags(directoryPath);
|
|
return allTags[title];
|
|
}
|
|
|
|
module.exports = {
|
|
loadTags,
|
|
getTagsForNote,
|
|
updateTagsForNote
|
|
};
|