11const fs = require ( 'fs' ) ;
22const fse = require ( 'fs-extra' ) ;
33const path = require ( 'path' ) ;
4+
45const spawn = require ( 'child-process-ext/spawn' ) ;
56const tomlParse = require ( '@iarna/toml/parse-string' ) ;
67
@@ -12,58 +13,75 @@ async function pyprojectTomlToRequirements() {
1213 return ;
1314 }
1415
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+ }
1626
1727 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+ }
4043 ) ;
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 ;
4154 }
42- throw e ;
43- }
4455
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+ } ) ;
5061
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 (
5678 sourceRequirements ,
57- requirementsContents . replace ( editableFlag , '' )
79+ path . join ( this . servicePath , '.serverless' , 'requirements.txt' ) ,
80+ { overwrite : true }
5881 ) ;
82+ } finally {
83+ generateRequirementsProgress && generateRequirementsProgress . remove ( ) ;
5984 }
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- ) ;
6785}
6886
6987/**
0 commit comments