XeNote/lib/node.js

62 lines
1.4 KiB
JavaScript

import path from 'path'
import fs from "fs"
class Node {
_markdownFolder;
_filesList;
isFile = (filename) => {
try {
return fs.existsSync(filename)
} catch (err) {
console.error(err)
return false
}
}
getFullPath = (folderPath) => {
return fs.readdirSync(folderPath).map(fn => path.join(folderPath, fn))
}
getFiles = (dir) => {
if (this._filesList) return this._filesList
var results = [];
var list = fs.readdirSync(dir);
const outterThis = this
list.forEach(function(file) {
file = dir + '/' + file;
var stat = fs.statSync(file);
if (stat && stat.isDirectory()) {
/* Recurse into a subdirectory */
results = results.concat(outterThis.getFiles(file));
} else {
/* Is a file */
results.push(file);
}
});
const filtered = results.filter(f => f.endsWith(".md"))
this._filesList = filtered
return this._filesList
}
readFileSync = (fullPath) => {
return fs.readFileSync(fullPath, "utf8")
}
getMarkdownFolder = () => {
if (this._markdownFolder) return this._markdownFolder
const notesDir = path.join(process.cwd(), 'notes')
if (!this.isFile(notesDir)) {
console.warn("Notes Directory does not seem to exist: ", notesDir)
}
this._markdownFolder = notesDir
return this._markdownFolder
}
}
const node = new Node()
export default node