Skip to content

Commit b6cacb9

Browse files
committed
feat(cli): implement new --last CLI flag
The upgrade of ES2017 to ES2022 is because of the use of `Object.hasOwn` API which is less ugly than hardcoding `undefined`.
1 parent f0ba2de commit b6cacb9

File tree

4 files changed

+44
-14
lines changed

4 files changed

+44
-14
lines changed

@commitlint/cli/src/cli.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ test('should produce success output with --verbose flag', async () => {
5555
test('should produce last commit and success output with --verbose flag', async () => {
5656
const cwd = await gitBootstrap('fixtures/simple');
5757
await execa('git', ['add', 'commitlint.config.js'], {cwd});
58-
const commit = await execa(
58+
await execa(
5959
'git',
6060
['commit', '-m', '"test: this should work"'],
6161
{cwd}
@@ -543,6 +543,7 @@ test('should print help', async () => {
543543
-H, --help-url help url in error message [string]
544544
-f, --from lower end of the commit range to lint; applies if edit=false [string]
545545
--git-log-args additional git log arguments as space separated string, example '--first-parent --cherry-pick' [string]
546+
-l, --last just analyze the last commit; applies if edit=false [boolean]
546547
-o, --format output format of the results [string]
547548
-p, --parser-preset configuration preset to use for conventional-commits-parser [string]
548549
-q, --quiet toggle console output [boolean] [default: false]

@commitlint/cli/src/cli.ts

+39-11
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ const cli = yargs(process.argv.slice(2))
9595
"additional git log arguments as space separated string, example '--first-parent --cherry-pick'",
9696
type: 'string',
9797
},
98+
last: {
99+
alias: 'l',
100+
description: 'just analyze the last commit; applies if edit=false',
101+
type: 'boolean',
102+
},
98103
format: {
99104
alias: 'o',
100105
description: 'output format of the results',
@@ -214,15 +219,34 @@ async function main(args: MainArgs): Promise<void> {
214219

215220
const fromStdin = checkFromStdin(raw, flags);
216221

217-
const input = await (fromStdin
218-
? stdin()
219-
: read({
220-
to: flags.to,
221-
from: flags.from,
222-
edit: flags.edit,
223-
cwd: flags.cwd,
224-
gitLogArgs: flags['git-log-args'],
225-
}));
222+
if (
223+
Object.hasOwn(flags, 'last') &&
224+
(Object.hasOwn(flags, 'from') || Object.hasOwn(flags, 'to') || flags.edit)
225+
) {
226+
const err = new CliError(
227+
'Please use the --last flag alone. The --last flag should not be used with --to or --from or --edit.',
228+
pkg.name
229+
);
230+
cli.showHelp('log');
231+
console.log(err.message);
232+
throw err;
233+
}
234+
235+
let input;
236+
if (Object.hasOwn(flags, 'last')) {
237+
const executeGitCommand = await execa('git', ['log', '-1', '--pretty=%B']);
238+
input = executeGitCommand.stdout;
239+
} else {
240+
input = await (fromStdin
241+
? stdin()
242+
: read({
243+
to: flags.to,
244+
from: flags.from,
245+
edit: flags.edit,
246+
cwd: flags.cwd,
247+
gitLogArgs: flags['git-log-args'],
248+
}));
249+
}
226250

227251
const messages = (Array.isArray(input) ? input : [input])
228252
.filter((message) => typeof message === 'string')
@@ -231,7 +255,7 @@ async function main(args: MainArgs): Promise<void> {
231255

232256
if (messages.length === 0 && !checkFromRepository(flags)) {
233257
const err = new CliError(
234-
'[input] is required: supply via stdin, or --env or --edit or --from and --to',
258+
'[input] is required: supply via stdin, or --env or --edit or --last or --from and --to',
235259
pkg.name
236260
);
237261
cli.showHelp('log');
@@ -374,7 +398,11 @@ function checkFromEdit(flags: CliFlags): boolean {
374398
}
375399

376400
function checkFromHistory(flags: CliFlags): boolean {
377-
return typeof flags.from === 'string' || typeof flags.to === 'string';
401+
return (
402+
typeof flags.from === 'string' ||
403+
typeof flags.to === 'string' ||
404+
typeof flags.last === 'boolean'
405+
);
378406
}
379407

380408
function normalizeFlags(flags: CliFlags): CliFlags {

@commitlint/cli/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface CliFlags {
99
'help-url'?: string;
1010
from?: string;
1111
'git-log-args'?: string;
12+
last?: boolean;
1213
format?: string;
1314
'parser-preset'?: string;
1415
quiet: boolean;

tsconfig.shared.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
3-
"target": "ES2017",
4-
"lib": ["es2017"],
3+
"target": "ES2022",
4+
"lib": ["es2022"],
55
"declaration": true,
66
"declarationMap": true,
77
"sourceMap": true,

0 commit comments

Comments
 (0)