| 
									
										
										
										
											2025-10-26 15:23:56 +00:00
										 |  |  | import { logger } from '../szci.logging.ts'; | 
					
						
							|  |  |  | import * as plugins from './mod.plugins.ts'; | 
					
						
							|  |  |  | import * as paths from '../szci.paths.ts'; | 
					
						
							|  |  |  | import { bash } from '../szci.bash.ts'; | 
					
						
							|  |  |  | import { Szci } from '../szci.classes.szci.ts'; | 
					
						
							| 
									
										
										
										
											2023-07-01 22:05:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-26 15:23:56 +00:00
										 |  |  | export class SzciCloudronManager { | 
					
						
							|  |  |  |   public szciRef: Szci; | 
					
						
							| 
									
										
										
										
											2023-07-01 22:05:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-26 15:23:56 +00:00
										 |  |  |   constructor(szciArg: Szci) { | 
					
						
							|  |  |  |     this.szciRef = szciArg; | 
					
						
							| 
									
										
										
										
											2023-07-01 22:05:43 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * handle cli input | 
					
						
							|  |  |  |    * @param argvArg | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   public handleCli = async (argvArg: any) => { | 
					
						
							|  |  |  |     if (argvArg._.length >= 2) { | 
					
						
							|  |  |  |       const action: string = argvArg._[1]; | 
					
						
							|  |  |  |       switch (action) { | 
					
						
							|  |  |  |         case 'deploy': | 
					
						
							|  |  |  |           await this.deploy(); | 
					
						
							|  |  |  |           break; | 
					
						
							|  |  |  |         default: | 
					
						
							|  |  |  |           logger.log('error', `>>npmci cloudron ...<< action >>${action}<< not supported`); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       logger.log( | 
					
						
							|  |  |  |         'info', | 
					
						
							|  |  |  |         `>>npmci cloudron ...<< cli arguments invalid... Please read the documentation.` | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Replaces the version string in CloudronManifest file | 
					
						
							|  |  |  |    * @param versionArg | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   public deploy = async () => { | 
					
						
							|  |  |  |     logger.log('info', 'now deploying to cloudron...'); | 
					
						
							|  |  |  |     logger.log('info', 'installing cloudron cli...'); | 
					
						
							|  |  |  |     await bash(`pnpm install -g cloudron`); | 
					
						
							|  |  |  |     logger.log('ok', 'cloudron cli installed'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // lets set the version in the CloudronManifest file
 | 
					
						
							| 
									
										
										
										
											2025-10-26 15:23:56 +00:00
										 |  |  |     await this.prepareCloudronManifest(this.szciRef.npmciConfig.getConfig().projectInfo.npm.version); | 
					
						
							| 
									
										
										
										
											2023-07-01 22:05:43 +02:00
										 |  |  |     logger.log('ok', 'CloudronManifest prepared'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // lets figure out the docker image tag
 | 
					
						
							| 
									
										
										
										
											2025-10-26 15:23:56 +00:00
										 |  |  |     const dockerImageTag = await this.szciRef.npmciConfig.kvStorage.readKey('latestPushedDockerTag'); | 
					
						
							|  |  |  |     const appName = this.szciRef.npmciConfig.getConfig().cloudronAppName; | 
					
						
							| 
									
										
										
										
											2023-07-01 22:05:43 +02:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											2025-10-26 15:23:56 +00:00
										 |  |  |     const cloudronEnvVar = Deno.env.get("NPMCI_LOGIN_CLOUDRON"); | 
					
						
							| 
									
										
										
										
											2023-07-01 22:05:43 +02:00
										 |  |  |     const cloudronServer = cloudronEnvVar.split('|')[0]; | 
					
						
							|  |  |  |     const cloudronToken = cloudronEnvVar.split('|')[1]; | 
					
						
							|  |  |  |     await bash(`cloudron update --server ${cloudronServer} --token ${cloudronToken} --image ${dockerImageTag} --app ${appName}`); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private prepareCloudronManifest = async (versionArg: string) => { | 
					
						
							|  |  |  |     const manifestPath = plugins.path.join(paths.cwd, 'CloudronManifest.json'); | 
					
						
							|  |  |  |     let manifestFile = plugins.smartfile.fs.toStringSync(manifestPath); | 
					
						
							|  |  |  |     manifestFile = manifestFile.replace(/##version##/g, versionArg); | 
					
						
							|  |  |  |     plugins.smartfile.memory.toFsSync(manifestFile, manifestPath); | 
					
						
							|  |  |  |     logger.log('info', 'Version replaced in CloudronManifest file'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |