All files / core/blong-browser/src sourceLinkDefine.ts

86.66% Statements 13/15
84.21% Branches 16/19
100% Functions 3/3
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69                                                                    9x 9x 25x 9x   16x 16x 16x           1x 1x               8x 8x 8x               8x    
/**
 * The source-link environment a Vite or Storybook build injects for the TestRun console.
 *
 * The browser cannot read `process.env`, and the source pointers a run captures are
 * either absolute (`file:///…`) or relative to the Vite root (`/s/browser/test/…`).
 * The package root and the workspace root are what turn the second kind into a
 * repository path, so they are resolved here, at build time, and replaced in the
 * bundle as `globalThis.__BLONG_SOURCE_LINK__`.
 */
import {existsSync} from 'node:fs';
import {dirname, join, relative, sep} from 'node:path';
import {fileURLToPath} from 'node:url';
 
/** What the browser reads back from `globalThis.__BLONG_SOURCE_LINK__`. */
export interface IInjectedSourceLink {
    /** The process is a captured CI run, so links point at the repository, not the editor. */
    ci: boolean;
    /** GitHub `owner/repository`, from `GITHUB_REPOSITORY`. */
    repository: string;
    /** PR head branch, tag or SHA, from `GITHUB_HEAD_REF`/`GITHUB_REF_NAME`. */
    branch: string;
    /** Absolute path of the monorepo root. */
    workspaceRoot: string;
    /** Package path from the workspace root, e.g. `realm/blong-access`. */
    packagePath: string;
    /**
     * The remote segment of a `vscode://` URI, from `BLONG_VSCODE_REMOTE` or
     * `VSCODE_REMOTE_AUTHORITY`, e.g. `vscode-remote/ssh-remote+ms-02`.
     */
    vscodeRemote?: string;
}
 
/** Walk up from `start` to the first folder holding `rush.json` or `.git`. */
export function workspaceRootOf(start: string): string {
    let current = start;
    for (;;) {
        if (existsSync(join(current, 'rush.json')) || existsSync(join(current, '.git'))) {
            return current;
        }
        const parent = dirname(current);
        Iif (parent === current) return start;
        current = parent;
    }
}
 
/** The package root a Vite config is written in. */
export function packageRootOf(importMetaUrl: string): string {
    Iif (!importMetaUrl.startsWith('file:')) return process.cwd();
    return dirname(fileURLToPath(importMetaUrl));
}
 
/** The `define` entry, shaped as the global the console reads. */
export function sourceLinkDefine(
    packageRoot: string,
    env: NodeJS.ProcessEnv = process.env,
): Record<string, string> {
    const workspaceRoot = workspaceRootOf(packageRoot);
    const packagePath = relative(workspaceRoot, packageRoot).split(sep).join('/');
    const value: IInjectedSourceLink = {
        ci: Boolean(env['CI']),
        repository: env['GITHUB_REPOSITORY'] || 'feasibleone/blong',
        branch: env['GITHUB_HEAD_REF'] || env['GITHUB_REF_NAME'] || 'main',
        workspaceRoot,
        packagePath: packagePath.startsWith('..') ? '' : packagePath,
        vscodeRemote: env['BLONG_VSCODE_REMOTE'] || env['VSCODE_REMOTE_AUTHORITY'] || '',
    };
    return {'globalThis.__BLONG_SOURCE_LINK__': JSON.stringify(value)};
}