import { program } from 'commander';
import * as shell from 'shelljs';
import { GitUtils } from '../utils/git.utils';

export class GitCommitMsgHook {
  constructor() {}

  setup() {
    this.setupInstallHookCommand();
    this.setupRemoveHookCommand();
  }

  private setupInstallHookCommand() {
    program
      .command('commit-msg-hook')
      .description('Setup git commit-msg hook for conventional commits')
      .action(async () => {
        this.runInstallHookCommand();
      });
  }

  private setupRemoveHookCommand() {
    program
      .command('remove-commit-msg-hook')
      .description('Remove git commit-msg hook')
      .action(async () => {
        this.runRemoveHookCommand();
      });
  }

  private runInstallHookCommand() {
    if (GitUtils.logErrorIfNotInGitProject()) return;

    shell.exec(
      'curl -o- https://raw.githubusercontent.com/tapsellorg/conventional-commits-git-hook/master/scripts/install.sh | sh',
    );
  }
  private runRemoveHookCommand() {
    if (GitUtils.logErrorIfNotInGitProject()) return;
    shell.exec('rm .git/hooks/commit-msg');
  }
}
