Skip to content

Commit

Permalink
refactor: migrate to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
timbru31 committed Aug 16, 2022
1 parent 6feff96 commit cb861d2
Show file tree
Hide file tree
Showing 37 changed files with 742 additions and 170 deletions.
568 changes: 559 additions & 9 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
{
"name": "stockshock",
"version": "5.13.0",
"version": "6.0.0",
"private": true,
"description": "A simple bot for MediaMarkt and Saturn to check if tracked items are available",
"main": "dist/index.js",
"exports": "./dist/index.js",
"scripts": {
"build": "tsc",
"start:dev": "ts-node src/index.ts",
"start:dev": "ts-node --esm src/index.ts",
"prestart": "npm run build",
"start": "node dist/src/index.js",
"lint": "npm test",
"test": "eslint . --ext .ts",
"prepare": "husky install"
"prepare": "husky install",
"postinstall": "patch-package"
},
"repository": "timbru31/mms-stockshock",
"author": "Tim Brust <github@timbrust.de>",
Expand All @@ -21,8 +22,10 @@
"engines": {
"node": ">=16"
},
"type": "module",
"dependencies": {
"@aws-sdk/client-dynamodb": "^3.137.0",
"@colors/colors": "^1.5.0",
"@types/uuid": "^8.3.4",
"date-fns": "^2.29.1",
"discord.js": "^13.8.1",
Expand All @@ -41,7 +44,7 @@
"yargs": "^17.5.1"
},
"devDependencies": {
"@types/inquirer": "^9.0.0",
"@types/inquirer": "^9.0.1",
"@types/user-agents": "^1.0.2",
"@types/ws": "^8.5.3",
"@types/yargs": "^17.0.11",
Expand All @@ -51,6 +54,7 @@
"eslint-plugin-deprecation": "^1.3.2",
"husky": "^8.0.1",
"lint-staged": "^13.0.3",
"patch-package": "^6.4.7",
"prettier": "^2.7.1",
"ts-node": "^10.9.1",
"typescript": "^4.7.4"
Expand Down
13 changes: 13 additions & 0 deletions patches/telegraf+4.8.6.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
diff --git a/node_modules/telegraf/typings/core/types/typegram.d.ts b/node_modules/telegraf/typings/core/types/typegram.d.ts
index 639228d..f58f9a7 100644
--- a/node_modules/telegraf/typings/core/types/typegram.d.ts
+++ b/node_modules/telegraf/typings/core/types/typegram.d.ts
@@ -2,8 +2,6 @@
/// <reference types="node" />
import { Typegram } from 'typegram';
export * from 'typegram/api';
-export * from 'typegram/markup';
-export * from 'typegram/menu-button';
export * from 'typegram/inline';
export * from 'typegram/manage';
export * from 'typegram/message';
20 changes: 10 additions & 10 deletions src/cookies/basket-adder.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import type { SerializableOrJSHandle } from "puppeteer";
import { v4 } from "uuid";
import type { Logger } from "winston";
import type { BrowserManager } from "../core/browser-manager";
import type { CooldownManager } from "../core/cooldown-manager";
import type { DatabaseConnection } from "../databases/database-connection";
import type { AddProductResponse } from "../models/api/add-product-response";
import type { Product } from "../models/api/product";
import type { Notifier } from "../models/notifier";
import type { StoreConfiguration } from "../models/stores/config-model";
import type { Store } from "../models/stores/store";
import { HTTPStatusCode } from "../utils/http";
import { GRAPHQL_CLIENT_VERSION, sleep } from "../utils/utils";
import type { BrowserManager } from "../core/browser-manager.js";
import type { CooldownManager } from "../core/cooldown-manager.js";
import type { DatabaseConnection } from "../databases/database-connection.js";
import type { AddProductResponse } from "../models/api/add-product-response.js";
import type { Product } from "../models/api/product.js";
import type { Notifier } from "../models/notifier.js";
import type { StoreConfiguration } from "../models/stores/config-model.js";
import type { Store } from "../models/stores/store.js";
import { HTTPStatusCode } from "../utils/http.js";
import { GRAPHQL_CLIENT_VERSION, sleep } from "../utils/utils.js";

export class BasketAdder {
private basketProducts = new Map<string, Product>();
Expand Down
20 changes: 11 additions & 9 deletions src/core/browser-manager.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { prompt } from "inquirer";
import inquirer from "inquirer";
import { Server } from "proxy-chain";
import type { Browser, Page, PuppeteerNodeLaunchOptions, SerializableOrJSHandle } from "puppeteer";
import puppeteer from "puppeteer-extra";
import StealthPlugin from "puppeteer-extra-plugin-stealth";
import UserAgent from "user-agents";
import { v4 } from "uuid";
import type { Logger } from "winston";
import type { LoginResponse } from "../models/api/login-response";
import type { Response } from "../models/api/response";
import type { Notifier } from "../models/notifier";
import type { StoreConfiguration } from "../models/stores/config-model";
import type { Store } from "../models/stores/store";
import { HTTPStatusCode } from "../utils/http";
import { GRAPHQL_CLIENT_VERSION, shuffle, sleep } from "../utils/utils";
import type { LoginResponse } from "../models/api/login-response.js";
import type { Response } from "../models/api/response.js";
import type { Notifier } from "../models/notifier.js";
import type { StoreConfiguration } from "../models/stores/config-model.js";
import type { Store } from "../models/stores/store.js";
import { HTTPStatusCode } from "../utils/http.js";
import { GRAPHQL_CLIENT_VERSION, shuffle, sleep } from "../utils/utils.js";

export class BrowserManager {
reLoginRequired = true;
Expand Down Expand Up @@ -48,6 +48,7 @@ export class BrowserManager {
this.proxies = shuffle(this.storeConfiguration.proxy_urls);
}

// @ts-expect-error Foo
puppeteer.use(StealthPlugin());
}

Expand Down Expand Up @@ -170,7 +171,7 @@ export class BrowserManager {
this.reLoginRequired = true;
throw new Error(`Login did not succeed. Status ${res.status}`);
}
await prompt({
await inquirer.prompt({
name: "noop",
message: "Login did not succeed, please check browser for captcha and log in manually. Then hit enter...",
});
Expand Down Expand Up @@ -251,6 +252,7 @@ export class BrowserManager {
args.push(`--proxy-server=${this.storeConfiguration.proxy_url}`);
}

// @ts-expect-error Foo
this.browser = await puppeteer.launch({
headless,
defaultViewport: null,
Expand Down
8 changes: 4 additions & 4 deletions src/core/cooldown-manager.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { add, isAfter, parseISO } from "date-fns";
import { existsSync, readFileSync, writeFileSync } from "fs";
import type { Item } from "../models/api/item";
import type { Product } from "../models/api/product";
import type { NotificationCooldown } from "../models/cooldown";
import { ProductHelper } from "../utils/product-helper";
import type { Item } from "../models/api/item.js";
import type { Product } from "../models/api/product.js";
import type { NotificationCooldown } from "../models/cooldown.js";
import { ProductHelper } from "../utils/product-helper.js";

export class CooldownManager {
private readonly cooldowns = new Map<string, NotificationCooldown>();
Expand Down
2 changes: 1 addition & 1 deletion src/databases/database-connection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Product } from "../models/api/product";
import type { Product } from "../models/api/product.js";

export interface DatabaseConnection {
storeCookies: (product: Product, cookies: string[]) => Promise<void>;
Expand Down
8 changes: 4 additions & 4 deletions src/databases/dynamodb-store.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable @typescript-eslint/naming-convention */
import type { DynamoDBClientConfig, GetItemCommandInput, UpdateItemCommandInput } from "@aws-sdk/client-dynamodb";
import { DynamoDBClient, GetItemCommand, UpdateItemCommand } from "@aws-sdk/client-dynamodb";
import type { Product } from "../models/api/product";
import type { StoreConfiguration } from "../models/stores/config-model";
import type { Store } from "../models/stores/store";
import type { DatabaseConnection } from "./database-connection";
import type { Product } from "../models/api/product.js";
import type { StoreConfiguration } from "../models/stores/config-model.js";
import type { Store } from "../models/stores/store.js";
import type { DatabaseConnection } from "./database-connection.js";

export class DynamoDBStore implements DatabaseConnection {
private readonly store: Store;
Expand Down
26 changes: 13 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import type { Logger } from "winston";
import { BasketAdder } from "./cookies/basket-adder";
import { BrowserManager } from "./core/browser-manager";
import { CooldownManager } from "./core/cooldown-manager";
import type { DatabaseConnection } from "./databases/database-connection";
import { DynamoDBStore } from "./databases/dynamodb-store";
import type { Product } from "./models/api/product";
import type { CliArguments } from "./models/cli";
import type { Notifier } from "./models/notifier";
import { DiscordNotifier, LoggerNotifier, TelegramNotifier, TwitterNotifier, WebSocketNotifier } from "./notifiers";
import { CategoryChecker } from "./stock-checkers/category-checker";
import { WishlistChecker } from "./stock-checkers/wishlist-checker";
import { getStoreAndStoreConfig } from "./utils/cli-helper";
import { createLogger, loadConfig, sleep } from "./utils/utils";
import { BasketAdder } from "./cookies/basket-adder.js";
import { BrowserManager } from "./core/browser-manager.js";
import { CooldownManager } from "./core/cooldown-manager.js";
import type { DatabaseConnection } from "./databases/database-connection.js";
import { DynamoDBStore } from "./databases/dynamodb-store.js";
import type { Product } from "./models/api/product.js";
import type { CliArguments } from "./models/cli.js";
import type { Notifier } from "./models/notifier.js";
import { DiscordNotifier, LoggerNotifier, TelegramNotifier, TwitterNotifier, WebSocketNotifier } from "./notifiers/index.js";
import { CategoryChecker } from "./stock-checkers/category-checker.js";
import { WishlistChecker } from "./stock-checkers/wishlist-checker.js";
import { getStoreAndStoreConfig } from "./utils/cli-helper.js";
import { createLogger, loadConfig, sleep } from "./utils/utils.js";

async function reLoginIfRequired(
browserManager: BrowserManager,
Expand Down
4 changes: 2 additions & 2 deletions src/models/api/add-product-response.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { LineItem } from "./line-item";
import type { Response } from "./response";
import type { LineItem } from "./line-item.js";
import type { Response } from "./response.js";

export interface AddProductResponse extends Response {
data?: {
Expand Down
4 changes: 2 additions & 2 deletions src/models/api/category-response.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ProductLight } from "./product-light";
import type { Response } from "./response";
import type { ProductLight } from "./product-light.js";
import type { Response } from "./response.js";

export interface CategoryResponse extends Response {
data?: {
Expand Down
6 changes: 3 additions & 3 deletions src/models/api/get-basket-response.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Fulfillment } from "./fulfillment";
import type { LineItem } from "./line-item";
import type { Response } from "./response";
import type { Fulfillment } from "./fulfillment.js";
import type { LineItem } from "./line-item.js";
import type { Response } from "./response.js";

export interface GetBasketResponse extends Response {
data?: {
Expand Down
4 changes: 2 additions & 2 deletions src/models/api/item.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Availability } from "./availability";
import type { Product } from "./product";
import type { Availability } from "./availability.js";
import type { Product } from "./product.js";

export interface Item {
product?: Product;
Expand Down
2 changes: 1 addition & 1 deletion src/models/api/login-response.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Response } from "./response";
import type { Response } from "./response.js";

export interface LoginResponse extends Response {
data?: {
Expand Down
2 changes: 1 addition & 1 deletion src/models/api/product-light.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Availability } from "./availability";
import type { Availability } from "./availability.js";

export interface ProductLight {
productId: string;
Expand Down
4 changes: 2 additions & 2 deletions src/models/api/selected-product-response.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Item } from "./item";
import type { Response } from "./response";
import type { Item } from "./item.js";
import type { Response } from "./response.js";

export interface SelectedProductResponse extends Response {
data?: Item;
Expand Down
2 changes: 1 addition & 1 deletion src/models/api/wishlist-item.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Item } from "./item";
import type { Item } from "./item.js";

export interface WishlistItem {
id: string;
Expand Down
4 changes: 2 additions & 2 deletions src/models/api/wishlist-response.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Response } from "./response";
import type { WishlistItem } from "./wishlist-item";
import type { Response } from "./response.js";
import type { WishlistItem } from "./wishlist-item.js";

export interface WishlistResponse extends Response {
data?: {
Expand Down
4 changes: 2 additions & 2 deletions src/models/notifier.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Item } from "./api/item";
import type { Product } from "./api/product";
import type { Item } from "./api/item.js";
import type { Product } from "./api/product.js";

export interface Notifier {
notifyAdmin: (message: string, error?: unknown) => Promise<void>;
Expand Down
2 changes: 1 addition & 1 deletion src/models/stores/abstract-store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Store } from "./store";
import type { Store } from "./store.js";

export abstract class CommonStore implements Store {
private readonly defaultMinSleepTime = 100;
Expand Down
4 changes: 2 additions & 2 deletions src/models/stores/media-markt-austria.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommonStore } from "./abstract-store";
import type { Store } from "./store";
import { CommonStore } from "./abstract-store.js";
import type { Store } from "./store.js";

export class MediaMarktAustria extends CommonStore implements Store {
readonly baseUrl = "https://www.mediamarkt.at";
Expand Down
4 changes: 2 additions & 2 deletions src/models/stores/media-markt-germany.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommonStore } from "./abstract-store";
import type { Store } from "./store";
import { CommonStore } from "./abstract-store.js";
import type { Store } from "./store.js";

export class MediaMarktGermany extends CommonStore implements Store {
readonly baseUrl = "https://www.mediamarkt.de";
Expand Down
4 changes: 2 additions & 2 deletions src/models/stores/media-markt-netherlands.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommonStore } from "./abstract-store";
import type { Store } from "./store";
import { CommonStore } from "./abstract-store.js";
import type { Store } from "./store.js";

export class MediaMarktNetherlands extends CommonStore implements Store {
readonly baseUrl = "https://www.mediamarkt.nl";
Expand Down
4 changes: 2 additions & 2 deletions src/models/stores/media-markt-spain.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommonStore } from "./abstract-store";
import type { Store } from "./store";
import { CommonStore } from "./abstract-store.js";
import type { Store } from "./store.js";

export class MediaMarktSpain extends CommonStore implements Store {
readonly baseUrl = "https://www.mediamarkt.es";
Expand Down
4 changes: 2 additions & 2 deletions src/models/stores/saturn.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommonStore } from "./abstract-store";
import type { Store } from "./store";
import { CommonStore } from "./abstract-store.js";
import type { Store } from "./store.js";

export class Saturn extends CommonStore implements Store {
readonly baseUrl = "https://www.saturn.de";
Expand Down
19 changes: 10 additions & 9 deletions src/notifiers/discord-notifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { format, parseISO } from "date-fns";
import type { TextChannel } from "discord.js";
import { Client, MessageEmbed } from "discord.js";
import type { Logger } from "winston";
import { version } from "../../package.json";
import type { Item } from "../models/api/item";
import type { Product } from "../models/api/product";
import type { Notifier } from "../models/notifier";
import type { StoreConfiguration } from "../models/stores/config-model";
import type { Store } from "../models/stores/store";
import { ProductHelper } from "../utils/product-helper";
import { noop } from "../utils/utils";
// @ts-expect-error Foo
import x from "../../package.json" assert { type: "json" };
import type { Item } from "../models/api/item.js";
import type { Product } from "../models/api/product.js";
import type { Notifier } from "../models/notifier.js";
import type { StoreConfiguration } from "../models/stores/config-model.js";
import type { Store } from "../models/stores/store.js";
import { ProductHelper } from "../utils/product-helper.js";
import { noop } from "../utils/utils.js";

export class DiscordNotifier implements Notifier {
discordBotReady = false;
Expand Down Expand Up @@ -414,7 +415,7 @@ export class DiscordNotifier implements Notifier {
private createEmbed(item: Item) {
const embed = new MessageEmbed().setTimestamp();
embed.setFooter({
text: `Stockshock v${version} • If you have paid for this, you have been scammed • Links may be affiliate links`,
text: `Stockshock v${x.version} • If you have paid for this, you have been scammed • Links may be affiliate links`,
});
if (!item.product) {
return embed;
Expand Down
10 changes: 5 additions & 5 deletions src/notifiers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from "./discord-notifier";
export * from "./logger-notifier";
export * from "./telegram-notifier";
export * from "./twitter-notifier";
export * from "./websocket-notifier";
export * from "./discord-notifier.js";
export * from "./logger-notifier.js";
export * from "./telegram-notifier.js";
export * from "./twitter-notifier.js";
export * from "./websocket-notifier.js";
14 changes: 7 additions & 7 deletions src/notifiers/logger-notifier.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Logger } from "winston";
import type { Item } from "../models/api/item";
import type { Product } from "../models/api/product";
import type { Notifier } from "../models/notifier";
import type { StoreConfiguration } from "../models/stores/config-model";
import type { Store } from "../models/stores/store";
import { ProductHelper } from "../utils/product-helper";
import { noop } from "../utils/utils";
import type { Item } from "../models/api/item.js";
import type { Product } from "../models/api/product.js";
import type { Notifier } from "../models/notifier.js";
import type { StoreConfiguration } from "../models/stores/config-model.js";
import type { Store } from "../models/stores/store.js";
import { ProductHelper } from "../utils/product-helper.js";
import { noop } from "../utils/utils.js";

export class LoggerNotifier implements Notifier {
private readonly checkOnlineStatus: boolean;
Expand Down
Loading

0 comments on commit cb861d2

Please sign in to comment.