Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(PatchHelper): enhance RegExp & quote syntax support #2661

Open
wants to merge 17 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor(scripts): use TypeScript
  • Loading branch information
bb010g committed Jul 3, 2024
commit 6c077a2d6066d2c12afd4f7aade20dd1a38a0f5e
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
"license": "GPL-3.0-or-later",
"author": "Vendicated",
"scripts": {
"build": "node --require=./scripts/suppressExperimentalWarnings.js scripts/build/build.mjs",
"build": "tsx --require=./scripts/suppressExperimentalWarnings.js scripts/build/build.mts",
"buildStandalone": "pnpm run build --standalone",
"buildWeb": "node --require=./scripts/suppressExperimentalWarnings.js scripts/build/buildWeb.mjs",
"buildWeb": "tsx --require=./scripts/suppressExperimentalWarnings.js scripts/build/buildWeb.mts",
"buildWebStandalone": "pnpm run buildWeb --standalone",
"buildReporter": "pnpm run buildWebStandalone --reporter --skip-extension",
"buildReporterDesktop": "pnpm run build --reporter",
Expand Down
66 changes: 30 additions & 36 deletions scripts/build/build.mjs → scripts/build/build.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/node
#!/usr/bin/env tsx
/*
* Vencord, a modification for Discord's desktop app
* Copyright (c) 2022 Vendicated and contributors
Expand All @@ -23,41 +23,35 @@ import { join } from "path";

import { BUILD_TIMESTAMP, commonOpts, exists, globPlugins, IS_DEV, IS_REPORTER, IS_STANDALONE, IS_UPDATER_DISABLED, resolvePluginName, VERSION, watch } from "./common.mjs";

const defines = {
IS_STANDALONE,
IS_DEV,
IS_REPORTER,
IS_UPDATER_DISABLED,
IS_WEB: false,
IS_EXTENSION: false,
const defines: esbuild.CommonOptions["define"] = {
IS_STANDALONE: JSON.stringify(IS_STANDALONE),
IS_DEV: JSON.stringify(IS_DEV),
IS_REPORTER: JSON.stringify(IS_REPORTER),
IS_UPDATER_DISABLED: JSON.stringify(IS_UPDATER_DISABLED),
IS_WEB: JSON.stringify(false),
IS_EXTENSION: JSON.stringify(false),
VERSION: JSON.stringify(VERSION),
BUILD_TIMESTAMP
BUILD_TIMESTAMP: JSON.stringify(BUILD_TIMESTAMP),
};

if (defines.IS_STANDALONE === false)
if (defines.IS_STANDALONE === "false")
// If this is a local build (not standalone), optimize
// for the specific platform we're on
defines["process.platform"] = JSON.stringify(process.platform);

/**
* @type {esbuild.BuildOptions}
*/
const nodeCommonOpts = {
const nodeCommonOpts: esbuild.BuildOptions = {
...commonOpts,
format: "cjs",
platform: "node",
target: ["esnext"],
external: ["electron", "original-fs", "~pluginNatives", ...commonOpts.external],
external: ["electron", "original-fs", "~pluginNatives", ...(commonOpts.external ?? [])],
define: defines
};

const sourceMapFooter = s => watch ? "" : `//# sourceMappingURL=vencord://${s}.js.map`;
const sourceMapFooter = (s: string) => watch ? "" : `//# sourceMappingURL=vencord://${s}.js.map`;
const sourcemap = watch ? "inline" : "external";

/**
* @type {import("esbuild").Plugin}
*/
const globNativesPlugin = {
const globNativesPlugin: esbuild.Plugin = {
name: "glob-natives-plugin",
setup: build => {
const filter = /^~pluginNatives$/;
Expand Down Expand Up @@ -112,11 +106,11 @@ await Promise.all([
sourcemap,
define: {
...defines,
IS_DISCORD_DESKTOP: true,
IS_VESKTOP: false
IS_DISCORD_DESKTOP: JSON.stringify(true),
IS_VESKTOP: JSON.stringify(false)
},
plugins: [
...nodeCommonOpts.plugins,
...(nodeCommonOpts.plugins ?? []),
globNativesPlugin
]
}),
Expand All @@ -131,12 +125,12 @@ await Promise.all([
sourcemap,
plugins: [
globPlugins("discordDesktop"),
...commonOpts.plugins
...(commonOpts.plugins ?? [])
],
define: {
...defines,
IS_DISCORD_DESKTOP: true,
IS_VESKTOP: false
IS_DISCORD_DESKTOP: JSON.stringify(true),
IS_VESKTOP: JSON.stringify(false)
}
}),
esbuild.build({
Expand All @@ -147,8 +141,8 @@ await Promise.all([
sourcemap,
define: {
...defines,
IS_DISCORD_DESKTOP: true,
IS_VESKTOP: false
IS_DISCORD_DESKTOP: JSON.stringify(true),
IS_VESKTOP: JSON.stringify(false)
}
}),

Expand All @@ -161,11 +155,11 @@ await Promise.all([
sourcemap,
define: {
...defines,
IS_DISCORD_DESKTOP: false,
IS_VESKTOP: true
IS_DISCORD_DESKTOP: JSON.stringify(false),
IS_VESKTOP: JSON.stringify(true)
},
plugins: [
...nodeCommonOpts.plugins,
...(nodeCommonOpts.plugins ?? []),
globNativesPlugin
]
}),
Expand All @@ -180,12 +174,12 @@ await Promise.all([
sourcemap,
plugins: [
globPlugins("vencordDesktop"),
...commonOpts.plugins
...(commonOpts.plugins ?? [])
],
define: {
...defines,
IS_DISCORD_DESKTOP: false,
IS_VESKTOP: true
IS_DISCORD_DESKTOP: JSON.stringify(false),
IS_VESKTOP: JSON.stringify(true)
}
}),
esbuild.build({
Expand All @@ -196,8 +190,8 @@ await Promise.all([
sourcemap,
define: {
...defines,
IS_DISCORD_DESKTOP: false,
IS_VESKTOP: true
IS_DISCORD_DESKTOP: JSON.stringify(false),
IS_VESKTOP: JSON.stringify(true)
}
}),
]).catch(err => {
Expand Down
53 changes: 21 additions & 32 deletions scripts/build/buildWeb.mjs → scripts/build/buildWeb.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/node
#!/usr/bin/env tsx
/*
* Vencord, a modification for Discord's desktop app
* Copyright (c) 2022 Vendicated and contributors
Expand All @@ -25,31 +25,28 @@ import Zip from "zip-local";

import { BUILD_TIMESTAMP, commonOpts, globPlugins, IS_DEV, IS_REPORTER, VERSION } from "./common.mjs";

/**
* @type {esbuild.BuildOptions}
*/
const commonOptions = {
const commonOptions: esbuild.BuildOptions = {
...commonOpts,
entryPoints: ["browser/Vencord.ts"],
globalName: "Vencord",
format: "iife",
external: ["~plugins", "~git-hash", "/assets/*"],
plugins: [
globPlugins("web"),
...commonOpts.plugins,
...(commonOpts.plugins ?? []),
],
target: ["esnext"],
define: {
IS_WEB: true,
IS_EXTENSION: false,
IS_STANDALONE: true,
IS_DEV,
IS_REPORTER,
IS_DISCORD_DESKTOP: false,
IS_VESKTOP: false,
IS_UPDATER_DISABLED: true,
IS_WEB: JSON.stringify(true),
IS_EXTENSION: JSON.stringify(false),
IS_STANDALONE: JSON.stringify(true),
IS_DEV: JSON.stringify(IS_DEV),
IS_REPORTER: JSON.stringify(IS_REPORTER),
IS_DISCORD_DESKTOP: JSON.stringify(false),
IS_VESKTOP: JSON.stringify(false),
IS_UPDATER_DISABLED: JSON.stringify(true),
VERSION: JSON.stringify(VERSION),
BUILD_TIMESTAMP
BUILD_TIMESTAMP: JSON.stringify(BUILD_TIMESTAMP),
}
};

Expand Down Expand Up @@ -95,7 +92,7 @@ await Promise.all(
outfile: "dist/extension.js",
define: {
...commonOptions?.define,
IS_EXTENSION: true,
IS_EXTENSION: JSON.stringify(true),
},
footer: { js: "//# sourceURL=VencordWeb" }
}),
Expand All @@ -118,11 +115,8 @@ await Promise.all(
]
);

/**
* @type {(dir: string) => Promise<string[]>}
*/
async function globDir(dir) {
const files = [];
async function globDir(dir: string): Promise<string[]> {
const files = [] as string[];

for (const child of await readdir(dir, { withFileTypes: true })) {
const p = join(dir, child.name);
Expand All @@ -135,19 +129,14 @@ async function globDir(dir) {
return files;
}

/**
* @type {(dir: string, basePath?: string) => Promise<Record<string, string>>}
*/
async function loadDir(dir, basePath = "") {
async function loadDir(dir: string, basePath: string = ""): Promise<Record<string, string>> {
const files = await globDir(dir);
return Object.fromEntries(await Promise.all(files.map(async f => [f.slice(basePath.length), await readFile(f)])));
const dirContentEntries = await Promise.all(files.map(async (f): Promise<[string, string]> => [f.slice(basePath.length), await readFile(f, { encoding: "utf-8" })]));
return Object.fromEntries(dirContentEntries satisfies [string, string][]);
}

/**
* @type {(target: string, files: string[]) => Promise<void>}
*/
async function buildExtension(target, files) {
const entries = {
async function buildExtension(target: string, files: string[]): Promise<void> {
const entries: Record<string, Buffer> = {
"dist/Vencord.js": await readFile("dist/extension.js"),
"dist/Vencord.css": await readFile("dist/extension.css"),
...await loadDir("dist/monaco"),
Expand All @@ -159,7 +148,7 @@ async function buildExtension(target, files) {
if (f.startsWith("manifest")) {
const json = JSON.parse(content.toString("utf-8"));
json.version = VERSION;
content = new TextEncoder().encode(JSON.stringify(json));
content = Buffer.from(JSON.stringify(json), "utf-8");
}

return [
Expand Down
50 changes: 12 additions & 38 deletions scripts/build/common.mjs → scripts/build/common.mts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@ import "../checkNodeVersion.js";

import { exec, execSync } from "child_process";
import esbuild from "esbuild";
import { constants as FsConstants, readFileSync } from "fs";
import { constants as FsConstants, type Dirent, type PathLike, readFileSync } from "fs";
import { access, readdir, readFile } from "fs/promises";
import { minify as minifyHtml } from "html-minifier-terser";
import { join, relative } from "path";
import { promisify } from "util";

import { getPluginTarget } from "../utils.mjs";

/** @type {import("../../package.json")} */
const PackageJSON = JSON.parse(readFileSync("package.json"));
const PackageJSON: typeof import("../../package.json") = JSON.parse(readFileSync("package.json", { encoding: "utf-8" }));

export const VERSION = PackageJSON.version;
// https://reproducible-builds.org/docs/source-date-epoch/
Expand All @@ -54,11 +53,7 @@ export const banner = {
};

const PluginDefinitionNameMatcher = /definePlugin\(\{\s*(["'])?name\1:\s*(["'`])(.+?)\2/;
/**
* @param {string} base
* @param {import("fs").Dirent} dirent
*/
export async function resolvePluginName(base, dirent) {
export async function resolvePluginName(base: string, dirent: Dirent): Promise<string> {
const fullPath = join(base, dirent.name);
const content = dirent.isFile()
? await readFile(fullPath, "utf-8")
Expand All @@ -79,28 +74,22 @@ export async function resolvePluginName(base, dirent) {
})();
}

export async function exists(path) {
export async function exists(path: PathLike): Promise<boolean> {
return await access(path, FsConstants.F_OK)
.then(() => true)
.catch(() => false);
}

// https://github.com/evanw/esbuild/issues/619#issuecomment-751995294
/**
* @type {import("esbuild").Plugin}
*/
export const makeAllPackagesExternalPlugin = {
export const makeAllPackagesExternalPlugin: esbuild.Plugin = {
name: "make-all-packages-external",
setup(build) {
const filter = /^[^./]|^\.[^./]|^\.\.[^/]/; // Must not start with "/" or "./" or "../"
build.onResolve({ filter }, args => ({ path: args.path, external: true }));
}
};

/**
* @type {(kind: "web" | "discordDesktop" | "vencordDesktop") => import("esbuild").Plugin}
*/
export const globPlugins = kind => ({
export const globPlugins = (kind: "web" | "discordDesktop" | "vencordDesktop"): esbuild.Plugin => ({
name: "glob-plugins",
setup: build => {
const filter = /^~plugins$/;
Expand Down Expand Up @@ -164,10 +153,7 @@ export const globPlugins = kind => ({
}
});

/**
* @type {import("esbuild").Plugin}
*/
export const gitHashPlugin = {
export const gitHashPlugin: esbuild.Plugin = {
name: "git-hash-plugin",
setup: build => {
const filter = /^~git-hash$/;
Expand All @@ -180,10 +166,7 @@ export const gitHashPlugin = {
}
};

/**
* @type {import("esbuild").Plugin}
*/
export const gitRemotePlugin = {
export const gitRemotePlugin: esbuild.Plugin = {
name: "git-remote-plugin",
setup: build => {
const filter = /^~git-remote$/;
Expand All @@ -205,10 +188,7 @@ export const gitRemotePlugin = {
}
};

/**
* @type {import("esbuild").Plugin}
*/
export const fileUrlPlugin = {
export const fileUrlPlugin: esbuild.Plugin = {
name: "file-uri-plugin",
setup: build => {
const filter = /^file:\/\/.+$/;
Expand Down Expand Up @@ -268,10 +248,7 @@ export const fileUrlPlugin = {
};

const styleModule = readFileSync("./scripts/build/module/style.js", "utf-8");
/**
* @type {import("esbuild").Plugin}
*/
export const stylePlugin = {
export const stylePlugin: esbuild.Plugin = {
name: "style-plugin",
setup: ({ onResolve, onLoad }) => {
onResolve({ filter: /\.css\?managed$/, namespace: "file" }, ({ path, resolveDir }) => ({
Expand All @@ -292,15 +269,12 @@ export const stylePlugin = {
}
};

/**
* @type {import("esbuild").BuildOptions}
*/
export const commonOpts = {
export const commonOpts: esbuild.BuildOptions = {
logLevel: "info",
bundle: true,
watch,
minify: !watch,
sourcemap: watch ? "inline" : "",
sourcemap: watch ? "inline" : undefined,
legalComments: "linked",
banner,
plugins: [fileUrlPlugin, gitHashPlugin, gitRemotePlugin, stylePlugin],
Expand Down
Loading