/
artifacts.ts
89 lines (81 loc) · 2.34 KB
/
artifacts.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import is from '@sindresorhus/is';
import { quote } from 'shlex';
import { TEMPORARY_ERROR } from '../../../constants/error-messages';
import { logger } from '../../../logger';
import { exec } from '../../../util/exec';
import type { ExecOptions } from '../../../util/exec/types';
import {
getSiblingFileName,
readLocalFile,
writeLocalFile,
} from '../../../util/fs';
import { getFile } from '../../../util/git';
import type { UpdateArtifact, UpdateArtifactsResult } from '../types';
export async function updateArtifacts({
packageFileName,
updatedDeps,
newPackageFileContent,
config,
}: UpdateArtifact): Promise<UpdateArtifactsResult[] | null> {
logger.trace(`helmfile.updateArtifacts(${packageFileName})`);
const isLockFileMaintenance = config.updateType === 'lockFileMaintenance';
if (
!isLockFileMaintenance &&
(updatedDeps === undefined || updatedDeps.length < 1)
) {
logger.debug('No updated helmfile deps - returning null');
return null;
}
const lockFileName = getSiblingFileName(packageFileName, 'helmfile.lock');
const existingLockFileContent = await getFile(lockFileName);
if (is.falsy(existingLockFileContent)) {
logger.debug('No helmfile.lock found');
return null;
}
try {
await writeLocalFile(packageFileName, newPackageFileContent);
const execOptions: ExecOptions = {
docker: {},
extraEnv: {},
toolConstraints: [
{
toolName: 'helm',
constraint: config.constraints?.helm,
},
{
toolName: 'helmfile',
constraint: config.constraints?.helmfile,
},
],
};
await exec(`helmfile deps -f ${quote(packageFileName)}`, execOptions);
const newHelmLockContent = await readLocalFile(lockFileName, 'utf8');
if (existingLockFileContent === newHelmLockContent) {
logger.debug('helmfile.lock is unchanged');
return null;
}
return [
{
file: {
type: 'addition',
path: lockFileName,
contents: newHelmLockContent,
},
},
];
} catch (err) {
// istanbul ignore if
if (err.message === TEMPORARY_ERROR) {
throw err;
}
logger.debug({ err }, 'Failed to update Helmfile lock file');
return [
{
artifactError: {
lockFile: lockFileName,
stderr: err.message,
},
},
];
}
}