Skip to content

Commit

Permalink
fix: actually handle multiple files; use logger for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Chickensoupwithrice committed Sep 15, 2023
1 parent 47a3ba9 commit 8b4adb3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion crawler.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ self.__bx_behaviors.selectMainBehavior();
async loadCustomBehaviors(filename) {
let str = "";

for (const source of await determineFileSource(filename, ".js")) {
for (const source of await determineFileSource(filename, ".js", logger)) {
str += `self.__bx_behaviors.load(${source});\n`;
}

Expand Down
16 changes: 8 additions & 8 deletions util/file_reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@ import crypto from "crypto";

const MAX_DEPTH = 2;

export async function determineFileSource(fileOrUrl, ext = null) {
export async function determineFileSource(fileOrUrl, ext = null, logger) {
if (typeof(fileOrUrl) === "string") {
if (fileOrUrl.startsWith("http")) {
return await collectOnlineFileSource(fileOrUrl);
return await collectOnlineFileSource(fileOrUrl, logger);
} else {
return collectAllFileSources(fileOrUrl, ext);
}
} else if (typeof(fileOrUrl) === "object") {
const returnArray = [];

Check failure on line 15 in util/file_reader.js

View workflow job for this annotation

GitHub Actions / lint (18.x)

Expected indentation of 4 spaces but found 2 tabs
for (const f of fileOrUrl) {
return await determineFileSource(f, null);
returnArray.concat(await determineFileSource(f, ext, logger));
}
return returnArray;

Check failure on line 19 in util/file_reader.js

View workflow job for this annotation

GitHub Actions / lint (18.x)

Expected indentation of 4 spaces but found 2 tabs
}
}

export async function collectOnlineFileSource(url) {
export async function collectOnlineFileSource(url, logger) {
const filename = crypto.randomBytes(4).toString("hex") + ".js";
await fetch(url)
.then(res => res.text())
.then(file => {return fs.promises.writeFile("/app/behaviors/" + filename, file);})
.then(() => {
console.log("done");
}).catch(err => {
console.log(err);
.catch(err => {
logger.error(err);
});
return collectAllFileSources("/app/behaviors", ".js");
}
Expand Down

0 comments on commit 8b4adb3

Please sign in to comment.