Skip to content
This repository has been archived by the owner on Nov 11, 2024. It is now read-only.

Commit

Permalink
chore: use @stegripe/eslint-config (#1432)
Browse files Browse the repository at this point in the history
* chore: use @stegripe/eslint-config

* chore(eslint-config): extend more

* chore: update script

* fix(eslint-config): wrong usage

* chore(deps): update lockfile

* fix(typescript): change some compiler option

* fix(eslint): properly ignore directories

* fix(*): adjust codes with linter requirements

* feat: patch for play-dl strategy

* chore(deps): update lockfile

---------

Co-authored-by: Mednoob <[email protected]>
  • Loading branch information
mzrtamp and Mednoob authored Jul 21, 2024
1 parent 521ac25 commit 6dcf8ad
Show file tree
Hide file tree
Showing 91 changed files with 3,196 additions and 1,395 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,4 @@ dist
# Rawon cache directories and files
data.json
scripts/
play-dl-fix/
17 changes: 17 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import nodePath from "node:path";
import { fileURLToPath } from "node:url";
import { includeIgnoreFile } from "@eslint/compat";
import { common, modules, node, prettier, typescript, extend, ignores } from "@stegripe/eslint-config";

const gitIgnore = nodePath.resolve(fileURLToPath(import.meta.url), "..", ".gitignore");

export default [...common, ...modules, ...node, ...prettier, ...extend(typescript, [{
rule: "typescript/no-unnecessary-condition",
option: ["off"]
}], ...ignores), includeIgnoreFile(gitIgnore), {
ignores: [
"yt-dlp-utils/*",
"play-dl-importer/*",
"play-dl-fix/*"
]
}];
36 changes: 18 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { downloadExecutable } from "./yt-dlp-utils/index.js";
import { existsSync, readFileSync, rmSync, writeFileSync } from "node:fs";
/* eslint-disable node/no-sync */
import { execSync } from "node:child_process";
import { resolve } from "node:path";
import { existsSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { Server } from "node:http";
import module from "node:module";
import nodePath from "node:path";
import process from "node:process";
import prism from "prism-media";
import { downloadExecutable } from "./yt-dlp-utils/index.js";

const ensureEnv = arr => arr.every(x => process.env[x] !== undefined);

Expand Down Expand Up @@ -36,7 +37,7 @@ const isGitHub = ensureEnv([

function npmInstall(deleteDir = false, forceInstall = false, additionalArgs = []) {
if (deleteDir) {
const modulesPath = resolve(process.cwd(), "node_modules");
const modulesPath = nodePath.resolve(process.cwd(), "node_modules");

if (existsSync(modulesPath)) {
rmSync(modulesPath, {
Expand All @@ -50,7 +51,7 @@ function npmInstall(deleteDir = false, forceInstall = false, additionalArgs = []
}

if (isGlitch) {
const gitIgnorePath = resolve(process.cwd(), ".gitignore");
const gitIgnorePath = nodePath.resolve(process.cwd(), ".gitignore");
try {
const data = readFileSync(gitIgnorePath, "utf8").toString();
if (data.includes("dev.env")) {
Expand All @@ -65,7 +66,7 @@ if (isGlitch) {
console.info("[INFO] Trying to re-install modules...");
npmInstall();
console.info("[INFO] Modules successfully re-installed.");
} catch (err) {
} catch {
console.info("[INFO] Failed to re-install modules, trying to delete node_modules and re-install...");
try {
npmInstall(true);
Expand All @@ -86,8 +87,6 @@ if (isGitHub) {
console.warn("[WARN] Running this bot using GitHub is not recommended.");
}

const require = module.createRequire(import.meta.url);

try {
prism.FFmpeg.getInfo(true);
} catch {
Expand All @@ -100,7 +99,7 @@ if (isGlitch || isReplit) {
new Server((req, res) => {
const now = new Date().toLocaleString("en-US");
res.end(`OK (200) - ${now}`);
}).listen(Number(process.env.PORT || 3000) || 3000);
}).listen(Number(process.env.PORT || 3_000) || 3_000);

console.info(`[INFO] ${isGlitch ? "Glitch" : "Replit"} environment detected, trying to compile...`);
execSync(`npm run compile`);
Expand All @@ -109,14 +108,15 @@ if (isGlitch || isReplit) {

const streamStrategy = process.env.STREAM_STRATEGY;
if (streamStrategy !== "play-dl") await downloadExecutable();
if (streamStrategy === "play-dl") {
try {
require("play-dl");
} catch {
console.info("[INFO] Installing play-dl...");
npmInstall(false, false, ["play-dl"]);
console.info("[INFO] Play-dl has been installed.");
}
if (streamStrategy === "play-dl" && !existsSync(nodePath.resolve(process.cwd(), "play-dl-fix"))) {
console.log("[INFO] Cloning play-dl fix...");
execSync("git clone https://github.com/YuzuZensai/play-dl-test.git play-dl-fix && cd play-dl-fix && git reset --hard 2bfbfe6");

console.log("[INFO] Installing packages for play-dl...");
execSync("cd play-dl-fix && npm install");

console.log("[INFO] Compiling play-dl...");
execSync("cd play-dl-fix && npm run build");
}
console.info("[INFO] Starting the bot...");

Expand Down
28 changes: 5 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"build": "npm run lint && npm run compile",
"compile": "npx swc src -d dist --strip-leading-paths",
"tscompile": "tsc --build tsconfig.json",
"lint": "eslint . --cache --cache-file .eslintcache --ignore-path .gitignore --ext .ts",
"lint:fix": "eslint . --cache --cache-file .eslintcache --ignore-path .gitignore --ext .ts --fix",
"lint": "eslint . --cache --cache-file .eslintcache",
"lint:fix": "eslint . --cache --cache-file .eslintcache --fix",
"pretty": "prettier --check src/**/*.ts",
"pretty:write": "prettier --write src/**/*.ts",
"start": "node --es-module-specifier-resolution=node -r dotenv/config .",
Expand All @@ -35,23 +35,6 @@
"arrowParens": "avoid",
"printWidth": 120
},
"eslintConfig": {
"extends": [
"@clytage/eslint-config/typescript",
"prettier"
],
"plugins": [
"prettier"
],
"ignorePatterns": [
"dist/*",
"index.js",
"yt-dlp-utils/"
],
"rules": {
"@typescript-eslint/no-extra-parens": "off"
}
},
"dependencies": {
"@discordjs/voice": "^0.17.0",
"@swc/cli": "0.4.0",
Expand All @@ -69,12 +52,11 @@
"youtubei": "1.5.4"
},
"devDependencies": {
"@clytage/eslint-config": "^3.0.1",
"@eslint/compat": "^1.1.1",
"@stegripe/eslint-config": "^1.0.1",
"@types/i18n": "0.13.12",
"@types/node": "20.14.11",
"@typescript-eslint/eslint-plugin": "6.18.1",
"@typescript-eslint/parser": "6.18.1",
"eslint": "8.56.0",
"eslint": "9.7.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"prettier": "^3.3.3",
Expand Down
14 changes: 14 additions & 0 deletions play-dl-importer/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Readable } from "node:stream";

export default {
stream: async (url: string, options: { discordPlayerCompatibility: boolean }): Promise<{ stream: Readable }> => {},
video_basic_info: async (url: string): Promise<{
video_details: {
durationInSec: number;
id: string | null;
thumbnails: { url: string; width: number; height: number }[];
title: string | null;
url: string;
}
}> => {}
};
16 changes: 16 additions & 0 deletions play-dl-importer/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { existsSync } from "node:fs";
import module from "node:module";
import nodePath from "node:path";
import { fileURLToPath } from "node:url";

// eslint-disable-next-line import/no-mutable-exports
let mod;

if (existsSync(nodePath.resolve(fileURLToPath(import.meta.url), "..", "..", "play-dl-fix"))) {
mod = await import("../play-dl-fix/dist/index.mjs");
} else {
const require = module.createRequire(nodePath.resolve(fileURLToPath(import.meta.url), ".."));
mod = require("play-dl");
}

export default mod;
Loading

0 comments on commit 6dcf8ad

Please sign in to comment.