diff --git a/.eslintrc.json b/.eslintrc.json index 8a346f5..2c3c923 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -13,5 +13,9 @@ ], "env": { "browser": false + }, + "rules": { + "@typescript-eslint/no-empty-function": "warn", + "@typescript-eslint/ban-ts-comment": "off" } } diff --git a/lib/anime-dl/index.ts b/lib/anime-dl/index.ts index 0d5243f..3eca10b 100644 --- a/lib/anime-dl/index.ts +++ b/lib/anime-dl/index.ts @@ -1,6 +1,5 @@ import * as fsP from "fs/promises"; -// eslint-disable-next-line -const path = require("path"); // ignoring import lint, path only imported as common +import path from "path"; import cheerio from "cheerio"; import utils from "../utils"; diff --git a/lib/anime-dl/processing.ts b/lib/anime-dl/processing.ts index ea992a5..26adfe4 100644 --- a/lib/anime-dl/processing.ts +++ b/lib/anime-dl/processing.ts @@ -53,7 +53,7 @@ const extractVideoMetadataFromDetailsPage = ( ): VideoMetadata => { const $ = cheerio.load(pageHTML); const videoInfo = $(".anime_info_body"); - let videoMetadata: VideoMetadata = {}; + const videoMetadata: VideoMetadata = {}; videoMetadata.title = videoInfo.find("h1").text(); @@ -76,14 +76,12 @@ const getEpisodeRangesFromDetailsPage = (pageHTML: string) => { const $ = cheerio.load(pageHTML); const episodePage = $("#episode_page"); const episodeRanges = episodePage.find("li"); - let extractedRanges: EpisodeRange[] = []; + const extractedRanges: EpisodeRange[] = []; episodeRanges.each(function () { const range = $(this).find("a").text(); const bounds = range.split("-"); - const [lowerBound, upperBound] = bounds; - const normalizedLowerBound = stripNewlinesAndSpacesToNum(lowerBound); - const normalizedUpperBound = stripNewlinesAndSpacesToNum(upperBound); + const [start, end] = bounds.map(stripNewlinesAndSpacesToNum); extractedRanges.push( bounds.length === 1 @@ -92,8 +90,8 @@ const getEpisodeRangesFromDetailsPage = (pageHTML: string) => { end: 1, } : { - start: normalizedLowerBound === 0 ? 1 : normalizedLowerBound, - end: normalizedUpperBound, + start: start === 0 ? 1 : start, + end, } ); }); diff --git a/lib/check-executable/index.ts b/lib/check-executable/index.ts index 38804ce..58908b2 100644 --- a/lib/check-executable/index.ts +++ b/lib/check-executable/index.ts @@ -18,7 +18,7 @@ const { const sanitizeCommand = (command: string): string => { let result = ""; - if (/[^A-Za-z0-9_\/:=-]/.test(command)) { + if (/[^A-Za-z0-9_/:=-]/.test(command)) { result = "'" + command.replace(/'/g, "'\\''") + "'"; result = command .replace(/^(?:'')+/g, "") // dedupe single-quote at the beginning diff --git a/lib/utils/http.ts b/lib/utils/http.ts index 2227075..8f284e6 100644 --- a/lib/utils/http.ts +++ b/lib/utils/http.ts @@ -5,24 +5,30 @@ interface ResponseHeadersWithLocation extends http.IncomingHttpHeaders { location: string; } -type ClientProtocols = "http" | "https"; -type ClientTypeOf = typeof http | typeof https; +type Protocols = "http" | "https"; +type ClientType = typeof http | typeof https; +type EmptyOptions = Record; -const Clients: { [k in ClientProtocols]: ClientTypeOf } = { +const Clients: { [k in Protocols]: ClientType } = { http, https, }; +const RedirectCodesMap: { [k in string]: number } = { + "301": 301, + "302": 302, +}; + const noop = () => {}; -const chooseClient = (url: string): ClientTypeOf => { +const chooseClient = (url: string): ClientType => { const reProtocolMatch = /(https?):\/\//gi; const matchResult = url.match(reProtocolMatch) || []; - let protocolScheme: ClientProtocols = "https"; + let protocolScheme: Protocols = "https"; if (matchResult.length) { protocolScheme = (matchResult[0].split(":")[0] || - protocolScheme) as ClientProtocols; + protocolScheme) as Protocols; } return Clients[protocolScheme]; @@ -36,15 +42,14 @@ const getOriginHeadersWithLocation = ( | PromiseLike ) => void, reject: (reason?: string | undefined) => void, - options: http.RequestOptions | {} + options: http.RequestOptions | EmptyOptions ) => { - const client = chooseClient(url) as unknown as typeof http; + const client = chooseClient(url) as unknown as ClientType; client .get(url, options, (res) => { const redirectCode = - res.statusCode != null && - (res.statusCode === 301 || res.statusCode === 302); + res.statusCode != null && RedirectCodesMap[res.statusCode]; if (redirectCode) { return getOriginHeadersWithLocation( @@ -65,15 +70,14 @@ const get = ( url: string, resolve: (value: string | PromiseLike) => void, reject: (reason?: string | undefined) => void, - options: http.RequestOptions | {} + options: http.RequestOptions | EmptyOptions ) => { - const client = chooseClient(url) as unknown as typeof http; + const client = chooseClient(url) as unknown as ClientType; client .get(url, options, (res) => { const redirectCode = - res.statusCode != null && - (res.statusCode === 301 || res.statusCode === 302); + res.statusCode != null && RedirectCodesMap[res.statusCode]; if (redirectCode) { return get(res.headers.location || "", resolve, reject, options); diff --git a/tsconfig.json b/tsconfig.json index 755f900..b1b8db1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,7 +3,8 @@ "module": "commonjs", "moduleResolution": "node", "noImplicitAny": true, - "noUnusedLocals": false, + "noUnusedLocals": true, + "esModuleInterop": true, "strict": true, "baseUrl": ".", "paths": {