XeNote/lib/node.js

44 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-11-28 15:45:01 +00:00
import path from 'path'
import fs from "fs"
export const Node = {
isFile: function(filename) {
try {
return fs.existsSync(filename)
} catch (err) {
console.error(err)
return false
}
},
getFullPath: function(folderPath) {
return fs.readdirSync(folderPath).map(fn => path.join(folderPath, fn))
},
getFiles: function(dir) {
var results = [];
var list = fs.readdirSync(dir);
list.forEach(function(file) {
file = dir + '/' + file;
var stat = fs.statSync(file);
if (stat && stat.isDirectory()) {
/* Recurse into a subdirectory */
results = results.concat(Node.getFiles(file));
} else {
/* Is a file */
results.push(file);
}
});
return results.filter(f => f.endsWith(".md"))
},
readFileSync: function(fullPath) {
return fs.readFileSync(fullPath, "utf8")
},
2020-11-30 11:29:34 +00:00
getMarkdownFolder: function() {
const notesDir = path.join(process.cwd(), 'notes')
if (!Node.isFile(notesDir)) throw new Error("The notes directory has not been created yet")
return notesDir
}
}