Skip to content

Commit 1845b05

Browse files
committed
feat: add -v short parameter support for version display
- Add -v short parameter alias for --version in both main and dev CLI - Update Commander.js version configuration to support both -v and --version - Add comprehensive test cases for -v parameter in development and production modes - Add test to verify -v and --version produce identical output - Update test suite description to reflect broader version testing scope This allows users to use the more concise 'commit-msg -v' command to quickly check the version number. Change-Id: I139019908562ebed17623f1ccb9c536687e793f2 Co-developed-by: Cursor <noreply@cursor.com> Signed-off-by: Jiang Xin <zhiyou.jx@alibaba-inc.com>
1 parent 2d11f06 commit 1845b05

3 files changed

Lines changed: 68 additions & 3 deletions

File tree

src/bin/commit-msg.dev.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async function main() {
2222
program
2323
.name('commit-msg')
2424
.description('CLI tool for managing Git commit-msg hooks')
25-
.version(`${packageJson.name}: ${packageJson.version}`);
25+
.version(`${packageJson.name}: ${packageJson.version}`, '-v, --version');
2626

2727
program
2828
.command('install')

src/bin/commit-msg.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async function main() {
2323
program
2424
.name('commit-msg')
2525
.description('CLI tool for managing Git commit-msg hooks')
26-
.version(`${packageJson.name}: ${packageJson.version}`);
26+
.version(`${packageJson.name}: ${packageJson.version}`, '-v, --version');
2727

2828
program
2929
.command('install')

test/version.test.ts

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const devScript =
1414
? 'dev:node18'
1515
: 'dev:compat';
1616

17-
describe('commit-msg CLI --version tests', () => {
17+
describe('commit-msg CLI version tests', () => {
1818
// Test development mode --version
1919
it('should output version in development mode with correct prefix', () => {
2020
try {
@@ -34,6 +34,25 @@ describe('commit-msg CLI --version tests', () => {
3434
}
3535
});
3636

37+
// Test development mode -v
38+
it('should output version in development mode with -v parameter', () => {
39+
try {
40+
const output = execSync(`npm run ${devScript} -- -v`, {
41+
encoding: 'utf-8',
42+
});
43+
expect(output).toContain('@ai-coding-workshop/commit-msg:');
44+
} catch (error) {
45+
// If development mode fails, skip this test for older Node.js versions
46+
if (nodeMajorVersion < 20) {
47+
console.log(
48+
`Skipping development mode -v test for Node.js ${nodeVersion} due to ESM limitations`
49+
);
50+
return;
51+
}
52+
throw error;
53+
}
54+
});
55+
3756
// Test production mode --version
3857
it('should output version in production mode with correct prefix', () => {
3958
// First build the project
@@ -46,6 +65,18 @@ describe('commit-msg CLI --version tests', () => {
4665
expect(output).toContain('@ai-coding-workshop/commit-msg:');
4766
});
4867

68+
// Test production mode -v
69+
it('should output version in production mode with -v parameter', () => {
70+
// First build the project
71+
execSync('npm run build', { stdio: 'inherit' });
72+
73+
// Then test the compiled version with -v
74+
const output = execSync('node dist/bin/commit-msg.js -v', {
75+
encoding: 'utf-8',
76+
});
77+
expect(output).toContain('@ai-coding-workshop/commit-msg:');
78+
});
79+
4980
// Test that both modes output the same version
5081
it('should output the same version in both development and production modes', () => {
5182
// For Node.js < 20, skip this test as development mode may not work
@@ -73,4 +104,38 @@ describe('commit-msg CLI --version tests', () => {
73104

74105
expect(devVersion).toBe(prodVersion);
75106
});
107+
108+
// Test that -v and --version produce the same output
109+
it('should produce the same output for -v and --version parameters', () => {
110+
// Build the project first
111+
execSync('npm run build', { stdio: 'inherit' });
112+
113+
// Test production mode
114+
const versionOutput = execSync('node dist/bin/commit-msg.js --version', {
115+
encoding: 'utf-8',
116+
});
117+
const vOutput = execSync('node dist/bin/commit-msg.js -v', {
118+
encoding: 'utf-8',
119+
});
120+
121+
expect(vOutput).toBe(versionOutput);
122+
123+
// Test development mode if supported
124+
if (nodeMajorVersion >= 20) {
125+
try {
126+
const devVersionOutput = execSync(`npm run ${devScript} -- --version`, {
127+
encoding: 'utf-8',
128+
});
129+
const devVOutput = execSync(`npm run ${devScript} -- -v`, {
130+
encoding: 'utf-8',
131+
});
132+
133+
expect(devVOutput).toBe(devVersionOutput);
134+
} catch (error) {
135+
console.log(
136+
`Skipping development mode equivalence test for Node.js ${nodeVersion} due to ESM limitations`
137+
);
138+
}
139+
}
140+
});
76141
});

0 commit comments

Comments
 (0)