-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathloader.mjs
More file actions
56 lines (46 loc) · 1.62 KB
/
loader.mjs
File metadata and controls
56 lines (46 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict';
import { readFile } from 'node:fs/promises';
import { extname } from 'node:path';
import { globSync } from 'glob';
import { VFile } from 'vfile';
import {
createProgressBar,
startProgressBar,
updateProgressBar,
stopProgressBar,
} from './utils/progressBar.mjs';
/**
* This method creates a simple abstract "Loader", which technically
* could be used for different things, but here we want to use it to load
* Markdown files and transform them into VFiles
*/
const createLoader = () => {
/**
* Loads API Doc files and transforms it into VFiles
*
* @param {string} searchPath A glob/path for API docs to be loaded
* The input string can be a simple path (relative or absolute)
* The input string can also be any allowed glob string
*
* @see https://code.visualstudio.com/docs/editor/glob-patterns
*/
const loadFiles = searchPath => {
const resolvedFiles = globSync(searchPath).filter(
filePath => extname(filePath) === '.md'
);
const progressBar = createProgressBar('Loading files');
startProgressBar(progressBar, resolvedFiles.length);
return resolvedFiles.map(async filePath => {
const fileContents = await readFile(filePath, 'utf-8');
updateProgressBar(progressBar);
// normally we stop the progress bar when the loop is done
// but here we return the loop so we need to stop it when the last file is loaded
if (progressBar.value === progressBar.total) {
stopProgressBar(progressBar);
}
return new VFile({ path: filePath, value: fileContents });
});
};
return { loadFiles };
};
export default createLoader;