2024-10-05 20:30:09 +02:00
|
|
|
import { assert, type Equals } from "tsafe/assert";
|
|
|
|
import type { BuildContext } from "./buildContext";
|
|
|
|
import { CUSTOM_HANDLER_ENV_NAMES } from "./constants";
|
|
|
|
import {
|
|
|
|
NOT_IMPLEMENTED_EXIT_CODE,
|
|
|
|
type CommandName,
|
|
|
|
BIN_NAME,
|
|
|
|
ApiVersion
|
|
|
|
} from "./customHandler";
|
|
|
|
import * as child_process from "child_process";
|
2024-10-25 00:01:12 +00:00
|
|
|
import { sep as pathSep } from "path";
|
2024-10-05 20:30:09 +02:00
|
|
|
import * as fs from "fs";
|
|
|
|
|
|
|
|
assert<Equals<ApiVersion, "v1">>();
|
|
|
|
|
2024-10-06 09:03:15 +02:00
|
|
|
export function maybeDelegateCommandToCustomHandler(params: {
|
2024-10-05 20:30:09 +02:00
|
|
|
commandName: CommandName;
|
|
|
|
buildContext: BuildContext;
|
2024-10-06 22:55:18 +02:00
|
|
|
}): { hasBeenHandled: boolean } {
|
2024-10-05 20:30:09 +02:00
|
|
|
const { commandName, buildContext } = params;
|
|
|
|
|
2024-10-25 00:01:12 +00:00
|
|
|
const nodeModulesBinDirPath = (() => {
|
|
|
|
const binPath = process.argv[1];
|
|
|
|
|
|
|
|
const segments: string[] = [".bin"];
|
|
|
|
|
|
|
|
let foundNodeModules = false;
|
|
|
|
|
|
|
|
for (const segment of binPath.split(pathSep).reverse()) {
|
|
|
|
skip_segment: {
|
|
|
|
if (foundNodeModules) {
|
|
|
|
break skip_segment;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (segment === "node_modules") {
|
|
|
|
foundNodeModules = true;
|
|
|
|
break skip_segment;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
segments.unshift(segment);
|
|
|
|
}
|
|
|
|
|
|
|
|
return segments.join(pathSep);
|
|
|
|
})();
|
|
|
|
|
|
|
|
if (!fs.readdirSync(nodeModulesBinDirPath).includes(BIN_NAME)) {
|
2024-10-06 22:55:18 +02:00
|
|
|
return { hasBeenHandled: false };
|
2024-10-05 20:30:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
child_process.execSync(`npx ${BIN_NAME}`, {
|
|
|
|
stdio: "inherit",
|
|
|
|
env: {
|
|
|
|
...process.env,
|
|
|
|
[CUSTOM_HANDLER_ENV_NAMES.COMMAND_NAME]: commandName,
|
|
|
|
[CUSTOM_HANDLER_ENV_NAMES.BUILD_CONTEXT]: JSON.stringify(buildContext)
|
|
|
|
}
|
|
|
|
});
|
2024-10-05 22:28:36 +02:00
|
|
|
} catch (error: any) {
|
2024-10-06 06:41:51 +02:00
|
|
|
const status = error.status;
|
2024-10-05 22:28:36 +02:00
|
|
|
|
2024-10-06 06:41:51 +02:00
|
|
|
if (status === NOT_IMPLEMENTED_EXIT_CODE) {
|
2024-10-06 22:55:18 +02:00
|
|
|
return { hasBeenHandled: false };
|
2024-10-05 20:30:09 +02:00
|
|
|
}
|
|
|
|
|
2024-10-06 06:41:51 +02:00
|
|
|
process.exit(status);
|
2024-10-05 20:30:09 +02:00
|
|
|
}
|
|
|
|
|
2024-10-06 22:55:18 +02:00
|
|
|
return { hasBeenHandled: true };
|
2024-10-05 20:30:09 +02:00
|
|
|
}
|