This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
generated from vendetta-mod/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 13
/
build.mjs
96 lines (84 loc) · 2.83 KB
/
build.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { readFile, writeFile, readdir } from "fs/promises"
import { createHash } from "crypto"
import { rollup } from "rollup"
import esbuild from "rollup-plugin-esbuild"
import commonjs from "@rollup/plugin-commonjs"
import nodeResolve from "@rollup/plugin-node-resolve"
import swc from "@swc/core"
import { execSync } from "child_process"
import { argv } from "process"
let deploy = argv[2]
if (deploy === "--deploy") deploy = true
/** @type import("rollup").InputPluginOption */
const plugins = [
nodeResolve(),
commonjs(),
{
name: "swc",
async transform(code, id) {
const result = await swc.transform(code, {
filename: id,
jsc: {
externalHelpers: true,
parser: {
syntax: "typescript",
tsx: true,
},
},
env: {
targets: "defaults",
include: [
"transform-classes",
"transform-arrow-functions",
],
},
})
return result.code
},
},
esbuild({ minify: true }),
]
for (let plug of await readdir("./plugins")) {
const manifest = JSON.parse(await readFile(`./plugins/${plug}/manifest.json`))
const outPath = `./dist/${plug}/index.js`
try {
const bundle = await rollup({
input: `./plugins/${plug}/${manifest.main}`,
onwarn: () => { },
plugins,
})
await bundle.write({
file: outPath,
globals(id) {
if (id.startsWith("@vendetta")) return id.substring(1).replace(/\//g, ".")
const map = {
react: "window.React",
}
return map[id] || null
},
format: "iife",
compact: true,
exports: "named",
})
await bundle.close()
const toHash = await readFile(outPath)
manifest.hash = createHash("sha256").update(toHash).digest("hex")
manifest.main = "index.js"
await writeFile(`./dist/${plug}/manifest.json`, JSON.stringify(manifest))
console.log(`Successfully built ${manifest.name}!`)
} catch (e) {
console.error("Failed to build plugin...", e)
process.exit(1)
}
}
if (deploy) {
const exec = (cmd) => execSync(cmd, { stdio: "inherit" })
console.log("Deploying plugin to device...")
let isXposed
try {
isXposed = execSync(`adb shell "pm list packages | grep com.vendetta.xposed"`).length > 0
} catch {}
const pkgName = isXposed ? "com.discord" : "dev.beefers.vendetta"
exec(`adb shell am force-stop ${pkgName}`)
exec(`adb shell am start -n ${pkgName}/com.discord.main.MainActivity`)
}