UNPKG

ikea-availability-checker

Version:

ikea product in-store availability checker and product search

142 lines (133 loc) 5.21 kB
#!/usr/bin/env node "use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); const program_1 = require("./lib/program"); const arguments_1 = require("./lib/arguments"); const options = __importStar(require("./lib/options")); const __1 = require(".."); const stores_1 = require("../lib/stores"); const stockTable_1 = require("./reporter/stockTable"); program_1.program .addArgument(arguments_1.productIds.argRequired()) .addOption(options.country) .addOption(options.plain) .addOption(options.pretty) .addOption(options.json) .addOption(options.store) .addOption(options.minStock) .description("Can be used to request the availability of one or multiple products " + "in specific countries and/or stores. Use the options to filter the " + "results.") .addHelpText("after", ` Examples: query single product in single store ikea-availability-checker stock --store 174 40299687 query multiple products in a single store ikea-availability-checker stock --store 174 40299687 S69022537 query single product in multiple stores ikea-availability-checker stock --store 174,328 40299687 query single product in all stores in a country ikea-availability-checker stock --country=at 40299687 query single product by matching city name ikea-availability-checker stock --store Berlin 40299687 show only stores with min 2 items in stock ikea-availability-checker stock --min-store 2 --store Berlin 40299687 output as json ikea-availability-checker stock --store 148 --json 40299687 output with aligned columns ikea-availability-checker stock --store Frankfurt --plain 40299687 | column -t `) .action(async (productIds) => { const opts = program_1.program.opts(); let format = "table"; if (opts.json) format = "json"; if (opts.plain) format = "tsv"; if (opts.pretty) format = "table"; let stores; if (!opts.store && opts.country) { stores = (0, stores_1.findByCountryCode)(opts.country); } else if (Array.isArray(opts.store)) { stores = (0, stores_1.findById)(opts.store); } else if (opts.store) { stores = (0, stores_1.findByQuery)(opts.store); } else { throw new Error("please provide country code and/or store id"); } const data = await (0, __1.availabilities)(stores, productIds); // only show items with at least "amount" items in stock const displayedData = data.filter((availability) => availability.stock >= opts.minStock); let report; switch (format) { case "json": report = JSON.stringify(displayedData); break; case "tsv": report = displayedData .map((availability) => { var _a, _b; return [ (_a = availability.createdAt) === null || _a === void 0 ? void 0 : _a.toISOString(), availability.productId, availability.store.countryCode, availability.store.country, availability.store.buCode, availability.store.name, availability.stock, availability.probability, (_b = availability.restockDate) === null || _b === void 0 ? void 0 : _b.toISOString(), ].join("\t"); }) .join("\n"); break; default: case "table": report = (0, stockTable_1.createStockInfoReportTable)(displayedData).toString(); break; } process.stdout.write(report); }); program_1.program.parseAsync() .catch((err) => { const message = err instanceof Error ? err.message : String(err); process.stderr.write(message + "\n"); process.exit(1); });