-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ts
116 lines (99 loc) · 4.13 KB
/
build.ts
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { CONFIG_V1_SCHEMA } from "@pbrucla/cyanea-cli/config.ts"
import { loadPlugin } from "@pbrucla/cyanea-cli/plugin.ts"
import chalk from "chalk"
import esbuild from "esbuild"
import child_process from "node:child_process"
import fs from "node:fs/promises"
import util from "node:util"
function generateBanner(packageName: string): { js: string } {
// fixes https://github.com/evanw/esbuild/issues/1921#issuecomment-1152991694
return {
js: `${
packageName === "cyanea" ? "#!/usr/bin/env node\n" : ""
}/** ${packageName} - ACM Cyber's modular script for syncing unified event information across disparate platforms! **/\nvar require=(await import("module")).createRequire(import.meta.url)${
packageName === "@pbrucla/cyanea-discord"
? `;var __dirname=(await import("path")).dirname((await import("url")).fileURLToPath(import.meta.url))`
: ""
}`,
}
}
const COMMON_ESLINT_OPTS: esbuild.BuildOptions = {
bundle: true,
minify: true,
platform: "node",
format: "esm",
target: "esnext",
}
console.log(chalk.blueBright("Locating packages..."))
const packages = new Set<string>()
for await (const p of await fs.opendir("packages")) {
packages.add(p.name)
}
// make sure cyanea-cli actually exists
if (!packages.has("cyanea-cli")) {
throw "fatal: could not find packages/cyanea-cli"
}
// don't build cyanea-core explicitly
packages.delete("cyanea-core")
console.log(chalk.blueBright("Building cyanea-cli..."))
await esbuild.build({
entryPoints: ["packages/cyanea-cli/index.ts"],
outfile: "dist/cyanea.mjs",
banner: generateBanner("cyanea"),
define: {
"process.env.DIST_PLUGINS_FOLDER": `"plugins"`,
"process.env.FLUENTFFMPEG_COV": "false",
"process.env.npm_package_version": JSON.stringify(process.env.npm_package_version ?? "unknown"),
},
...COMMON_ESLINT_OPTS,
})
packages.delete("cyanea-cli")
const configs: {
filestore: Record<string, Record<string, any>>
source: Record<string, Record<string, any>>
sink: Record<string, Record<string, any>>
} = { filestore: {}, source: {}, sink: {} }
for (const p of packages) {
console.log(chalk.blueBright(`Building plugin ${p}...`))
const plugin = await loadPlugin(`@pbrucla/${p}`)
if (plugin === null || typeof plugin !== "object") {
throw `package 'packages/${p}' is not a plugin`
}
for (const type of ["filestore", "source", "sink"] as const) {
if (plugin[type] !== undefined && plugin[type] !== null) {
if (typeof plugin[type] != "object" || !("configSchema" in plugin[type])) {
throw `package 'packages/${p}''s ${type} declaration is not a CyaneaPluginComponent`
}
const pluginConfigSchema = plugin[type].configSchema as Record<string, any>
if (!("description" in pluginConfigSchema)) {
pluginConfigSchema["description"] = `@pbrucla/${p} ${type} config options.`
}
configs[type][`@pbrucla/${p}`] = pluginConfigSchema
}
}
await esbuild.build({
entryPoints: [`packages/${p}/index.ts`],
outfile: `dist/plugins/@pbrucla/${p}.mjs`,
banner: generateBanner(`@pbrucla/${p}`),
define: {
"process.env.FLUENTFFMPEG_COV": "false",
},
...COMMON_ESLINT_OPTS,
})
}
console.log(chalk.blueBright("Exporting schemas..."))
await fs.mkdir("dist/schemas", { recursive: true })
await fs.copyFile("packages/cyanea-core/event/event.schema.json", "dist/schemas/event.schema.json")
await fs.copyFile("packages/cyanea-core/event/events.schema.json", "dist/schemas/events.schema.json")
const finalConfigSchema = Object.assign({ $schema: "https://json-schema.org/draft-07/schema#" }, CONFIG_V1_SCHEMA)
finalConfigSchema.properties!.filestore["properties"] = configs.filestore
finalConfigSchema.properties!.source["properties"] = configs.source
finalConfigSchema.properties!.sinks["properties"] = configs.sink
await fs.writeFile("dist/schemas/config.schema.json", JSON.stringify(finalConfigSchema, undefined, 2))
console.log(chalk.blueBright("Exporting licenses..."))
await fs.writeFile(
"dist/LICENSE-3RD-PARTY.txt",
(await util.promisify(child_process.exec)("yarn licenses generate-disclaimer --recursive --production")).stdout,
)
console.log(chalk.blueBright("Build succeeded!"))
process.exit(0)