-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
32 lines (27 loc) · 866 Bytes
/
utils.js
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
const fs = require('fs');
const util = require('util');
const fetch = require('cross-fetch');
// utils.js
class Utils {
constructor(errorMessage) {
this.errorMessage = errorMessage;
console.log(this.errorMessage);
}
writeFile = util.promisify(fs.writeFile);
throwErrorIfNull(value) {
if (value === null || value === undefined) {
throw new Error(this.errorMessage);
}
}
async createFileFromUrl(url, filePath) {
try {
const response = await fetch(url);
const buffer = await response.buffer();
await this.writeFile(filePath, buffer);
console.log(`File ${filePath} created successfully from ${url}`);
} catch (error) {
console.error(`Error creating file from ${url}: ${error}`);
}
}
}
module.exports = Utils;