Skip to content
Snippets Groups Projects
Select Git revision
6 results Searching

gitMission.ts

Blame
  • gitMission.ts 2.79 KiB
    import simpleGit, { SimpleGit, SimpleGitOptions } from 'simple-git';
    import { error } from '../recorder/utils';
    import { PROJECT_SRC_PATH } from './config';
    import UserConfig from './userConfig';
    
    const util = require('util');
    const exec = util.promisify(require('child_process').exec);
    
    const DEFAULT_REMOTE = 'origin';
    const DEFAULT_BRANCH = 'master';
    
    export default class GitMission {
      private git: SimpleGit;
    
      constructor(private userConfig: UserConfig) {
        const options: Partial<SimpleGitOptions> = {
          baseDir: PROJECT_SRC_PATH,
          binary: 'git',
          maxConcurrentProcesses: 2,
        };
    
        this.git = simpleGit(options);
      }
    
      async setupSshAgent() {
        const { err, stderr, stdout } = await exec(
          `eval "$(ssh-agent -s)" && ssh-keyscan -p ${this.userConfig.getGiteaSshPort()} -H ${this.userConfig.getGiteaHost()} >> ~/.ssh/known_hosts`,
        );
    
        console.log(stdout);
    
        if (err) {
          console.error(stderr);
          throw new Error(err);
        }
      }
    
      get author() {
        return this.userConfig.getUsername();
      }
    
      async init() {
        try {
          console.log('Setup ssh agent');
          await this.setupSshAgent();
    
          console.log('Init Git mission..');
    
          const remote = await this.readRemote();
          if (remote === DEFAULT_REMOTE) {
            return Promise.resolve(this);
          }
    
          const remotePath = this.getRemotePath();
    
          await this.git.init();
          await this.git.addRemote(DEFAULT_REMOTE, remotePath);
          await this.git.addConfig('user.email', this.userConfig.getCurrentUserDetails().email, false, 'system');
          await this.git.addConfig(
            'user.name',
            `${this.userConfig.getCurrentUserDetails().lastName} ${this.userConfig.getCurrentUserDetails().firstName}`,
            false,
            'system',
          );
    
          return Promise.resolve(this);
        } catch (e) {
          console.error(e);
          return Promise.reject(e);
        }