XeNote/lib/node.js

62 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

2020-11-28 15:45:01 +00:00
import path from 'path'
import fs from "fs"
2024-01-14 05:26:21 +00:00
class Node {
_markdownFolder;
_filesList;
2023-12-26 04:03:37 +00:00
2024-01-14 05:26:21 +00:00
isFile = (filename) => {
try {
return fs.existsSync(filename)
} catch (err) {
console.error(err)
return false
}
2023-12-26 04:03:37 +00:00
}
2024-01-14 05:26:21 +00:00
getFullPath = (folderPath) => {
return fs.readdirSync(folderPath).map(fn => path.join(folderPath, fn))
2023-12-26 04:03:37 +00:00
}
2024-01-14 05:26:21 +00:00
getFiles = (dir) => {
if (this._filesList) return this._filesList
var results = [];
var list = fs.readdirSync(dir);
2024-01-14 05:26:21 +00:00
const outterThis = this
list.forEach(function(file) {
file = dir + '/' + file;
var stat = fs.statSync(file);
if (stat && stat.isDirectory()) {
/* Recurse into a subdirectory */
2024-01-14 05:26:21 +00:00
results = results.concat(outterThis.getFiles(file));
} else {
/* Is a file */
results.push(file);
}
});
2024-01-14 05:26:21 +00:00
const filtered = results.filter(f => f.endsWith(".md"))
this._filesList = filtered
return this._filesList
2023-12-26 04:03:37 +00:00
}
2024-01-14 05:26:21 +00:00
readFileSync = (fullPath) => {
return fs.readFileSync(fullPath, "utf8")
2023-12-26 04:03:37 +00:00
}
2020-11-30 11:29:34 +00:00
2024-01-14 05:26:21 +00:00
getMarkdownFolder = () => {
if (this._markdownFolder) return this._markdownFolder
const notesDir = path.join(process.cwd(), 'notes')
2024-01-14 05:26:21 +00:00
if (!this.isFile(notesDir)) {
2023-12-26 01:50:08 +00:00
console.warn("Notes Directory does not seem to exist: ", notesDir)
}
2024-01-14 05:26:21 +00:00
this._markdownFolder = notesDir
return this._markdownFolder
}
}
2024-01-14 05:26:21 +00:00
const node = new Node()
export default node