@@ -13,7 +13,7 @@ import tar from 'tar';
13
13
import { X_EMAIL_HEADER } from '../utils/constants' ;
14
14
import { GenerateCodeRequest , getGenerateCodeError , getGenerateCodeResponse , Project } from '../models/code' ;
15
15
import { GitPlatformEntity } from '../models/gitPlatform' ;
16
- import { OldVersion , ProjectEntity } from '../models/project' ;
16
+ import { Metadata , OldVersion , ProjectEntity } from '../models/project' ;
17
17
import { ExistingProjectGitServerRequest } from '../integrations/simple-git/models' ;
18
18
import { ProjectService } from '../services/projectService' ;
19
19
import { GitPlatformService } from '../services/gitPlatformService' ;
@@ -72,8 +72,7 @@ codeOperationsRouter.post('/generate', requireEmailMiddleware, async (request, r
72
72
const lastVersion = previousVersion ( currentVersion ) ;
73
73
const oldVersions = projectEntity . old_versions ;
74
74
// iterate over all oldVersions to find the last version in it.
75
- for ( const oldVersion of oldVersions as string [ ] ) {
76
- const oldVersionJson = JSON . parse ( oldVersion ) ;
75
+ for ( const oldVersionJson of JSON . parse ( oldVersions ) as OldVersion [ ] ) {
77
76
// check if the lastVersion matches with current version
78
77
if ( oldVersionJson . version === lastVersion ) {
79
78
// remove unwanted keys and then do the equality check. This is needed to avoid keys used to render ui.
@@ -87,23 +86,39 @@ codeOperationsRouter.post('/generate', requireEmailMiddleware, async (request, r
87
86
88
87
// create directory hierarchy here itself as creating it after receiving data will not be proper.
89
88
const originalProjectPath = `${ os . tmpdir ( ) } /${ projectEntity . display_name } ` ;
89
+ // this is path where project will be downloaded
90
90
const downloadedProjectPath = `${ originalProjectPath } _downloaded` ;
91
+ // this is path where project will be cloned
92
+ const clonedProjectPath = `${ originalProjectPath } _cloned` ;
93
+ // this is path in tar file
94
+ const generatedProjectPath = `${ downloadedProjectPath } ` + `${ originalProjectPath } ` ;
95
+ // this is path where tar file will be saved
96
+ const downloadedProjectTarFilePath = `${ originalProjectPath } _downloaded.tar.gz` ;
91
97
try {
98
+ // create downloadedProjectPath
92
99
if ( fs . existsSync ( downloadedProjectPath ) ) {
93
100
cleanup ( downloadedProjectPath ) ;
94
101
}
95
102
fs . mkdirSync ( downloadedProjectPath , { recursive : true } ) ;
103
+ // create clonedProjectPath
104
+ if ( fs . existsSync ( clonedProjectPath ) ) {
105
+ cleanup ( clonedProjectPath ) ;
106
+ }
107
+ fs . mkdirSync ( clonedProjectPath , { recursive : true } ) ;
96
108
} catch ( err : any ) {
97
109
if ( err . code !== 'EEXIST' ) {
98
110
const message = `unable to generate code for ${ projectEntity . display_name } [${ projectEntity . id } ] => ${ err } ` ;
99
111
return resource . status ( 500 ) . json ( getGenerateCodeError ( message ) ) ;
100
112
} else {
101
113
// first clean up and then recreate (it might be a residue of previous run)
114
+ // create downloadedProjectPath
102
115
cleanup ( downloadedProjectPath ) ;
103
116
fs . mkdirSync ( downloadedProjectPath , { recursive : true } ) ;
117
+ // create clonedProjectPath
118
+ cleanup ( clonedProjectPath ) ;
119
+ fs . mkdirSync ( clonedProjectPath , { recursive : true } ) ;
104
120
}
105
121
}
106
- const projectTarFilePath = `${ downloadedProjectPath } _downloaded.tar.gz` ;
107
122
108
123
const gitPlatformEntity : GitPlatformEntity = await gitPlatformService . getGitPlatform ( projectEntity . owner_email as string , projectEntity . git_platform_name as string ) ;
109
124
@@ -127,8 +142,8 @@ codeOperationsRouter.post('/generate', requireEmailMiddleware, async (request, r
127
142
call . on ( 'data' , async ( response : { fileChunk : any } ) => {
128
143
// chunk is available, append it to the given path.
129
144
if ( response . fileChunk ) {
130
- fs . appendFileSync ( projectTarFilePath , response . fileChunk ) ;
131
- Logger . debug ( `writing tar file chunk to: ${ projectTarFilePath } ` ) ;
145
+ fs . appendFileSync ( downloadedProjectTarFilePath , response . fileChunk ) ;
146
+ Logger . debug ( `writing tar file chunk to: ${ downloadedProjectTarFilePath } ` ) ;
132
147
}
133
148
} ) ;
134
149
@@ -147,7 +162,7 @@ codeOperationsRouter.post('/generate', requireEmailMiddleware, async (request, r
147
162
C : downloadedProjectPath
148
163
} ) ;
149
164
// stream on extraction on tar file
150
- const fscrs = fs . createReadStream ( projectTarFilePath ) ;
165
+ const fscrs = fs . createReadStream ( downloadedProjectTarFilePath ) ;
151
166
fscrs . on ( 'error' , ( err : any ) => {
152
167
Logger . error ( JSON . stringify ( err ) ) ;
153
168
} ) ;
@@ -156,7 +171,8 @@ codeOperationsRouter.post('/generate', requireEmailMiddleware, async (request, r
156
171
extract . on ( 'finish' , async ( ) => {
157
172
// clone existing repository
158
173
const existingProjectGitServerRequest : ExistingProjectGitServerRequest = {
159
- clonedProjectPath : `${ downloadedProjectPath } ` ,
174
+ projectName : projectEntity . display_name ,
175
+ projectVersion : projectEntity . version ,
160
176
gitProviderDetails : {
161
177
repositoryBranch : projectEntity . repository_branch as string ,
162
178
repositoryName : projectEntity . repository_name as string ,
@@ -167,9 +183,8 @@ codeOperationsRouter.post('/generate', requireEmailMiddleware, async (request, r
167
183
platformUrl : gitPlatformEntity . url ,
168
184
platformName : gitPlatformEntity . name ,
169
185
} ,
170
- projectVersion : projectEntity . version ,
171
- generatedProjectPath : `${ downloadedProjectPath } ` + `${ originalProjectPath } ` ,
172
- existingProject : `${ downloadedProjectPath } /${ projectEntity . repository_name } ` ,
186
+ clonedProjectPath : `${ clonedProjectPath } ` ,
187
+ generatedProjectPath : `${ generatedProjectPath } ` ,
173
188
} ;
174
189
175
190
let error : string = await cloneExistingProjectFromGitServer ( existingProjectGitServerRequest ) ;
@@ -189,26 +204,26 @@ codeOperationsRouter.post('/generate', requireEmailMiddleware, async (request, r
189
204
Logger . error ( pushErrorMessage ) ;
190
205
return resource . status ( 500 ) . json ( getGenerateCodeError ( pushErrorMessage ) ) ;
191
206
}
192
-
193
207
Logger . debug ( `saved ${ downloadedProjectPath } to github.` ) ;
194
208
cleanup ( downloadedProjectPath ) ;
209
+ cleanup ( clonedProjectPath ) ;
210
+ cleanup ( downloadedProjectTarFilePath ) ;
195
211
196
212
// update status of projectEntity
197
- const metadata = JSON . parse ( projectEntity . metadata as string ) || new Map < string , string > ( ) ;
213
+ const metadata = JSON . parse ( projectEntity . metadata as string ) || { } as Metadata ;
198
214
metadata . isGenerated = true ;
199
215
metadata . version = projectEntity . version ;
200
216
// add metadata back to projectEntity.spec
201
217
projectEntity . metadata = JSON . stringify ( metadata ) ;
202
218
// change version now
203
- if ( projectEntity . old_versions ) {
204
- const oldVersion : OldVersion = {
205
- version : projectEntity . version ,
206
- json : projectEntity . json
207
- } ;
208
- projectEntity . version = nextVersion ( projectEntity . version ) ;
209
- projectEntity . old_versions . push ( JSON . stringify ( oldVersion ) ) ;
210
- }
211
- console . log ( 'projectEntity' , projectEntity ) ;
219
+ const oldVersion : OldVersion = {
220
+ version : projectEntity . version ,
221
+ json : projectEntity . json
222
+ } ;
223
+ const oldVersions = JSON . parse ( projectEntity . old_versions ) as OldVersion [ ] ;
224
+ projectEntity . version = nextVersion ( projectEntity . version ) ;
225
+ oldVersions . push ( oldVersion ) ;
226
+ projectEntity . old_versions = JSON . stringify ( oldVersions ) ;
212
227
213
228
const isUpdated = await projectService . updateProject ( projectId , projectEntity ) ;
214
229
if ( isUpdated ) {
0 commit comments