/
artifacts.ts
86 lines (79 loc) · 2.24 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
import is from '@sindresorhus/is';
import { quote } from 'shlex';
import { logger } from '../../../logger';
import { exec } from '../../../util/exec';
import type { ExecOptions } from '../../../util/exec/types';
import { readLocalFile } from '../../../util/fs';
import { getRepoStatus } from '../../../util/git';
import * as hostRules from '../../../util/host-rules';
import { regEx } from '../../../util/regex';
import type { UpdateArtifact, UpdateArtifactsResult } from '../types';
export async function updateArtifacts({
packageFileName,
config,
updatedDeps,
}: UpdateArtifact): Promise<UpdateArtifactsResult[] | null> {
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 \
--extra-experimental-features flakes `;
const { token } = hostRules.find({
hostType: 'github',
url: 'https://api.github.com/',
});
if (token) {
cmd += `--extra-access-tokens github.com=${token} `;
}
if (config.isLockFileMaintenance) {
cmd += 'flake update';
} else {
const inputs = updatedDeps
.map(({ depName }) => depName)
.filter(is.nonEmptyStringAndNotWhitespace)
.map((depName) => `--update-input ${quote(depName)}`)
.join(' ');
cmd += `flake lock ${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: {
lockFile: lockFileName,
stderr: err.message,
},
},
];
}
}