hoppscotch-backend-git/scripts/prepare-webapp-frontend.ts
tlemesle d8a27e9839 Show local user details on desktop confirm login
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-24 15:44:15 +02:00

69 lines
2.4 KiB
TypeScript

#!/usr/bin/env bun
/**
* Extract Hoppscotch selfhost-web assets from the official Docker image
* and inject VITE_* backend URLs via import-meta-env.
*/
import { mkdir, rm, writeFile } from 'node:fs/promises';
import { join, resolve } from 'node:path';
import { tmpdir } from 'node:os';
const root = resolve(import.meta.dir, '..');
const outDir = resolve(root, process.env.WEBAPP_FRONTEND_PATH || 'webapp-frontend');
const image = process.env.HOPP_DOCKER_IMAGE || 'hoppscotch/hoppscotch:2026.6.1';
const backendOrigin = (process.env.BACKEND_PUBLIC_URL || 'http://127.0.0.1:3200').replace(/\/$/, '');
const viteEnv = {
VITE_BASE_URL: backendOrigin,
VITE_SHORTCODE_BASE_URL: backendOrigin,
VITE_ADMIN_URL: backendOrigin,
VITE_BACKEND_GQL_URL: `${backendOrigin}/graphql`,
VITE_BACKEND_WS_URL: `${backendOrigin.replace(/^http/, 'ws')}/graphql`,
VITE_BACKEND_API_URL: `${backendOrigin}/v1`,
VITE_APP_TOS_LINK: 'https://docs.hoppscotch.io/support/terms',
VITE_APP_PRIVACY_POLICY_LINK: 'https://docs.hoppscotch.io/support/privacy',
};
const run = async (cmd: string[]) => {
const proc = Bun.spawn(cmd, {
cwd: root,
stdout: 'inherit',
stderr: 'inherit',
});
const code = await proc.exited;
if(code !== 0)
throw new Error(`Command failed (${code}): ${cmd.join(' ')}`);
};
console.log(`Pulling frontend from ${image}${outDir}`);
await rm(outDir, { recursive: true, force: true });
await mkdir(outDir, { recursive: true });
await run([
'docker', 'run', '--rm',
'-v', `${outDir}:/out`,
'--entrypoint', 'sh',
image,
'-c', 'cp -a /site/selfhost-web/. /out/',
]);
const envDir = join(tmpdir(), `hopp-env-${crypto.randomUUID()}`);
await mkdir(envDir, { recursive: true });
const buildEnvPath = join(envDir, 'build.env');
await writeFile(
buildEnvPath,
Object.entries(viteEnv).map(([key, val]) => `${key}="${val}"`).join('\n'),
);
const importMetaEnvBin = resolve(root, 'node_modules/@import-meta-env/cli/bin/import-meta-env.js');
console.log(`Injecting backend URLs for ${backendOrigin}`);
await run([
'bun', importMetaEnvBin,
'-x', buildEnvPath,
'-e', buildEnvPath,
'-p', `${outDir}/**/*`,
]);
await rm(envDir, { recursive: true, force: true });
console.log('Patching device-login confirm screen');
await run(['bun', resolve(root, 'scripts/patch-device-login-webapp.ts')]);
console.log('webapp-frontend ready');