Files
renovate/lib/modules/manager/pub/artifacts.ts
T
Sebastian Poxhofer 9717259ffc refactor(oxlint): enable no-else-return and unicorn/no-lonely-if (#44553)
Enable no-else-return (with allowElseIf: false) and unicorn/no-lonely-if
as errors in .oxlintrc.json and fix all 131 pre-existing violations
(91 no-else-return, 40 unicorn/no-lonely-if) across 98 files.

The no-else-return violations were fixed with the oxlint autofix
(iterated to a fixpoint, plus two else-if chains that required
--fix-dangerously) followed by Prettier formatting. One comment dropped
by the autofix in lib/modules/manager/pub/artifacts.ts was restored
manually. The unicorn/no-lonely-if violations were merged by hand,
preserving comments and coverage-ignore hints.

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-14 12:11:21 +00:00

130 lines
3.8 KiB
TypeScript

import { isEmptyArray, isString } from '@sindresorhus/is';
import { quote } from 'shlex';
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 {
getSiblingFileName,
readLocalFile,
writeLocalFile,
} from '../../../util/fs/index.ts';
import type {
UpdateArtifact,
UpdateArtifactsResult,
Upgrade,
} from '../types.ts';
import { parsePubspec, parsePubspecLock } from './utils.ts';
const SDK_NAMES = ['dart', 'flutter'];
const PUB_GET_COMMAND = 'pub get --no-precompile';
export async function updateArtifacts({
packageFileName,
updatedDeps,
newPackageFileContent,
config,
}: UpdateArtifact): Promise<UpdateArtifactsResult[] | null> {
logger.debug(`pub.updateArtifacts(${packageFileName})`);
const { isLockFileMaintenance } = config;
if (isEmptyArray(updatedDeps) && !isLockFileMaintenance) {
logger.debug('No updated pub deps - returning null');
return null;
}
const lockFileName = getSiblingFileName(packageFileName, 'pubspec.lock');
const oldLockFileContent = await readLocalFile(lockFileName, 'utf8');
if (!oldLockFileContent) {
logger.debug('No pubspec.lock found');
return null;
}
try {
await writeLocalFile(packageFileName, newPackageFileContent);
const isFlutter = newPackageFileContent.includes('sdk: flutter');
const toolName = isFlutter ? 'flutter' : 'dart';
const cmd = getExecCommand(toolName, updatedDeps, isLockFileMaintenance);
let constraint = config.constraints?.[toolName];
if (!constraint) {
const pubspec = parsePubspec(packageFileName, newPackageFileContent);
const pubspecToolName = isFlutter ? 'flutter' : 'sdk';
constraint = pubspec?.environment[pubspecToolName];
if (!constraint) {
const pubspecLock = parsePubspecLock(lockFileName, oldLockFileContent);
constraint = pubspecLock?.sdks[toolName];
}
}
const execOptions: ExecOptions = {
cwdFile: packageFileName,
docker: {},
toolConstraints: [
{
toolName,
constraint,
},
],
};
await exec(cmd, execOptions);
const newLockFileContent = await readLocalFile(lockFileName, 'utf8');
if (oldLockFileContent === newLockFileContent) {
return null;
}
return [
{
file: {
type: 'addition',
path: lockFileName,
contents: newLockFileContent,
},
},
];
} catch (err) {
// istanbul ignore if
if (err.message === TEMPORARY_ERROR) {
throw err;
}
logger.warn({ lockfile: lockFileName, err }, `Failed to update lock file`);
return [
{
artifactError: {
fileName: lockFileName,
stderr: err.message,
},
},
];
}
}
function getExecCommand(
toolName: string,
updatedDeps: Upgrade<Record<string, unknown>>[],
isLockFileMaintenance: boolean | undefined,
): string {
if (isLockFileMaintenance) {
return `${toolName} pub upgrade`;
}
const depNames = updatedDeps.map((dep) => dep.depName).filter(isString);
if (depNames.length === 1 && SDK_NAMES.includes(depNames[0])) {
return `${toolName} ${PUB_GET_COMMAND}`;
}
// If there are two updated dependencies and both of them are SDK updates (Dart and Flutter),
// we use Flutter over Dart to run `pub get` as it is a Flutter project.
if (
depNames.length === 2 &&
depNames.filter((depName) => SDK_NAMES.includes(depName)).length === 2
) {
return `flutter ${PUB_GET_COMMAND}`;
}
const depNamesCmd = depNames
.filter((depName) => !SDK_NAMES.includes(depName))
.map(quote)
.join(' ');
return `${toolName} pub upgrade ${depNamesCmd}`;
}