From 897df3a5d44df9227e35f82e6dcddd3020a04035 Mon Sep 17 00:00:00 2001 From: Vendicated Date: Mon, 10 Apr 2023 22:05:21 +0200 Subject: [PATCH] Add support for (optional) GITHUB_TOKEN env variable --- .env.example | 6 ++++++ .gitignore | 1 + package.json | 8 +++++--- pnpm-lock.yaml | 8 ++++++++ scripts/build/build.mts | 16 ++++++++++++---- scripts/build/injectReact.mjs | 10 ++++++++-- scripts/start.ts | 11 +++++++++++ scripts/startWatch.mts | 21 ++++++++------------- scripts/utils/dotenv.ts | 9 +++++++++ scripts/utils/spawn.mts | 18 ++++++++++++++++++ src/main/utils/vencordLoader.ts | 9 +++++++-- 11 files changed, 93 insertions(+), 24 deletions(-) create mode 100644 .env.example create mode 100644 scripts/start.ts create mode 100644 scripts/utils/dotenv.ts create mode 100644 scripts/utils/spawn.mts diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..ebec903 --- /dev/null +++ b/.env.example @@ -0,0 +1,6 @@ +# githubs api has a rate limit of 60/h if not authorised. +# you may quickly hit that and get rate limited. To counteract this, you can provide a github token +# here and it will be used. To do so, create a token at the following links and just leave +# all permissions at the defaults (public repos read only, 0 permissions): +# https://github.com/settings/personal-access-tokens/new +GITHUB_TOKEN= diff --git a/.gitignore b/.gitignore index de4d1f0..6ee3e11 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ dist node_modules +.env diff --git a/package.json b/package.json index b859c60..81018a4 100644 --- a/package.json +++ b/package.json @@ -10,13 +10,14 @@ "main": "dist/js/main.js", "scripts": { "build": "tsx scripts/build/build.mts", + "build:dev": "pnpm build --dev", "package": "pnpm build && electron-builder", "package:dir": "pnpm build && electron-builder --dir", - "lint": "eslint . --ext .js,.jsx,.ts,.tsx", + "lint": "eslint . --ext .js,.jsx,.ts,.tsx,.mts,.mjs", "lint:fix": "pnpm lint --fix", "start": "pnpm build && electron .", - "start:dev": "pnpm build --dev && electron .", - "start:watch": "tsx scripts/startWatch.mts", + "start:dev": "pnpm build:dev && electron .", + "start:watch": "pnpm build:dev && tsx scripts/startWatch.mts", "test": "pnpm lint && pnpm testTypes", "testTypes": "tsc --noEmit", "watch": "pnpm build --watch" @@ -26,6 +27,7 @@ "@types/react": "^18.0.33", "@typescript-eslint/eslint-plugin": "^5.57.1", "@typescript-eslint/parser": "^5.57.1", + "dotenv": "^16.0.3", "electron": "^23.2.0", "electron-builder": "^23.6.0", "esbuild": "^0.17.14", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8b762f8..4816cd5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -13,6 +13,9 @@ devDependencies: '@typescript-eslint/parser': specifier: ^5.57.1 version: 5.57.1(eslint@8.38.0)(typescript@5.0.2) + dotenv: + specifier: ^16.0.3 + version: 16.0.3 electron: specifier: ^23.2.0 version: 23.2.0 @@ -1390,6 +1393,11 @@ packages: resolution: {integrity: sha512-YXQl1DSa4/PQyRfgrv6aoNjhasp/p4qs9FjJ4q4cQk+8m4r6k4ZSiEyytKG8f8W9gi8WsQtIObNmKd+tMzNTmA==} dev: true + /dotenv@16.0.3: + resolution: {integrity: sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ==} + engines: {node: '>=12'} + dev: true + /dotenv@9.0.2: resolution: {integrity: sha512-I9OvvrHp4pIARv4+x9iuewrWycX6CcZtoAu1XrzPxc5UygMJXJZYmBsynku8IkrJwgypE5DGNjDPmPRhDCptUg==} engines: {node: '>=10'} diff --git a/scripts/build/build.mts b/scripts/build/build.mts index c335fe8..54f80cb 100644 --- a/scripts/build/build.mts +++ b/scripts/build/build.mts @@ -1,3 +1,9 @@ +/* + * SPDX-License-Identifier: GPL-3.0 + * Vencord Desktop, a desktop app aiming to give you a snappier Discord Experience + * Copyright (c) 2023 Vendicated and Vencord contributors + */ + import { BuildContext, BuildOptions, context } from "esbuild"; const isDev = process.argv.includes("--dev"); @@ -55,8 +61,10 @@ const watch = process.argv.includes("--watch"); if (watch) { await Promise.all(contexts.map(ctx => ctx.watch())); } else { - await Promise.all(contexts.map(async ctx => { - await ctx.rebuild(); - await ctx.dispose(); - })); + await Promise.all( + contexts.map(async ctx => { + await ctx.rebuild(); + await ctx.dispose(); + }) + ); } diff --git a/scripts/build/injectReact.mjs b/scripts/build/injectReact.mjs index 0890ac0..0f1bba2 100644 --- a/scripts/build/injectReact.mjs +++ b/scripts/build/injectReact.mjs @@ -1,3 +1,9 @@ +/* + * SPDX-License-Identifier: GPL-3.0 + * Vencord Desktop, a desktop app aiming to give you a snappier Discord Experience + * Copyright (c) 2023 Vendicated and Vencord contributors + */ + export const VencordFragment = /* #__PURE__*/ Symbol.for("react.fragment"); -export let VencordCreateElement = - (...args) => (VencordCreateElement = Vencord.Webpack.Common.React.createElement)(...args); +export let VencordCreateElement = (...args) => + (VencordCreateElement = Vencord.Webpack.Common.React.createElement)(...args); diff --git a/scripts/start.ts b/scripts/start.ts new file mode 100644 index 0000000..c2cc154 --- /dev/null +++ b/scripts/start.ts @@ -0,0 +1,11 @@ +/* + * SPDX-License-Identifier: GPL-3.0 + * Vencord Desktop, a desktop app aiming to give you a snappier Discord Experience + * Copyright (c) 2023 Vendicated and Vencord contributors + */ + +import "./utils/dotenv"; + +import { spawnNodeModuleBin } from "./utils/spawn.mjs"; + +spawnNodeModuleBin("electron", ["."]); diff --git a/scripts/startWatch.mts b/scripts/startWatch.mts index d30a938..1f72443 100644 --- a/scripts/startWatch.mts +++ b/scripts/startWatch.mts @@ -1,15 +1,10 @@ -import { spawn as cpSpawn, SpawnOptions } from "child_process"; -import { join } from "path"; +/* + * SPDX-License-Identifier: GPL-3.0 + * Vencord Desktop, a desktop app aiming to give you a snappier Discord Experience + * Copyright (c) 2023 Vendicated and Vencord contributors + */ -const EXT = process.platform === "win32" ? ".cmd" : ""; +import "./start"; -const OPTS: SpawnOptions = { - stdio: "inherit", -}; - -function spawn(bin: string, args: string[]) { - cpSpawn(join("node_modules", ".bin", bin + EXT), args, OPTS); -} - -spawn("tsx", ["scripts/build/build.mts", "--", "--watch", "--dev"]); -spawn("electron", ["."]); +import { spawnNodeModuleBin } from "./utils/spawn.mjs"; +spawnNodeModuleBin("tsx", ["scripts/build/build.mts", "--", "--watch", "--dev"]); diff --git a/scripts/utils/dotenv.ts b/scripts/utils/dotenv.ts new file mode 100644 index 0000000..73afb64 --- /dev/null +++ b/scripts/utils/dotenv.ts @@ -0,0 +1,9 @@ +/* + * SPDX-License-Identifier: GPL-3.0 + * Vencord Desktop, a desktop app aiming to give you a snappier Discord Experience + * Copyright (c) 2023 Vendicated and Vencord contributors + */ + +import { config } from "dotenv"; + +config(); diff --git a/scripts/utils/spawn.mts b/scripts/utils/spawn.mts new file mode 100644 index 0000000..2d2f2a9 --- /dev/null +++ b/scripts/utils/spawn.mts @@ -0,0 +1,18 @@ +/* + * SPDX-License-Identifier: GPL-3.0 + * Vencord Desktop, a desktop app aiming to give you a snappier Discord Experience + * Copyright (c) 2023 Vendicated and Vencord contributors + */ + +import { spawn as spaaawn, SpawnOptions } from "child_process"; +import { join } from "path"; + +const EXT = process.platform === "win32" ? ".cmd" : ""; + +const OPTS: SpawnOptions = { + stdio: "inherit" +}; + +export function spawnNodeModuleBin(bin: string, args: string[]) { + spaaawn(join("node_modules", ".bin", bin + EXT), args, OPTS); +} diff --git a/src/main/utils/vencordLoader.ts b/src/main/utils/vencordLoader.ts index 5f17add..8799280 100644 --- a/src/main/utils/vencordLoader.ts +++ b/src/main/utils/vencordLoader.ts @@ -5,6 +5,7 @@ */ import { existsSync, mkdirSync } from "fs"; +import type { RequestOptions } from "https"; import { join } from "path"; import { USER_AGENT, VENCORD_FILES_DIR } from "../constants"; @@ -15,12 +16,16 @@ const API_BASE = "https://api.github.com/repos/Vendicated/Vencord"; const FILES_TO_DOWNLOAD = ["vencordDesktopMain.js", "preload.js", "vencordDesktopRenderer.js", "renderer.css"]; export async function githubGet(endpoint: string) { - return simpleGet(API_BASE + endpoint, { + const opts: RequestOptions = { headers: { Accept: "application/vnd.github+json", "User-Agent": USER_AGENT } - }); + }; + + if (process.env.GITHUB_TOKEN) opts.headers!.Authorization = `Bearer ${process.env.GITHUB_TOKEN}`; + + return simpleGet(API_BASE + endpoint, opts); } export async function downloadVencordFiles() {