108 lines
3.2 KiB
TypeScript
Raw Normal View History

import { generateSrcMainResources } from "./generateSrcMainResources";
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";
2024-05-20 02:27:40 +02:00
import { vitePluginSubScriptEnvNames, skipBuildJarsEnvName } 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";
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-09 09:15:16 +02:00
await generateSrcMainResources({ buildContext });
2024-05-12 19:38:48 +02:00
2024-03-05 19:42:49 +01:00
run_post_build_script: {
2024-06-09 09:15:16 +02:00
if (buildContext.bundler !== "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-09 09:15:16 +02:00
cwd: buildContext.projectDirPath,
2024-05-20 15:48:51 +02:00
env: {
2024-03-05 19:42:49 +01:00
...process.env,
2024-05-20 15:48:51 +02:00
[vitePluginSubScriptEnvNames.runPostBuildScript]:
2024-06-09 09:15:16 +02:00
JSON.stringify(buildContext)
2024-03-05 19:42:49 +01:00
}
});
}
2024-05-20 02:27:40 +02:00
build_jars: {
if (process.env[skipBuildJarsEnvName]) {
break build_jars;
}
2024-06-09 09:15:16 +02:00
await buildJars({ buildContext });
2024-05-20 02:27:40 +02:00
}
2024-05-20 15:48:51 +02:00
console.log(
chalk.green(`✓ built in ${((Date.now() - startTime) / 1000).toFixed(2)}s`)
);
}