import { isNonEmptyStringAndNotWhitespace } from '@sindresorhus/is'; import { quote } from 'shlex'; import { logger } from '../../../logger/index.ts'; import { findGithubToken } from '../../../util/check-token.ts'; import { exec } from '../../../util/exec/index.ts'; import type { ExecOptions } from '../../../util/exec/types.ts'; import { readLocalFile } from '../../../util/fs/index.ts'; import { getRepoStatus } from '../../../util/git/index.ts'; import * as hostRules from '../../../util/host-rules.ts'; import { regEx } from '../../../util/regex.ts'; import type { UpdateArtifact, UpdateArtifactsResult } from '../types.ts'; export async function updateArtifacts({ packageFileName, config, updatedDeps, }: UpdateArtifact): Promise { const lockFileName = packageFileName.replace(regEx(/\.nix$/), '.lock'); const existingLockFileContent = await readLocalFile(lockFileName, 'utf8'); if (!existingLockFileContent) { logger.debug('No flake.lock found'); return null; } let cmd = `nix --extra-experimental-features 'nix-command flakes' `; const token = findGithubToken( hostRules.find({ hostType: 'github', url: 'https://api.github.com/', }), ); if (token) { cmd += `--extra-access-tokens github.com=${quote(token)} `; } if (config.isLockFileMaintenance) { cmd += 'flake update'; } else { const inputs = updatedDeps .map(({ depName }) => depName) .filter(isNonEmptyStringAndNotWhitespace) .map((depName) => quote(depName)) .join(' '); cmd += `flake update ${inputs}`; } const execOptions: ExecOptions = { cwdFile: packageFileName, toolConstraints: [ { toolName: 'nix', constraint: config.constraints?.nix, }, ], docker: {}, }; try { await exec(cmd, execOptions); const status = await getRepoStatus(); if (!status.modified.includes(lockFileName)) { return null; } logger.debug('Returning updated flake.lock'); return [ { file: { type: 'addition', path: lockFileName, contents: await readLocalFile(lockFileName), }, }, ]; } catch (err) { logger.warn({ err }, 'Error updating flake.lock'); return [ { artifactError: { fileName: lockFileName, stderr: err.message, }, }, ]; } }