Files
Sebastian Poxhofer bf5ff94d28 refactor(manager): build artifact results with the shared helpers (#46092)
Migrate apm, bun, cargo, cocoapods, composer, conan, copier, deno,
devbox, gomod, helmv3, jsonnet-bundler, kustomize, mix, nuget and vendir
onto `fileAddition()`, `artifactError()` and `artifactErrorResult()`.
copier's private `artifactError()` is replaced by the shared one.

Behaviour change: artifact error text now comes from
`artifactErrorMessageFromExecError` everywhere, i.e. a non-blank
`err.stderr`, else a non-blank `err.stdout`, else `err.message`.
Previously apm used `${err}` ("Error: " prefix), cocoapods used a
nullish coalesce that kept an empty stderr, nuget preferred stdout over
stderr, and jsonnet-bundler emitted an undefined stderr with no
fallback. The rest used `err.message` only and are unchanged for errors
without output.
2026-09-16 13:13:41 +00:00

87 lines
2.9 KiB
TypeScript

import { isNonEmptyArray } from '@sindresorhus/is';
import { TEMPORARY_ERROR } from '../../../constants/error-messages.ts';
import { logger } from '../../../logger/index.ts';
import { exec } from '../../../util/exec/index.ts';
import type { ExecOptions } from '../../../util/exec/types.ts';
import {
deleteLocalFile,
getSiblingFileName,
readLocalFile,
writeLocalFile,
} from '../../../util/fs/index.ts';
import { collectFileChanges } from '../../../util/git/file-changes.ts';
import { getRepoStatus } from '../../../util/git/index.ts';
import type { UpdateArtifact, UpdateArtifactsResult } from '../types.ts';
import {
artifactErrorResult,
fileChangesToArtifactResults,
resolveToolConstraint,
} from '../util.ts';
export async function updateArtifacts({
packageFileName,
updatedDeps,
newPackageFileContent,
config,
}: UpdateArtifact): Promise<UpdateArtifactsResult[] | null> {
logger.debug(`apm.updateArtifacts(${packageFileName})`);
const { isLockFileMaintenance } = config;
if (!isNonEmptyArray(updatedDeps) && !isLockFileMaintenance) {
logger.debug('apm: no updated deps - returning null');
return null;
}
const lockFileName = getSiblingFileName(packageFileName, 'apm.lock.yaml');
const existingLockFileContent = await readLocalFile(lockFileName, 'utf8');
if (!existingLockFileContent) {
logger.debug('apm: no lock file found');
return null;
}
try {
await writeLocalFile(packageFileName, newPackageFileContent);
if (isLockFileMaintenance) {
await deleteLocalFile(lockFileName);
}
const execOptions: ExecOptions = {
cwdFile: packageFileName,
docker: {},
toolConstraints: [
{
toolName: 'apm',
constraint: await resolveToolConstraint(config, 'apm'),
},
],
};
await exec('apm install', execOptions);
// `apm install` regenerates the lockfile and re-deploys the harness
// directories (`.github/`, `.claude/`, ...) that APM consumers commit, so
// return every file it changed - not just the lockfile - or the committed
// instruction files go stale after a bump. `apm_modules/` is the gitignored
// cache, so it is not reported here.
const status = await getRepoStatus();
const res = fileChangesToArtifactResults([
...(await collectFileChanges(status, {
include: ['modified', 'not_added'],
// the manifest itself is committed as an updated package file
filter: (path) => path !== packageFileName,
})),
...(await collectFileChanges(status, { include: ['deleted'] })),
]);
if (!res.length) {
logger.debug('apm: no changed files after install');
return null;
}
return res;
} catch (err) {
if (err.message === TEMPORARY_ERROR) {
throw err;
}
logger.debug({ err }, `Failed to update ${lockFileName}`);
return artifactErrorResult(lockFileName, err);
}
}