diff --git a/.ctrc.yml b/.ctrc.yml index e741c55..5e83db7 100644 --- a/.ctrc.yml +++ b/.ctrc.yml @@ -14,9 +14,9 @@ hooks: echo "$(conventional-tools commitgen)$(cat ${1})" > ${1}; fi pre-commit: - - $(git rev-parse --show-toplevel)/.github/hooks/check-copyright.sh - - $(git rev-parse --show-toplevel)/.github/hooks/no-yaml-files.sh - - $(git rev-parse --show-toplevel)/.github/hooks/prettier.sh + - $(conventional-tools root)/.github/hooks/check-copyright.sh + - $(conventional-tools root)/.github/hooks/no-yaml-files.sh + - $(conventional-tools root)/.github/hooks/prettier.sh commit: scopes: - core diff --git a/src/commands/git-hook.ts b/src/commands/git-hook.ts index 299ecf2..d6a0441 100644 --- a/src/commands/git-hook.ts +++ b/src/commands/git-hook.ts @@ -24,12 +24,9 @@ export async function handler(args: Arguments): Promise { runner.start(); - writer.print(runner); - const ticker = setInterval(() => writer.update(runner), 80); - await runner.wait(); - - clearInterval(ticker); - writer.update(runner); + process.stdout.isTTY + ? await interactive(runner, writer) + : await nonInteractive(runner, writer); let exitCode = 0; for (const command of runner) { @@ -49,4 +46,19 @@ export async function handler(args: Arguments): Promise { return exitCode; } +async function interactive(runner: Runner, writer: Writer) { + writer.print(runner); + const ticker = setInterval(() => writer.update(runner), 80); + + await runner.wait(); + + clearInterval(ticker); + writer.update(runner); +} + +async function nonInteractive(runner: Runner, writer: Writer) { + writer.print(runner); + await runner.wait(); +} + export default {builder, handler: handlerWrapper(handler)}; diff --git a/src/commands/root.ts b/src/commands/root.ts new file mode 100644 index 0000000..d88cdd2 --- /dev/null +++ b/src/commands/root.ts @@ -0,0 +1,18 @@ +import {handlerWrapper} from '../lib/handler-wrapper'; +import {log} from '../lib/logger'; +import {getSourceControlProvider} from '../lib/source-control'; + +export const builder = {} as const; + +export async function handler(): Promise { + const sourceControl = await getSourceControlProvider(); + if (!sourceControl) { + throw new Error('No source control provider found'); + } + + log(await sourceControl.root()); + + return 0; +} + +export default {builder, handler: handlerWrapper(handler)}; diff --git a/src/index.ts b/src/index.ts index ea14aac..d926eec 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,6 +4,7 @@ import {hideBin} from 'yargs/helpers'; import commitgen from './commands/commitgen'; import gitHook from './commands/git-hook'; import gitHookInstall from './commands/git-hook:install'; +import root from './commands/root'; export async function run(args: string[]) { await yargs(hideBin(args)) @@ -13,6 +14,12 @@ export async function run(args: string[]) { commitgen.builder, commitgen.handler, ) + .command( + 'root', + 'Gets the root directory of the current repository', + root.builder, + root.handler, + ) .command( 'git-hook', 'Run the hooks for a given action', diff --git a/src/lib/source-control/git.ts b/src/lib/source-control/git.ts index d7526bf..8715774 100644 --- a/src/lib/source-control/git.ts +++ b/src/lib/source-control/git.ts @@ -12,6 +12,11 @@ const git: SourceControlProvider = { return stdout.trim(); }, + + root: async () => { + const {stdout} = await run(`git rev-parse --show-toplevel`); + return stdout.trim(); + }, }; export default git;