Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(git-hooks): non interactive hooks #91

Merged
merged 2 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .ctrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 18 additions & 6 deletions src/commands/git-hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@ export async function handler(args: Arguments<Options>): Promise<number> {

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) {
Expand All @@ -49,4 +46,19 @@ export async function handler(args: Arguments<Options>): Promise<number> {
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)};
18 changes: 18 additions & 0 deletions src/commands/root.ts
Original file line number Diff line number Diff line change
@@ -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<number> {
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)};
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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',
Expand Down
5 changes: 5 additions & 0 deletions src/lib/source-control/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;