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.
50 lines
1.3 KiB
50 lines
1.3 KiB
// 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)) |
|
} |
|
|
|
// This might also return true for folders, plz fix |
|
export function isFile(path) { |
|
return fs.existsSync(path) |
|
} |