|
|
|
// Helpers for filesystem / folder manipulations and loading files
|
|
|
|
import fs from 'fs'
|
|
|
|
import readYamlAndMarkdown from 'yaml-head-loader'
|
|
|
|
|
|
|
|
// Loads the text of a file
|
|
|
|
async function loadTextFile(path) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fs.readFile(path, 'utf8', function(err, data) {
|
|
|
|
if (err) {
|
|
|
|
console.log('Reading file failed:', err)
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
resolve(data)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function loadJsonFile(path) {
|
|
|
|
const loadedText = await loadTextFile(path)
|
|
|
|
return JSON.parse(loadedText)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loads the given text file and returns a dictionary of its contents parsed into a .header dict and .tail markdown content
|
|
|
|
export async function loadYamlMarkdownFile(path) {
|
|
|
|
let text = await loadTextFile(path)
|
|
|
|
if(!text) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
let dict = readYamlAndMarkdown(text)
|
|
|
|
return dict
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loads and returns the list of contents of a folder as an array
|
|
|
|
export function lsFolder(path) {
|
|
|
|
try {
|
|
|
|
return fs.readdirSync(path).filter(fileOrFolderName => fileOrFolderName.length >= 1 && fileOrFolderName[0] !== '.')
|
|
|
|
} catch(err) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if the path is a folder
|
|
|
|
export function isFolder(path) {
|
|
|
|
return Array.isArray(lsFolder(path))
|
|
|
|
}
|