Skip to content

Commit

Permalink
feat: add directory flag --directory
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-ippolito committed Dec 11, 2024
1 parent 1d65666 commit dcb036d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
31 changes: 31 additions & 0 deletions libs/report/committers/src/lib/committer-directory.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { reportCommittersCommand } from './committers';
import { runCommand } from '@herodevs/utility';

jest.mock('@herodevs/utility', () => ({
runCommand: jest.fn().mockResolvedValue(''),
}));

describe('reportCommittersCommand directory', () => {
const mockRunCommand = runCommand as jest.Mock;

beforeEach(() => {
jest.clearAllMocks();
});

it('should construct the git command with directory option', async () => {
const args = {
afterDate: '2023-01-01',
beforeDate: '2023-01-31',
exclude: [],
json: false,
directory: 'src',
$0: 'test',
_: [],
};

await reportCommittersCommand.handler(args);

const calledCommand = mockRunCommand.mock.calls[0][0];
expect(calledCommand).toMatch(/-- src/);
});
});
25 changes: 16 additions & 9 deletions libs/report/committers/src/lib/committers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface Options {
afterDate: string;
exclude: string[];
json: boolean;
// monthly: boolean;
directory: string;
}

export const reportCommittersCommand: CommandModule<object, Options> = {
Expand Down Expand Up @@ -44,13 +44,12 @@ export const reportCommittersCommand: CommandModule<object, Options> = {
default: false,
boolean: true,
},
// monthly: {
// alias: 'm',
// describe:
// 'Break down by calendar month, rather than by committer. (eg -m)',
// required: false,
// default: false,
// },
directory: {
alias: 'd',
describe: 'Directory to search',
required: false,
string: true,
},
} as CommandBuilder<unknown, Options>,
handler: run,
};
Expand All @@ -61,7 +60,15 @@ async function run(args: ArgumentsCamelCase<Options>): Promise<void> {

const ignores = args.exclude && args.exclude.length ? `-- . "!(${args.exclude.join('|')})"` : '';

const gitCommand = `git log --since "${afterDate}" --until "${beforeDateEndOfDay}" --pretty=format:${gitOutputFormat} ${ignores}`;
let gitCommand = `git log --since "${afterDate}" --until "${beforeDateEndOfDay}" --pretty=format:${gitOutputFormat} ${ignores}`;

const cwd = args.directory;

if (cwd) {
// According to git documentation, the -- is used to separate the options from the pathspecs
// https://git-scm.com/docs/git-log#Documentation/git-log.txt---ltpathgt82308203
gitCommand = `${gitCommand} -- ${cwd}`;
}

const result = await runCommand(gitCommand);

Expand Down

0 comments on commit dcb036d

Please sign in to comment.