| 
									
										
										
										
											2023-07-01 22:05:43 +02:00
										 |  |  | import { logger } from '../npmci.logging.js'; | 
					
						
							|  |  |  | import * as plugins from './mod.plugins.js'; | 
					
						
							|  |  |  | import * as paths from '../npmci.paths.js'; | 
					
						
							|  |  |  | import { bash } from '../npmci.bash.js'; | 
					
						
							|  |  |  | import { Npmci } from '../npmci.classes.npmci.js'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class NpmciCloudronManager { | 
					
						
							|  |  |  |   public npmciRef: Npmci; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   constructor(npmciArg: Npmci) { | 
					
						
							|  |  |  |     this.npmciRef = npmciArg; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * 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
 | 
					
						
							|  |  |  |     await this.prepareCloudronManifest(this.npmciRef.npmciConfig.getConfig().projectInfo.npm.version); | 
					
						
							|  |  |  |     logger.log('ok', 'CloudronManifest prepared'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // lets figure out the docker image tag
 | 
					
						
							| 
									
										
										
										
											2023-07-02 00:10:27 +02:00
										 |  |  |     const dockerImageTag = await this.npmciRef.npmciConfig.kvStorage.readKey('latestPushedDockerTag'); | 
					
						
							| 
									
										
										
										
											2023-07-01 22:05:43 +02:00
										 |  |  |     const appName = this.npmciRef.npmciConfig.getConfig().cloudronAppName; | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     const cloudronEnvVar = process.env.NPMCI_LOGIN_CLOUDRON; | 
					
						
							|  |  |  |     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'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |