186 lines
5.0 KiB
TypeScript
Raw Normal View History

2024-05-19 04:02:36 +02:00
import * as child_process from "child_process";
import * as fs from "fs";
2024-07-10 01:26:55 +02:00
import { join } from "path";
2024-05-19 04:02:36 +02:00
import { assert } from "tsafe/assert";
import { transformCodebase } from "../src/bin/tools/transformCodebase";
2024-05-19 10:46:26 +02:00
import chalk from "chalk";
2024-09-08 00:06:47 +02:00
import { WELL_KNOWN_DIRECTORY_BASE_NAME } from "../src/bin/shared/constants";
2024-05-19 10:46:26 +02:00
console.log(chalk.cyan("Building Keycloakify..."));
const startTime = Date.now();
2024-05-19 04:02:36 +02:00
if (fs.existsSync(join("dist", "bin", "main.original.js"))) {
2024-05-20 15:48:51 +02:00
fs.renameSync(
join("dist", "bin", "main.original.js"),
join("dist", "bin", "main.js")
);
2024-05-19 04:02:36 +02:00
fs.readdirSync(join("dist", "bin")).forEach(fileBasename => {
2024-06-23 21:10:11 +02:00
if (/[0-9]\.index.js/.test(fileBasename) || fileBasename.endsWith(".node")) {
2024-05-19 04:02:36 +02:00
fs.rmSync(join("dist", "bin", fileBasename));
}
});
}
run(`npx tsc -p ${join("src", "bin", "tsconfig.json")}`);
2024-05-20 19:29:38 +02:00
if (
!fs
.readFileSync(join("dist", "bin", "main.js"))
.toString("utf8")
.includes("__nccwpck_require__")
) {
2024-05-20 15:34:07 +02:00
fs.cpSync(join("dist", "bin", "main.js"), join("dist", "bin", "main.original.js"));
}
2024-05-19 04:02:36 +02:00
run(`npx ncc build ${join("dist", "bin", "main.js")} -o ${join("dist", "ncc_out")}`);
transformCodebase({
2024-05-20 15:48:51 +02:00
srcDirPath: join("dist", "ncc_out"),
destDirPath: join("dist", "bin"),
transformSourceCode: ({ fileRelativePath, sourceCode }) => {
2024-05-19 04:02:36 +02:00
if (fileRelativePath === "index.js") {
return {
2024-05-20 15:48:51 +02:00
newFileName: "main.js",
modifiedSourceCode: sourceCode
2024-05-19 04:02:36 +02:00
};
}
2024-05-20 15:48:51 +02:00
return { modifiedSourceCode: sourceCode };
2024-05-19 04:02:36 +02:00
}
});
2024-05-20 15:48:51 +02:00
fs.rmSync(join("dist", "ncc_out"), { recursive: true });
2024-05-19 04:02:36 +02:00
2024-06-06 06:11:34 +02:00
{
let hasBeenPatched = false;
fs.readdirSync(join("dist", "bin")).forEach(fileBasename => {
if (fileBasename !== "main.js" && !fileBasename.endsWith(".index.js")) {
return;
}
const { hasBeenPatched: hasBeenPatched_i } = patchDeprecatedBufferApiUsage(
join("dist", "bin", fileBasename)
);
if (hasBeenPatched_i) {
hasBeenPatched = true;
}
});
assert(hasBeenPatched);
}
2024-05-19 04:02:36 +02:00
fs.chmodSync(
join("dist", "bin", "main.js"),
2024-05-20 15:48:51 +02:00
fs.statSync(join("dist", "bin", "main.js")).mode |
fs.constants.S_IXUSR |
fs.constants.S_IXGRP |
fs.constants.S_IXOTH
2024-05-19 04:02:36 +02:00
);
run(`npx tsc -p ${join("src", "tsconfig.json")}`);
run(`npx tsc-alias -p ${join("src", "tsconfig.json")}`);
if (fs.existsSync(join("dist", "vite-plugin", "index.original.js"))) {
2024-05-20 15:48:51 +02:00
fs.renameSync(
join("dist", "vite-plugin", "index.original.js"),
join("dist", "vite-plugin", "index.js")
);
2024-05-19 04:02:36 +02:00
}
run(`npx tsc -p ${join("src", "vite-plugin", "tsconfig.json")}`);
2024-05-20 15:48:51 +02:00
if (
!fs
.readFileSync(join("dist", "vite-plugin", "index.js"))
.toString("utf8")
2024-05-20 19:29:38 +02:00
.includes("__nccwpck_require__")
2024-05-20 15:48:51 +02:00
) {
fs.cpSync(
join("dist", "vite-plugin", "index.js"),
join("dist", "vite-plugin", "index.original.js")
);
2024-05-20 15:34:07 +02:00
}
2024-05-19 04:02:36 +02:00
2024-05-20 15:48:51 +02:00
run(
`npx ncc build ${join("dist", "vite-plugin", "index.js")} -o ${join(
"dist",
"ncc_out"
)}`
);
2024-05-19 04:02:36 +02:00
2024-06-23 21:10:11 +02:00
fs.readdirSync(join("dist", "ncc_out")).forEach(fileBasename => {
assert(!fileBasename.endsWith(".index.js"));
assert(!fileBasename.endsWith(".node"));
});
2024-06-06 06:11:34 +02:00
2024-05-19 04:02:36 +02:00
transformCodebase({
2024-05-20 15:48:51 +02:00
srcDirPath: join("dist", "ncc_out"),
destDirPath: join("dist", "vite-plugin"),
transformSourceCode: ({ fileRelativePath, sourceCode }) => {
2024-05-19 04:02:36 +02:00
assert(fileRelativePath === "index.js");
2024-05-20 15:48:51 +02:00
return { modifiedSourceCode: sourceCode };
2024-05-19 04:02:36 +02:00
}
});
2024-05-20 15:48:51 +02:00
fs.rmSync(join("dist", "ncc_out"), { recursive: true });
2024-05-19 04:02:36 +02:00
2024-06-06 06:11:34 +02:00
{
const { hasBeenPatched } = patchDeprecatedBufferApiUsage(
join("dist", "vite-plugin", "index.js")
);
assert(hasBeenPatched);
}
2024-09-08 00:06:47 +02:00
for (const dirBasename of [
"src",
WELL_KNOWN_DIRECTORY_BASE_NAME.RESOURCES,
WELL_KNOWN_DIRECTORY_BASE_NAME.ACCOUNT_V1
]) {
const destDirPath = join("dist", dirBasename);
2024-09-08 00:06:47 +02:00
fs.rmSync(destDirPath, { recursive: true, force: true });
fs.cpSync(dirBasename, destDirPath, { recursive: true });
}
2024-06-06 07:41:01 +02:00
transformCodebase({
srcDirPath: join("stories"),
destDirPath: join("dist", "stories"),
transformSourceCode: ({ fileRelativePath, sourceCode }) => {
if (!fileRelativePath.endsWith(".stories.tsx")) {
return undefined;
}
return { modifiedSourceCode: sourceCode };
}
});
2024-05-19 10:46:26 +02:00
console.log(chalk.green(`✓ built in ${((Date.now() - startTime) / 1000).toFixed(2)}s`));
2024-05-19 04:02:36 +02:00
function run(command: string) {
2024-05-19 10:46:26 +02:00
console.log(chalk.grey(`$ ${command}`));
2024-05-19 04:02:36 +02:00
2024-05-20 15:48:51 +02:00
child_process.execSync(command, { stdio: "inherit" });
2024-05-19 04:02:36 +02:00
}
function patchDeprecatedBufferApiUsage(filePath: string) {
const before = fs.readFileSync(filePath).toString("utf8");
const after = before.replace(
`var buffer = new Buffer(toRead);`,
`var buffer = Buffer.allocUnsafe ? Buffer.allocUnsafe(toRead) : new Buffer(toRead);`
);
fs.writeFileSync(filePath, Buffer.from(after, "utf8"));
2024-06-06 06:11:34 +02:00
const hasBeenPatched = after !== before;
return { hasBeenPatched };
}