1
1
const fs = require ( 'fs' ) ;
2
2
const fse = require ( 'fs-extra' ) ;
3
3
const path = require ( 'path' ) ;
4
+
4
5
const spawn = require ( 'child-process-ext/spawn' ) ;
5
6
const tomlParse = require ( '@iarna/toml/parse-string' ) ;
6
7
@@ -12,58 +13,75 @@ async function pyprojectTomlToRequirements() {
12
13
return ;
13
14
}
14
15
15
- this . serverless . cli . log ( 'Generating requirements.txt from pyproject.toml...' ) ;
16
+ let generateRequirementsProgress ;
17
+ if ( this . progress ) {
18
+ generateRequirementsProgress = this . progress . get (
19
+ 'python-generate-requirements-toml'
20
+ ) ;
21
+ } else {
22
+ this . serverless . cli . log (
23
+ 'Generating requirements.txt from pyproject.toml...'
24
+ ) ;
25
+ }
16
26
17
27
try {
18
- await spawn (
19
- 'poetry' ,
20
- [
21
- 'export' ,
22
- '--without-hashes' ,
23
- '-f' ,
24
- 'requirements.txt' ,
25
- '-o' ,
26
- 'requirements.txt' ,
27
- '--with-credentials' ,
28
- ] ,
29
- {
30
- cwd : this . servicePath ,
31
- }
32
- ) ;
33
- } catch ( e ) {
34
- if (
35
- e . stderrBuffer &&
36
- e . stderrBuffer . toString ( ) . includes ( 'command not found' )
37
- ) {
38
- throw new Error (
39
- `poetry not found! Install it according to the poetry docs.`
28
+ try {
29
+ await spawn (
30
+ 'poetry' ,
31
+ [
32
+ 'export' ,
33
+ '--without-hashes' ,
34
+ '-f' ,
35
+ 'requirements.txt' ,
36
+ '-o' ,
37
+ 'requirements.txt' ,
38
+ '--with-credentials' ,
39
+ ] ,
40
+ {
41
+ cwd : this . servicePath ,
42
+ }
40
43
) ;
44
+ } catch ( e ) {
45
+ if (
46
+ e . stderrBuffer &&
47
+ e . stderrBuffer . toString ( ) . includes ( 'command not found' )
48
+ ) {
49
+ throw new Error (
50
+ `poetry not found! Install it according to the poetry docs.`
51
+ ) ;
52
+ }
53
+ throw e ;
41
54
}
42
- throw e ;
43
- }
44
55
45
- const editableFlag = new RegExp ( / ^ - e / gm) ;
46
- const sourceRequirements = path . join ( this . servicePath , 'requirements.txt' ) ;
47
- const requirementsContents = fse . readFileSync ( sourceRequirements , {
48
- encoding : 'utf-8' ,
49
- } ) ;
56
+ const editableFlag = new RegExp ( / ^ - e / gm) ;
57
+ const sourceRequirements = path . join ( this . servicePath , 'requirements.txt' ) ;
58
+ const requirementsContents = fse . readFileSync ( sourceRequirements , {
59
+ encoding : 'utf-8' ,
60
+ } ) ;
50
61
51
- if ( requirementsContents . match ( editableFlag ) ) {
52
- this . serverless . cli . log (
53
- 'The generated file contains -e flags, removing them...'
54
- ) ;
55
- fse . writeFileSync (
62
+ if ( requirementsContents . match ( editableFlag ) ) {
63
+ if ( this . log ) {
64
+ this . log . info ( 'The generated file contains -e flags, removing them' ) ;
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 (
56
78
sourceRequirements ,
57
- requirementsContents . replace ( editableFlag , '' )
79
+ path . join ( this . servicePath , '.serverless' , 'requirements.txt' ) ,
80
+ { overwrite : true }
58
81
) ;
82
+ } finally {
83
+ generateRequirementsProgress && generateRequirementsProgress . remove ( ) ;
59
84
}
60
-
61
- fse . ensureDirSync ( path . join ( this . servicePath , '.serverless' ) ) ;
62
- fse . moveSync (
63
- sourceRequirements ,
64
- path . join ( this . servicePath , '.serverless' , 'requirements.txt' ) ,
65
- { overwrite : true }
66
- ) ;
67
85
}
68
86
69
87
/**
0 commit comments