Skip to content

Commit 256a5aa

Browse files
committed
refactor: Adapt poetry for modern logs
1 parent cdbfc7d commit 256a5aa

File tree

1 file changed

+62
-43
lines changed

1 file changed

+62
-43
lines changed

lib/poetry.js

Lines changed: 62 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,57 +12,76 @@ function pyprojectTomlToRequirements() {
1212
return;
1313
}
1414

15-
this.serverless.cli.log('Generating requirements.txt from pyproject.toml...');
15+
let generateRequirementsProgress;
16+
if (this.progress) {
17+
generateRequirementsProgress = this.progress.get(
18+
'python-generate-requirements-toml'
19+
);
20+
} else {
21+
this.serverless.cli.log(
22+
'Generating requirements.txt from pyproject.toml...'
23+
);
24+
}
1625

17-
const res = spawnSync(
18-
'poetry',
19-
[
20-
'export',
21-
'--without-hashes',
22-
'-f',
23-
'requirements.txt',
24-
'-o',
25-
'requirements.txt',
26-
'--with-credentials',
27-
],
28-
{
29-
cwd: this.servicePath,
26+
try {
27+
const res = spawnSync(
28+
'poetry',
29+
[
30+
'export',
31+
'--without-hashes',
32+
'-f',
33+
'requirements.txt',
34+
'-o',
35+
'requirements.txt',
36+
'--with-credentials',
37+
],
38+
{
39+
cwd: this.servicePath,
40+
}
41+
);
42+
if (res.error) {
43+
if (res.error.code === 'ENOENT') {
44+
throw new Error(
45+
`poetry not found! Install it according to the poetry docs.`
46+
);
47+
}
48+
throw new Error(res.error);
3049
}
31-
);
32-
if (res.error) {
33-
if (res.error.code === 'ENOENT') {
34-
throw new Error(
35-
`poetry not found! Install it according to the poetry docs.`
36-
);
50+
if (res.status !== 0) {
51+
throw new Error(res.stderr);
3752
}
38-
throw new Error(res.error);
39-
}
40-
if (res.status !== 0) {
41-
throw new Error(res.stderr);
42-
}
4353

44-
const editableFlag = new RegExp(/^-e /gm);
45-
const sourceRequirements = path.join(this.servicePath, 'requirements.txt');
46-
const requirementsContents = fse.readFileSync(sourceRequirements, {
47-
encoding: 'utf-8',
48-
});
54+
const editableFlag = new RegExp(/^-e /gm);
55+
const sourceRequirements = path.join(this.servicePath, 'requirements.txt');
56+
const requirementsContents = fse.readFileSync(sourceRequirements, {
57+
encoding: 'utf-8',
58+
});
4959

50-
if (requirementsContents.match(editableFlag)) {
51-
this.serverless.cli.log(
52-
'The generated file contains -e flags, removing them...'
53-
);
54-
fse.writeFileSync(
60+
if (requirementsContents.match(editableFlag)) {
61+
if (this.log) {
62+
this.log.info(
63+
'The generated file contains -e flags, removing them'
64+
);
65+
} else {
66+
this.serverless.cli.log(
67+
'The generated file contains -e flags, removing them...'
68+
);
69+
}
70+
fse.writeFileSync(
71+
sourceRequirements,
72+
requirementsContents.replace(editableFlag, '')
73+
);
74+
}
75+
76+
fse.ensureDirSync(path.join(this.servicePath, '.serverless'));
77+
fse.moveSync(
5578
sourceRequirements,
56-
requirementsContents.replace(editableFlag, '')
79+
path.join(this.servicePath, '.serverless', 'requirements.txt'),
80+
{ overwrite: true }
5781
);
82+
} finally {
83+
generateRequirementsProgress && generateRequirementsProgress.remove();
5884
}
59-
60-
fse.ensureDirSync(path.join(this.servicePath, '.serverless'));
61-
fse.moveSync(
62-
sourceRequirements,
63-
path.join(this.servicePath, '.serverless', 'requirements.txt'),
64-
{ overwrite: true }
65-
);
6685
}
6786

6887
/**

0 commit comments

Comments
 (0)