import { logger } from '../szci.logging.ts'; import * as plugins from './mod.plugins.ts'; import { bash, bashNoError } from '../szci.bash.ts'; import { Szci } from '../szci.classes.szci.ts'; export class SzciGitManager { public szciRef: Szci; constructor(szciRefArg: Szci) { this.szciRef = szciRefArg; } /** * handle cli input * @param argvArg */ public handleCli = async (argvArg: any) => { if (argvArg._.length >= 2) { const action: string = argvArg._[1]; switch (action) { case 'mirror': await this.mirror(); break; default: logger.log('error', `npmci git -> action >>${action}<< not supported!`); } } else { logger.log('info', `npmci git -> cli arguments invalid! Please read the documentation.`); } }; public mirror = async () => { const githubToken = Deno.env.get("NPMCI_GIT_GITHUBTOKEN"); const githubUser = Deno.env.get("NPMCI_GIT_GITHUBGROUP") || this.szciRef.npmciEnv.repo.user; const githubRepo = Deno.env.get("NPMCI_GIT_GITHUB") || this.szciRef.npmciEnv.repo.repo; if ( this.szciRef.npmciConfig.getConfig().projectInfo.npm.packageJson.private === true || this.szciRef.npmciConfig.getConfig().npmAccessLevel === 'private' ) { logger.log( 'warn', `refusing to mirror due to private property use a private mirror location instead` ); return; } if (githubToken) { logger.log('info', 'found github token.'); logger.log('info', 'attempting the mirror the repository to GitHub'); // remove old mirrors await bashNoError('git remote rm mirror'); await bash(`git fetch`); // add the mirror await bashNoError( `git remote add mirror https://${githubToken}@github.com/${githubUser}/${githubRepo}.git` ); await bashNoError(`git push mirror --all`); await bashNoError(`git checkout origin/master`); await bashNoError(`git push mirror master`); logger.log('ok', 'pushed all branches to mirror!'); await bashNoError(`git push mirror --tags`); logger.log('ok', 'pushed all tags to mirror!'); // remove old mirrors await bashNoError('git remote rm mirror'); } else { logger.log('error', `cannot find NPMCI_GIT_GITHUBTOKEN env var!`); Deno.exit(1); } }; }