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'; export class SzciCloudronManager { public szciRef: Szci; constructor(szciArg: Szci) { this.szciRef = szciArg; } /** * 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.szciRef.npmciConfig.getConfig().projectInfo.npm.version); logger.log('ok', 'CloudronManifest prepared'); // lets figure out the docker image tag const dockerImageTag = await this.szciRef.npmciConfig.kvStorage.readKey('latestPushedDockerTag'); const appName = this.szciRef.npmciConfig.getConfig().cloudronAppName; const cloudronEnvVar = Deno.env.get("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'); } }