-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathutils.js
More file actions
34 lines (31 loc) · 902 Bytes
/
utils.js
File metadata and controls
34 lines (31 loc) · 902 Bytes
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
import path from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs';
/**
* Throws an error if any of the keys are missing from the object
* @param {*} obj
* @param {string[]} keys
* @throws {Error}
*/
export function throwIfMissing(obj, keys) {
const missing = [];
for (let key of keys) {
if (!(key in obj) || !obj[key]) {
missing.push(key);
}
}
if (missing.length > 0) {
throw new Error(`Missing required fields: ${missing.join(', ')}`);
}
}
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const staticFolder = path.join(__dirname, '../static');
/**
* Returns the contents of a file in the static folder
* @param {string} fileName
* @returns {string} Contents of static/{fileName}
*/
export function getStaticFile(fileName) {
return fs.readFileSync(path.join(staticFolder, fileName)).toString();
}