refactor: Stamp build information in as "defines" (#2654)

This commit is contained in:
Dermot Duffy
2026-08-03 22:36:32 -07:00
committed by GitHub
parent 2d124fe3cd
commit 06bcfd58ba
13 changed files with 133 additions and 148 deletions
+44
View File
@@ -0,0 +1,44 @@
import { execFileSync } from 'node:child_process';
import { readFileSync } from 'node:fs';
/**
* Asks git something or gives back nothing when it cannot be asked.
*/
const askGit = (...args) => {
try {
return execFileSync('git', args, {
encoding: 'utf-8',
stdio: ['ignore', 'pipe', 'ignore'],
}).trim();
} catch {
return '';
}
};
const getPackageVersion = () =>
JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf-8')).version;
/**
* What the build stamps into the card, as names for the bundler to substitute.
*
* `releaseVersion` is the version being released, which only the release
* workflow knows; a build without one reports the version in `package.json`, or
* a development build the commit it was made from.
*
* The values are JSON so that a bundler can drop them in as written.
*/
export const getBuildDefines = ({ dev, releaseVersion }) => {
const gitHash = askGit('rev-parse', '--short', 'HEAD');
const developmentVersion = gitHash ? `dev+${gitHash}` : 'dev';
return {
__ADVANCED_CAMERA_CARD_RELEASE_VERSION__: JSON.stringify(
releaseVersion ?? (dev ? developmentVersion : getPackageVersion()),
),
__ADVANCED_CAMERA_CARD_GIT_HASH__: JSON.stringify(gitHash),
__ADVANCED_CAMERA_CARD_GIT_DATE__: JSON.stringify(
askGit('log', '-1', '--format=%cI'),
),
__ADVANCED_CAMERA_CARD_BUILD_DATE__: JSON.stringify(new Date().toISOString()),
};
};
-25
View File
@@ -1,25 +0,0 @@
import { RELEASE_VERSION_TOKEN } from './release-version.js';
// What the card substitutes to mean "the version in package.json". The
// development substitution is not used here because it appends a git hash that
// only a build step knows.
const PACKAGE_VERSION = 'pkg';
/**
* Substitutes the release version the way the build does.
*
* The card reads it out of a string literal that Rollup rewrites, so without
* this it renders the placeholder itself: the loading screen shows the raw
* token, which then appears in every failure screenshot.
*/
export const releaseVersion = () => ({
name: 'release-version',
transform(code, id) {
if (!id.includes('src/utils/diagnostics.ts')) {
return null;
}
return { code: code.replace(RELEASE_VERSION_TOKEN, PACKAGE_VERSION), map: null };
},
});
-12
View File
@@ -1,12 +0,0 @@
/**
* The literal the card carries in place of its version, for the build to
* rewrite.
*
* `getReleaseVersion` cannot import this: it has to sit in that source as a
* plain string for the build to have something to replace. It lives in its own
* module because the build, the browser tests and a unit test all have to agree
* on it, and none of them should have to depend on either of the others.
*
* It must be in a JS file as Node's loader cannot import TypeScript.
*/
export const RELEASE_VERSION_TOKEN = '__ADVANCED_CAMERA_CARD_RELEASE_VERSION__';