Files
api/plugin/builder/build-plugin.ts
Pujit Mehrotra 97ab6fbe32 refactor(api): vendor node_modules instead of pnpm store (#1346)
due to issues and redundancies in vendoring postinstall side-effects, such as compiled bindings for libvirt, we reverted to vendoring `node_modules`, installed via `npm` instead of a global pnpm store generated by `pnpm`.

This should resolve runtime issues with e.g. the libvirt bindings because `node_modules` will contain the correct "side-effects."

## Summary by CodeRabbit

- **New Features**
- Introduced a command to remove stale archive files during the cleanup
process.
  - Added functionality to archive the `node_modules` directory.
- Enhanced dependency resolution with new overrides for specific
packages.

- **Chores**
- Updated dependency settings by replacing one key dependency with an
alternative and removing two unused ones, ensuring optimal deployment.
- Enhanced the installation process to operate strictly in offline mode.
- Updated artifact naming conventions for clarity and consistency in
workflows.
- Modified volume mappings in the Docker Compose configuration to
reflect new artifact names.
- Improved error handling in the GitHub Actions workflow by adding
checks for required files.
- Updated references in the build process to use a vendor store instead
of the PNPM store.
- Removed the management of PNPM store archives from the build process.
2025-04-09 15:17:50 -04:00

119 lines
3.4 KiB
TypeScript

import { readFile, writeFile, mkdir, rename } from "fs/promises";
import { $ } from "zx";
import { escape as escapeHtml } from "html-sloppy-escaper";
import { dirname, join } from "node:path";
import { getTxzName, pluginName, startingDir } from "./utils/consts";
import { getAssetUrl, getPluginUrl } from "./utils/bucket-urls";
import { getMainTxzUrl } from "./utils/bucket-urls";
import {
deployDir,
getDeployPluginPath,
getRootPluginPath,
} from "./utils/paths";
import { PluginEnv, setupPluginEnv } from "./cli/setup-plugin-environment";
import { cleanupPluginFiles } from "./utils/cleanup";
import { bundleVendorStore, getVendorBundleName } from "./build-vendor-store";
/**
* Check if git is available
*/
const checkGit = async () => {
try {
await $`git log -1 --pretty=%B`;
} catch (err) {
console.error(`Error: git not available: ${err}`);
throw new Error(`Git not available: ${err}`);
}
};
const moveTxzFile = async (txzPath: string, pluginVersion: string) => {
const txzName = getTxzName(pluginVersion);
await rename(txzPath, join(deployDir, txzName));
};
function updateEntityValue(
xmlString: string,
entityName: string,
newValue: string
) {
console.log("Updating entity:", entityName, "with value:", newValue);
const regex = new RegExp(`<!ENTITY ${entityName} "[^"]*">`);
if (regex.test(xmlString)) {
return xmlString.replace(regex, `<!ENTITY ${entityName} "${newValue}">`);
}
throw new Error(`Entity ${entityName} not found in XML`);
}
const buildPlugin = async ({
pluginVersion,
baseUrl,
tag,
txzSha256,
releaseNotes,
}: PluginEnv) => {
// Update plg file
let plgContent = await readFile(getRootPluginPath({ startingDir }), "utf8");
// Update entity values
const entities: Record<string, string> = {
name: pluginName,
version: pluginVersion,
pluginURL: getPluginUrl({ baseUrl, tag }),
MAIN_TXZ: getMainTxzUrl({ baseUrl, pluginVersion, tag }),
TXZ_SHA256: txzSha256,
VENDOR_STORE_URL: getAssetUrl({ baseUrl, tag }, getVendorBundleName()),
VENDOR_STORE_FILENAME: getVendorBundleName(),
...(tag ? { TAG: tag } : {}),
};
console.log("Entities:", entities);
// Iterate over entities and update them
Object.entries(entities).forEach(([key, value]) => {
if (!value) {
throw new Error(
`Entity ${key} not set in entities: ${JSON.stringify(entities)}`
);
}
plgContent = updateEntityValue(plgContent, key, value);
});
if (releaseNotes) {
// Update the CHANGES section with release notes
plgContent = plgContent.replace(
/<CHANGES>.*?<\/CHANGES>/s,
`<CHANGES>\n${escapeHtml(releaseNotes)}\n</CHANGES>`
);
}
await mkdir(dirname(getDeployPluginPath({ startingDir })), {
recursive: true,
});
console.log("Writing plg file to:", getDeployPluginPath({ startingDir }));
await writeFile(getDeployPluginPath({ startingDir }), plgContent);
};
/**
* Main build script
*/
const main = async () => {
try {
const validatedEnv = await setupPluginEnv(process.argv);
if (validatedEnv.tag === "LOCAL_PLUGIN_BUILD") {
console.log("Skipping git check for LOCAL_PLUGIN_BUILD");
} else {
await checkGit();
}
await cleanupPluginFiles();
await buildPlugin(validatedEnv);
await moveTxzFile(validatedEnv.txzPath, validatedEnv.pluginVersion);
await bundleVendorStore();
} catch (error) {
console.error(error);
process.exit(1);
}
};
await main();