-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
artifacts.ts
105 lines (98 loc) · 2.91 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import is from '@sindresorhus/is';
import { quote } from 'shlex';
import { parse } from 'upath';
import { TEMPORARY_ERROR } from '../../constants/error-messages';
import { logger } from '../../logger';
import { ExecOptions, exec } from '../../util/exec';
import { deleteLocalFile, readLocalFile, writeLocalFile } from '../../util/fs';
import { getRepoStatus } from '../../util/git';
import { regEx } from '../../util/regex';
import type {
UpdateArtifact,
UpdateArtifactsConfig,
UpdateArtifactsResult,
} from '../types';
function getPythonConstraint(
config: UpdateArtifactsConfig
): string | undefined | null {
const { constraints = {} } = config;
const { python } = constraints;
if (python) {
logger.debug('Using python constraint from config');
return python;
}
return undefined;
}
function getPipToolsConstraint(config: UpdateArtifactsConfig): string {
const { constraints = {} } = config;
const { pipTools } = constraints;
if (is.string(pipTools)) {
logger.debug('Using pipTools constraint from config');
return pipTools;
}
return '';
}
export async function updateArtifacts({
packageFileName: inputFileName,
newPackageFileContent: newInputContent,
config,
}: UpdateArtifact): Promise<UpdateArtifactsResult[] | null> {
const outputFileName = inputFileName.replace(regEx(/(\.in)?$/), '.txt');
logger.debug(
`pipCompile.updateArtifacts(${inputFileName}->${outputFileName})`
);
const existingOutput = await readLocalFile(outputFileName, 'utf8');
if (!existingOutput) {
logger.debug('No pip-compile output file found');
return null;
}
try {
await writeLocalFile(inputFileName, newInputContent);
if (config.isLockFileMaintenance) {
await deleteLocalFile(outputFileName);
}
const cmd = `pip-compile ${quote(parse(inputFileName).base)}`;
const tagConstraint = getPythonConstraint(config);
const pipToolsConstraint = getPipToolsConstraint(config);
const execOptions: ExecOptions = {
cwdFile: inputFileName,
docker: {
image: 'python',
tagConstraint,
tagScheme: 'pep440',
preCommands: [
`pip install --user ${quote(`pip-tools${pipToolsConstraint}`)}`,
],
},
};
logger.debug({ cmd }, 'pip-compile command');
await exec(cmd, execOptions);
const status = await getRepoStatus();
if (!status?.modified.includes(outputFileName)) {
return null;
}
logger.debug('Returning updated pip-compile result');
return [
{
file: {
name: outputFileName,
contents: await readLocalFile(outputFileName, 'utf8'),
},
},
];
} catch (err) {
// istanbul ignore if
if (err.message === TEMPORARY_ERROR) {
throw err;
}
logger.debug({ err }, 'Failed to pip-compile');
return [
{
artifactError: {
lockFile: outputFileName,
stderr: err.message,
},
},
];
}
}