117 lines
3.4 KiB
TypeScript
Raw Normal View History

import { generateResources } from "./generateResources";
import { join as pathJoin, relative as pathRelative, sep as pathSep } from "path";
import * as child_process from "child_process";
import * as fs from "fs";
2024-06-09 09:15:16 +02:00
import { getBuildContext } from "../shared/buildContext";
import { vitePluginSubScriptEnvNames } from "../shared/constants";
import { buildJars } from "./buildJars";
import type { CliCommandOptions } from "../main";
2024-05-18 10:24:55 +02:00
import chalk from "chalk";
import { readThisNpmPackageVersion } from "../tools/readThisNpmPackageVersion";
2024-05-18 10:46:48 +02:00
import * as os from "os";
2024-06-10 07:57:12 +02:00
import { rmSync } from "../tools/fs.rmSync";
export async function command(params: { cliCommandOptions: CliCommandOptions }) {
2024-05-18 11:40:09 +02:00
exit_if_maven_not_installed: {
2024-05-18 10:46:48 +02:00
let commandOutput: Buffer | undefined = undefined;
try {
2024-05-20 15:48:51 +02:00
commandOutput = child_process.execSync("mvn --version", {
stdio: ["ignore", "pipe", "ignore"]
});
2024-05-18 10:46:48 +02:00
} catch {}
2024-05-19 04:44:38 +02:00
if (commandOutput?.toString("utf8").includes("Apache Maven")) {
2024-05-18 11:40:09 +02:00
break exit_if_maven_not_installed;
2024-05-18 10:46:48 +02:00
}
const installationCommand = (() => {
switch (os.platform()) {
case "darwin":
2024-05-19 04:44:38 +02:00
return "brew install mvn";
2024-05-18 10:46:48 +02:00
case "win32":
2024-05-19 04:44:38 +02:00
return "choco install mvn";
2024-05-18 10:46:48 +02:00
case "linux":
default:
2024-05-19 04:44:38 +02:00
return "sudo apt-get install mvn";
2024-05-18 10:46:48 +02:00
}
})();
2024-05-20 15:48:51 +02:00
console.log(
`${chalk.red("Apache Maven required.")} Install it with \`${chalk.bold(
installationCommand
)}\` (for example)`
);
2024-05-18 10:46:48 +02:00
process.exit(1);
}
2024-05-18 11:09:04 +02:00
const { cliCommandOptions } = params;
2024-06-09 09:15:16 +02:00
const buildContext = getBuildContext({ cliCommandOptions });
2024-05-18 10:24:55 +02:00
console.log(
[
chalk.cyan(`keycloakify v${readThisNpmPackageVersion()}`),
2024-05-20 15:48:51 +02:00
chalk.green(
`Building the keycloak theme in .${pathSep}${pathRelative(
process.cwd(),
2024-06-09 09:15:16 +02:00
buildContext.keycloakifyBuildDirPath
2024-05-20 15:48:51 +02:00
)} ...`
)
2024-05-18 10:24:55 +02:00
].join(" ")
);
2024-05-18 10:24:55 +02:00
const startTime = Date.now();
2024-05-12 19:37:16 +02:00
{
2024-06-09 09:15:16 +02:00
if (!fs.existsSync(buildContext.keycloakifyBuildDirPath)) {
fs.mkdirSync(buildContext.keycloakifyBuildDirPath, {
2024-05-20 15:48:51 +02:00
recursive: true
});
2024-05-12 19:38:48 +02:00
}
2024-05-20 15:48:51 +02:00
fs.writeFileSync(
2024-06-09 09:15:16 +02:00
pathJoin(buildContext.keycloakifyBuildDirPath, ".gitignore"),
2024-05-20 15:48:51 +02:00
Buffer.from("*", "utf8")
);
2024-05-12 19:38:48 +02:00
}
2024-06-10 07:57:12 +02:00
const resourcesDirPath = pathJoin(buildContext.keycloakifyBuildDirPath, "resources");
await generateResources({
2024-06-10 07:57:12 +02:00
resourcesDirPath,
buildContext
});
2024-05-12 19:38:48 +02:00
2024-03-05 19:42:49 +01:00
run_post_build_script: {
if (buildContext.bundler.type !== "vite") {
2024-03-05 19:42:49 +01:00
break run_post_build_script;
}
2024-03-05 19:42:49 +01:00
child_process.execSync("npx vite", {
2024-06-10 09:12:24 +02:00
cwd: buildContext.projectDirPath,
2024-05-20 15:48:51 +02:00
env: {
2024-03-05 19:42:49 +01:00
...process.env,
[vitePluginSubScriptEnvNames.runPostBuildScript]: JSON.stringify({
resourcesDirPath,
buildContext
})
2024-03-05 19:42:49 +01:00
}
});
}
await buildJars({
resourcesDirPath,
buildContext
});
2024-06-10 07:57:12 +02:00
2024-06-10 09:32:07 +02:00
rmSync(resourcesDirPath, { recursive: true });
2024-05-20 15:48:51 +02:00
console.log(
2024-06-10 09:32:07 +02:00
chalk.green(
`✓ keycloak theme built in ${((Date.now() - startTime) / 1000).toFixed(2)}s`
)
2024-05-20 15:48:51 +02:00
);
}