import { TEMPORARY_ERROR } from '../../../constants/error-messages.ts'; import { logger } from '../../../logger/index.ts'; import { coerceArray } from '../../../util/array.ts'; import type { ExecOptions } from '../../../util/exec/types.ts'; import { getParentDir, getSiblingFileName, readLocalFile, writeLocalFile, } from '../../../util/fs/index.ts'; import { withGitEnvironment } from '../../../util/git/exec.ts'; import { getRepoStatus } from '../../../util/git/index.ts'; import type { UpdateArtifact, UpdateArtifactsResult } from '../types.ts'; const gitExec = withGitEnvironment(); export async function updateArtifacts({ packageFileName, newPackageFileContent, config, }: UpdateArtifact): Promise { logger.debug(`vendir.updateArtifacts(${packageFileName})`); const lockFileName = getSiblingFileName(packageFileName, 'vendir.lock.yml'); if (!lockFileName) { logger.warn('No vendir.lock.yml found'); return null; } const existingLockFileContent = await readLocalFile(lockFileName, 'utf8'); if (!existingLockFileContent) { logger.warn('Empty vendir.lock.yml found'); return null; } try { await writeLocalFile(packageFileName, newPackageFileContent); logger.debug('Updating Vendir artifacts'); const execOptions: ExecOptions = { cwdFile: packageFileName, docker: {}, toolConstraints: [ { toolName: 'vendir', constraint: config.constraints?.vendir }, { toolName: 'helm', constraint: config.constraints?.helm }, ], }; await gitExec(`vendir sync`, execOptions); logger.debug('Returning updated Vendir artifacts'); const fileChanges: UpdateArtifactsResult[] = []; const newVendirLockContent = await readLocalFile(lockFileName, 'utf8'); const isLockFileChanged = existingLockFileContent !== newVendirLockContent; if (isLockFileChanged) { fileChanges.push({ file: { type: 'addition', path: lockFileName, contents: newVendirLockContent, }, }); } // add modified vendir archives to artifacts logger.debug("Adding Sync'd files to git"); // Files must be in the vendor path to get added const vendorDir = getParentDir(packageFileName); const status = await getRepoStatus(); if (status) { const modifiedFiles = coerceArray(status.modified); const notAddedFiles = status.not_added; const deletedFiles = coerceArray(status.deleted); for (const f of modifiedFiles.concat(notAddedFiles)) { const isFileInVendorDir = f.startsWith(vendorDir); if (vendorDir || isFileInVendorDir) { fileChanges.push({ file: { type: 'addition', path: f, contents: await readLocalFile(f), }, }); } } for (const f of deletedFiles) { fileChanges.push({ file: { type: 'deletion', path: f, }, }); } } else { logger.error('Failed to get git status'); } return fileChanges.length ? fileChanges : null; } catch (err) { if (err.message === TEMPORARY_ERROR) { throw err; } logger.debug({ err }, 'Failed to update Vendir lock file'); return [ { artifactError: { fileName: lockFileName, stderr: err.message, }, }, ]; } }