Skip to content

Commit 7c6b466

Browse files
committed
feat: make install command the default when no arguments provided
Changes: - When running commit-msg without arguments, automatically run the install command instead of showing help - Updated README.md to document this behavior - Updated tests to reflect the new behavior This makes it easier for users to install the hook by simply running `npx commit-msg` without any arguments. Change-Id: I8593343a0f4080c4c6554b0920254cd1f4a8561e Co-developed-by: Claude <noreply@anthropic.com> Signed-off-by: Jiang Xin <zhiyou.jx@alibaba-inc.com>
1 parent fc336c8 commit 7c6b466

4 files changed

Lines changed: 23 additions & 11 deletions

File tree

‎README.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ npx commit-msg install
3131
This will install the commit-msg hook in your repository's `.git/hooks` directory,
3232
or in the hook path defined by `core.hooksPath` config variables.
3333

34+
If you run `commit-msg` without any arguments, it will automatically run the `install` command:
35+
36+
```bash
37+
npx commit-msg
38+
```
39+
40+
This is equivalent to running `npx commit-msg install`.
41+
3442
### Development
3543

3644
For development, you can run the tool directly with ts-node:
@@ -83,6 +91,8 @@ When building the project, only the production files are compiled to the `dist`
8391
- `install` - Install the commit-msg hook in the current Git repository
8492
- `exec` - Execute the commit-msg hook logic (used internally by the hook)
8593

94+
If no command is specified, the `install` command will be run by default.
95+
8696
## Configuration
8797

8898
The tool follows these Git configuration options:

‎src/bin/commit-msg.dev.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,16 @@ async function main() {
203203
}
204204
});
205205

206-
// Add default command to handle commit message file directly
206+
// Add default command to handle commit message file directly or run install when no args
207207
program
208208
.description('CLI tool for managing Git commit-msg hooks')
209209
.arguments('[message-file]')
210210
.action(async (messageFile, options) => {
211-
// If no message file is provided, show help
211+
const verbose = getVerboseMode({ ...program.opts(), ...options });
212+
213+
// If no message file is provided, run install command
212214
if (!messageFile) {
213-
program.help();
215+
await handleInstallCommand(verbose, install, checkAndUpgrade);
214216
return;
215217
}
216218

@@ -220,7 +222,6 @@ async function main() {
220222
throw new CleanError(`Command or file not found: ${messageFile}`);
221223
}
222224

223-
const verbose = getVerboseMode({ ...program.opts(), ...options });
224225
await handleExecCommand(messageFile, verbose, exec, checkAndUpgrade);
225226
});
226227

‎src/bin/commit-msg.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,16 @@ async function main() {
203203
}
204204
});
205205

206-
// Add default command to handle commit message file directly
206+
// Add default command to handle commit message file directly or run install when no args
207207
program
208208
.description('CLI tool for managing Git commit-msg hooks')
209209
.arguments('[message-file]')
210210
.action(async (messageFile, options) => {
211-
// If no message file is provided, show help
211+
const verbose = getVerboseMode({ ...program.opts(), ...options });
212+
213+
// If no message file is provided, run install command
212214
if (!messageFile) {
213-
program.help();
215+
await handleInstallCommand(verbose, install, checkAndUpgrade);
214216
return;
215217
}
216218

@@ -220,7 +222,6 @@ async function main() {
220222
throw new CleanError(`Command or file not found: ${messageFile}`);
221223
}
222224

223-
const verbose = getVerboseMode({ ...program.opts(), ...options });
224225
await handleExecCommand(messageFile, verbose, exec, checkAndUpgrade);
225226
});
226227

‎test/direct-hook.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ describe('direct commit-msg hook execution tests', () => {
8787
);
8888
});
8989

90-
it('should show help when executed without arguments', () => {
90+
it('should run install command when executed without arguments', () => {
9191
// Execute commit-msg directly without arguments
9292
const result = spawnSync(
9393
'node',
@@ -100,9 +100,9 @@ describe('direct commit-msg hook execution tests', () => {
100100
);
101101

102102
expect(result.status).toBe(0);
103-
expect(result.stdout).toContain('Usage: commit-msg');
103+
expect(result.stdout).toContain('Installing commit-msg hook...');
104104
expect(result.stdout).toContain(
105-
'CLI tool for managing Git commit-msg hooks'
105+
'Commit-msg hook installed successfully!'
106106
);
107107
});
108108

0 commit comments

Comments
 (0)