You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.0 KiB
JavaScript

"use strict";
const chalk = require("chalk");
const util = require("util");
const itemHasPartialData = require("./item-has-partial-data");
function concatenateArguments(args) {
let stringified = args.map((arg) => {
if (typeof arg === "string") {
return arg;
} else {
return util.inspect(arg, {depth: null, colors: false});
}
});
return stringified.join(" ");
}
module.exports = function createScrapeLogger({name}) {
return {
log: function log(message) {
console.log(`${chalk.bold.gray(`[${name}]`)} ${message}`);
},
warning: function logWarning(...args) {
let message = concatenateArguments(args);
this.log(`${chalk.bold.yellow("[⚠ WARNING]")} ${message}`);
},
error: function logError(...args) {
let message = concatenateArguments(args);
this.log(`${chalk.bold.red("[✖ ERROR ]")} ${message}`);
},
info: function logInfo(...args) {
let message = concatenateArguments(args);
this.log(`${chalk.bold.cyan("[* info ]")} ${message}`);
},
done: function done(item) {
let {data} = item;
let title, sku, partial;
if (data.brand != null && data.model != null) {
title = `${data.brand} ${data.model}`;
} else if (data.brand != null) {
title = `${data.brand} ${data.title}`;
} else {
title = data.title;
}
if (data.sku != null) {
sku = `(SKU: ${data.sku}) `;
} else {
sku = "";
}
let containsPartialData = itemHasPartialData(item);
if (containsPartialData) {
partial = chalk.cyan("[partial] ");
} else {
partial = "";
}
let message = `${sku}${partial}${title}`;
this.log(`${chalk.bold.green("[✔ done ]")} ${message}`);
if (data.downloads != null) {
data.downloads.forEach((download) => {
this.log(`${chalk.bold.green(" |")} (${download.language}) ${download.description} :: ${download.url}`);
});
}
},
debug: function debug(...args) {
if (process.env.SCRAPER_DEBUG === "1") {
let message = concatenateArguments(args);
this.log(chalk.gray(message));
}
}
};
};