@@ -129,24 +129,6 @@ async function ensureDistDkrRepo(branch = 'develop') {
129129}
130130
131131
132- // Get the paths from compose.yml
133- function getComposeFilePaths ( composePath ) {
134- const content = fs . readFileSync ( composePath , 'utf8' ) ;
135- const parsed = yaml . load ( content ) ;
136- const paths = new Set ( ) ;
137-
138- if ( parsed && parsed . include ) {
139- // Collect all paths from the "include" section in compose.yml
140- for ( const item of parsed . include ) {
141- if ( item . path ) {
142- paths . add ( item . path ) ;
143- }
144- }
145- }
146-
147- return [ ...paths ] ; // Return paths as an array
148- }
149-
150132// Copy files defined in compose.yml from the dist-dkr repo
151133async function copyDistDkrAssetsFromCompose ( composeContent , branch = 'develop' ) {
152134
@@ -171,9 +153,9 @@ async function copyDistDkrAssetsFromCompose(composeContent, branch = 'develop')
171153 let fileContent = fs . readFileSync ( srcPath , 'utf8' ) ;
172154
173155 // Fix YAML block strings if necessary
174- fileContent = fixYmlBlockStrings ( fileContent ) ;
156+ fileContent = yaml . load ( fileContent ) ;
175157
176- outputFiles [ item . path ] = fileContent ;
158+ outputFiles [ filePath ] = fileContent ;
177159 console . log ( `✔️ Copied ${ item . path } from dist-dkr` ) ;
178160 } else {
179161 console . warn ( `⚠️ File ${ item . path } not found at path: ${ srcPath } ` ) ;
@@ -202,78 +184,87 @@ async function copyDistDkrAssetsFromCompose(composeContent, branch = 'develop')
202184 return outputFiles ;
203185}
204186
187+ const getFormattedDate = ( ) => {
188+ const now = new Date ( ) ;
189+ const year = now . getFullYear ( ) ;
190+ const month = String ( now . getMonth ( ) + 1 ) . padStart ( 2 , '0' ) ; // Months are 0-based, so +1
191+ const day = String ( now . getDate ( ) ) . padStart ( 2 , '0' ) ;
192+ return `${ year } ${ month } ${ day } ` ;
193+ } ;
205194
206- async function sendToExternalSolutionRepo ( folderName , zipPath ) {
195+ async function sendToExternalSolutionRepo ( folderName , solutionFolder ) {
207196 const repoUrl = 'https://github.com/openimis/solutions.git' ;
208- const branchName = `solution/${ folderName } ` ;
209- const tempDir = path . join ( __dirname , 'temp_solutions_repo' ) ;
210- const unzipDir = path . join ( __dirname , 'unzipped_output' ) ;
211- const targetFolder = path . join ( tempDir , folderName ) ;
212-
197+ const branchName = `solution/${ folderName } /${ getFormattedDate ( ) } ` ;
198+ const tempDir = path . join ( __dirname , '.cache/temp_solutions_repo' ) ;
199+
213200 // Clean up any previous temp/unzip folder
214- if ( fs . existsSync ( tempDir ) ) fs . rmSync ( tempDir , { recursive : true } ) ;
215- if ( fs . existsSync ( unzipDir ) ) fs . rmSync ( unzipDir , { recursive : true } ) ;
216-
217- fs . mkdirSync ( unzipDir ) ;
218-
219- // Unzip output.zip
220- await fs . createReadStream ( zipPath )
221- . pipe ( unzipper . Extract ( { path : unzipDir } ) )
222- . promise ( ) ;
223-
201+ if ( ! fs . existsSync ( tempDir ) ) {
202+ await git . clone ( repoUrl , tempDir ) ;
203+ }
224204 const git = simpleGit ( ) ;
225-
226- // Clone the repo and checkout a new branch
227- await git . clone ( repoUrl , tempDir ) ;
228205 const gitRepo = simpleGit ( tempDir ) ;
229- await gitRepo . checkout ( 'develop' ) ;
230- await gitRepo . checkoutBranch ( branchName , 'develop' ) ;
206+ await gitRepo . checkout ( 'develop' ) ; // Clone the repo and checkout a new branch
207+
208+ const localBranches = await gitRepo . branchLocal ( ) ;
209+ if ( localBranches . all . includes ( branchName ) ) {
210+ await gitRepo . checkout ( branchName ) ;
211+ } else {
212+ await gitRepo . checkoutBranch ( branchName , 'develop' ) ;
231213
214+ }
232215 // Create or overwrite target folder in the cloned repo
216+ const targetFolder = path . join ( tempDir , folderName )
233217 if ( ! fs . existsSync ( targetFolder ) ) fs . mkdirSync ( targetFolder ) ;
234- fs . cpSync ( unzipDir , targetFolder , { recursive : true } ) ;
235-
218+ fs . cpSync ( solutionFolder , targetFolder , { recursive : true } ) ;
236219 // Commit and push
220+
237221 await gitRepo . add ( '.' ) ;
238222 await gitRepo . commit ( `Add/update ${ folderName } solution` ) ;
239- await gitRepo . push ( 'origin' , branchName ) ;
223+ // await gitRepo.push('origin', branchName);
240224 console . log ( `✔️ Pushed ${ folderName } to branch ${ branchName } ` ) ;
241225}
242226
243227
244- const { createZip, processSolutions } = require ( "./solutionBuilder.js" ) ;
228+
229+
230+ const { createZip, processSolutions , createSolutionDirectory} = require ( "./solutionBuilder.js" ) ;
245231
246232async function main ( ) {
247233 try {
248234 const args = process . argv . slice ( 2 ) ;
249235 const shouldPublish = args . includes ( '--publish' ) ;
250236 const folderArg = args . find ( arg => arg . startsWith ( '--folder=' ) ) ;
251237 const solutionName = folderArg ? folderArg . split ( '=' ) [ 1 ] : 'default-solution' ;
252-
253- const solution_path = './solution/solutions/HF.json' ;
238+ const solutions = {
239+ 'coreMIS' : './solution/solutions/coreMIS.json' ,
240+ 'SHI' : './solution/solutions/HF.json' ,
241+ 'claimai' : './solution/solutions/HF.json' ,
242+ 'full' : './solution/solutions/full.json' ,
243+ }
254244 const permission = fs . readFileSync ( './solution/permissions_map.json' , 'utf8' ) ;
255245 const permissionMap = JSON . parse ( permission ) ;
256-
257-
258- const output = await processSolutions (
259- solution_path ,
260- process . cwd ( ) ,
261- permissionMap ,
262- ) ;
263-
264- // Get dist-dkr files from compose.yml and merge them into output
265- const composeFiles = await copyDistDkrAssetsFromCompose ( output [ 'compose.yml' ] , 'develop' ) ;
266- Object . assign ( output , composeFiles ) ;
267-
268- // Create zip
269- const zipPath = path . join ( __dirname , 'output.zip' ) ;
270- await createZip ( output , zipPath ) ;
271-
272- // Publish if requested
273- if ( shouldPublish ) {
274- await sendToExternalSolutionRepo ( solutionName , zipPath ) ;
275- } else {
276- console . log ( '🛈 Skipping publish step (use --publish to enable)' ) ;
246+ let output = [ ]
247+ for ( const [ name , solution_path ] of Object . entries ( solutions ) ) {
248+ console . log ( `generating ${ name } ` )
249+ output [ name ] = await processSolutions (
250+ solution_path ,
251+ process . cwd ( ) ,
252+ permissionMap ,
253+ ) ;
254+ // Get dist-dkr files from compose.yml and merge them into output
255+ const composeFiles = await copyDistDkrAssetsFromCompose ( output [ name ] [ 'compose.yml' ] , 'develop' ) ;
256+ Object . assign ( output [ name ] , composeFiles ) ;
257+ const zipPath = path . join ( __dirname , 'build' , name + '.zip' ) ;
258+ await createZip ( output [ name ] , zipPath ) ;
259+ baseDir = 'build/' + name
260+ await createSolutionDirectory ( baseDir , output [ name ] )
261+
262+ // Publish if requested
263+ if ( shouldPublish ) {
264+ await sendToExternalSolutionRepo ( name , baseDir ) ;
265+ } else {
266+ console . log ( '🛈 Skipping publish step (use --publish to enable)' ) ;
267+ }
277268 }
278269 } catch ( error ) {
279270 console . error ( 'Error:' , error ) ;
0 commit comments