diff --git a/ee/apps/den-api/scripts/build.mjs b/ee/apps/den-api/scripts/build.mjs index 5fe9bab9b9..badbcfa730 100644 --- a/ee/apps/den-api/scripts/build.mjs +++ b/ee/apps/den-api/scripts/build.mjs @@ -1,14 +1,14 @@ import { spawnSync } from "node:child_process" import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs" import path from "node:path" -import { fileURLToPath } from "node:url" +import { fileURLToPath, pathToFileURL } from "node:url" const scriptDir = path.dirname(fileURLToPath(import.meta.url)) const serviceDir = path.resolve(scriptDir, "..") const repoRoot = path.resolve(serviceDir, "..", "..", "..") const desktopPackagePath = path.join(repoRoot, "apps", "desktop", "package.json") const generatedVersionPath = path.join(serviceDir, "src", "generated", "app-version.ts") -const pnpmCommand = process.platform === "win32" ? "pnpm.cmd" : "pnpm" +const pnpmCommand = "pnpm" const fallbackAppVersion = "0.0.0" function readDesktopVersion() { @@ -40,21 +40,47 @@ function writeGeneratedVersionFile(latestAppVersion) { ) } +export function quoteShellArg(value) { + return `"${String(value).replace(/"/g, '\\"')}"` +} + +export function createPnpmSpawnInput(command, args, platform = process.platform) { + const useShell = platform === "win32" + return { + command: useShell ? [command, ...args].map(quoteShellArg).join(" ") : command, + args: useShell ? [] : args, + shell: useShell, + } +} + function run(command, args) { - const result = spawnSync(command, args, { + const input = createPnpmSpawnInput(command, args) + const result = spawnSync(input.command, input.args, { cwd: serviceDir, env: process.env, stdio: "inherit", + shell: input.shell, }) + if (result.error) { + console.error(result.error) + process.exit(1) + } + if (result.status !== 0) { process.exit(result.status ?? 1) } } -process.env.DEN_API_LATEST_APP_VERSION = process.env.DEN_API_LATEST_APP_VERSION || readDesktopVersion() -writeGeneratedVersionFile(process.env.DEN_API_LATEST_APP_VERSION) +function main() { + process.env.DEN_API_LATEST_APP_VERSION = process.env.DEN_API_LATEST_APP_VERSION || readDesktopVersion() + writeGeneratedVersionFile(process.env.DEN_API_LATEST_APP_VERSION) -run(pnpmCommand, ["run", "build:email"]) -run(pnpmCommand, ["run", "build:den-db"]) -run(pnpmCommand, ["exec", "tsc", "-p", "tsconfig.json"]) + run(pnpmCommand, ["run", "build:email"]) + run(pnpmCommand, ["run", "build:den-db"]) + run(pnpmCommand, ["exec", "tsc", "-p", "tsconfig.json"]) +} + +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { + main() +} diff --git a/ee/apps/den-api/src/generated/app-version.ts b/ee/apps/den-api/src/generated/app-version.ts index 9a4e741eab..1e43bedf9d 100644 --- a/ee/apps/den-api/src/generated/app-version.ts +++ b/ee/apps/den-api/src/generated/app-version.ts @@ -1 +1 @@ -export const BUILD_LATEST_APP_VERSION = "0.13.8" as const +export const BUILD_LATEST_APP_VERSION = "0.16.2" as const diff --git a/ee/apps/den-api/test/build-script.test.ts b/ee/apps/den-api/test/build-script.test.ts new file mode 100644 index 0000000000..ccccb22e67 --- /dev/null +++ b/ee/apps/den-api/test/build-script.test.ts @@ -0,0 +1,25 @@ +import { describe, expect, test } from "bun:test" +import { createPnpmSpawnInput } from "../scripts/build.mjs" + +describe("den-api build script", () => { + test("quotes the pnpm command when Windows shell mode is required", () => { + const input = createPnpmSpawnInput("C:\\Program Files\\pnpm (den)\\pnpm.cmd", ["exec", "tsc", "-p", "tsconfig.json"], "win32") + + expect(input).toEqual({ + command: '"C:\\Program Files\\pnpm (den)\\pnpm.cmd" "exec" "tsc" "-p" "tsconfig.json"', + args: [], + shell: true, + }) + }) + + test("keeps direct argv spawning on non-Windows platforms", () => { + const args = ["exec", "tsc", "-p", "tsconfig.json"] + const input = createPnpmSpawnInput("pnpm", args, "linux") + + expect(input).toEqual({ + command: "pnpm", + args, + shell: false, + }) + }) +})