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.
41 lines
1.1 KiB
41 lines
1.1 KiB
3 years ago
|
// 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)
|
||
|
})
|
||
|
})
|
||
|
}
|
||
|
|
||
|
// 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))
|
||
|
}
|