|
| 1 | +/* eslint-disable turbo/no-undeclared-env-vars */ |
| 2 | + |
| 3 | +import os from 'node:os'; |
| 4 | +import path from 'node:path'; |
| 5 | + |
| 6 | +import { stateFile } from '../models/stateFile'; |
| 7 | +import { awaitableTreekill, fs, waitForServer } from './index'; |
| 8 | +import { run } from './run'; |
| 9 | + |
| 10 | +export const startClerkJsHttpServer = async () => { |
| 11 | + if (process.env.E2E_APP_CLERK_JS) { |
| 12 | + return; |
| 13 | + } |
| 14 | + if (!process.env.CI) { |
| 15 | + await copyClerkJsToTempDir(); |
| 16 | + } |
| 17 | + return serveFromTempDir(); |
| 18 | +}; |
| 19 | + |
| 20 | +export const killClerkJsHttpServer = async () => { |
| 21 | + const clerkJsHttpServerPid = stateFile.getClerkJsHttpServerPid(); |
| 22 | + if (clerkJsHttpServerPid) { |
| 23 | + console.log('Killing clerkJsHttpServer', clerkJsHttpServerPid); |
| 24 | + await awaitableTreekill(clerkJsHttpServerPid, 'SIGKILL'); |
| 25 | + } |
| 26 | +}; |
| 27 | + |
| 28 | +// If we are running the tests locally, then clerk.browser.js should be built already |
| 29 | +// so we simply copy it from packages/clerk to the same location as CICD would install it |
| 30 | +const copyClerkJsToTempDir = async () => { |
| 31 | + const clerkJsTempDir = getClerkJsTempDir(); |
| 32 | + await fs.remove(clerkJsTempDir); |
| 33 | + await fs.ensureDir(clerkJsTempDir); |
| 34 | + const packagesClerkJsDistPath = path.join(process.cwd(), 'packages/clerk-js/dist'); |
| 35 | + fs.copySync(packagesClerkJsDistPath, clerkJsTempDir); |
| 36 | +}; |
| 37 | + |
| 38 | +const serveFromTempDir = async () => { |
| 39 | + console.log('Serving clerkJs from temp dir'); |
| 40 | + const port = 18211; |
| 41 | + const serverUrl = `http://localhost:${port}`; |
| 42 | + const now = Date.now(); |
| 43 | + const TMP_DIR = path.join(process.cwd(), '.temp_integration'); |
| 44 | + const stdoutFilePath = path.resolve(TMP_DIR, `clerkJsHttpServer.${now}.log`); |
| 45 | + const stderrFilePath = path.resolve(TMP_DIR, `clerkJsHttpServer.${now}.err.log`); |
| 46 | + const clerkJsTempDir = getClerkJsTempDir(); |
| 47 | + const proc = run(`node_modules/.bin/http-server ${clerkJsTempDir} -d --gzip --cors -a localhost`, { |
| 48 | + cwd: process.cwd(), |
| 49 | + env: { PORT: port.toString() }, |
| 50 | + detached: true, |
| 51 | + stdout: fs.openSync(stdoutFilePath, 'a'), |
| 52 | + stderr: fs.openSync(stderrFilePath, 'a'), |
| 53 | + }); |
| 54 | + stateFile.setClerkJsHttpServerPid(proc.pid); |
| 55 | + await waitForServer(serverUrl, { log: console.log, maxAttempts: Infinity }); |
| 56 | + console.log('clerk.browser.js is being served from', serverUrl); |
| 57 | +}; |
| 58 | + |
| 59 | +// The location where the clerk.browser.js is served from |
| 60 | +// For simplicity, on CICD we install `@clerk/clerk-js` on osTemp |
| 61 | +// so the actual clerk.browser.file is at osTemp/clerk-js/node_modules/@clerk/clerk-js/dist |
| 62 | +// Locally, it's the osTemp/clerk-js/node_modules/@clerk/clerk-js/dist |
| 63 | +// You can override it by setting the `E2E_APP_CLERK_JS_DIR` env variable |
| 64 | +const getClerkJsTempDir = () => { |
| 65 | + const osTempDir = process.env.E2E_APP_CLERK_JS_DIR || os.tmpdir(); |
| 66 | + return path.join(osTempDir, ...'clerk-js/node_modules/@clerk/clerk-js/dist'.split('/')); |
| 67 | +}; |
0 commit comments