Skip to content

Commit 0ad8b91

Browse files
committed
fix: completion-handler __complete => complete
1 parent 8d06638 commit 0ad8b91

File tree

3 files changed

+38
-56
lines changed

3 files changed

+38
-56
lines changed

bin/completion-handlers.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ async function checkCliHasCompletions(
1515
): Promise<boolean> {
1616
try {
1717
debugLog(`Checking if ${cliName} has completions via ${packageManager}`);
18-
const command = `${packageManager} ${cliName} __complete`;
18+
const command = `${packageManager} ${cliName} complete --`;
1919
const result = execSync(command, {
2020
encoding: 'utf8',
2121
stdio: ['pipe', 'pipe', 'ignore'],
22-
timeout: 1000, // AMIR: we still havin issues with this, it still hangs if a cli doesn't have completions.
22+
timeout: 1000, // AMIR: we still havin issues with this, it still hangs if a cli doesn't have completions. longer timeout needed for shell completion system (shell → node → package manager → cli)
2323
});
2424
const hasCompletions = !!result.trim();
2525
debugLog(`${cliName} supports completions: ${hasCompletions}`);
@@ -39,13 +39,13 @@ async function getCliCompletions(
3939
const completeArgs = args.map((arg) =>
4040
arg.includes(' ') ? `"${arg}"` : arg
4141
);
42-
const completeCommand = `${packageManager} ${cliName} __complete ${completeArgs.join(' ')}`;
42+
const completeCommand = `${packageManager} ${cliName} complete -- ${completeArgs.join(' ')}`;
4343
debugLog(`Getting completions with command: ${completeCommand}`);
4444

4545
const result = execSync(completeCommand, {
4646
encoding: 'utf8',
4747
stdio: ['pipe', 'pipe', 'ignore'],
48-
timeout: 1000,
48+
timeout: 1000, // same: longer timeout needed for shell completion system (shell → node → package manager → cli)
4949
});
5050

5151
const completions = result.trim().split('\n').filter(Boolean);
Lines changed: 33 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/env node
22

3-
const cac = require('cac');
3+
import cac from 'cac';
4+
import tab from '../../dist/src/cac.js';
5+
46
const cli = cac('demo-cli-cac');
57

68
// Define version and help
@@ -29,61 +31,40 @@ cli
2931
console.log('Options:', options);
3032
});
3133

32-
// Manual implementation of completion for CAC
33-
if (process.argv[2] === '__complete') {
34-
const args = process.argv.slice(3);
35-
const toComplete = args[args.length - 1] || '';
36-
const previousArgs = args.slice(0, -1);
37-
38-
// Root command completion
39-
if (previousArgs.length === 0) {
40-
console.log('start\tStart the application');
41-
console.log('build\tBuild the application');
42-
console.log('--help\tDisplay help');
43-
console.log('--version\tOutput the version number');
44-
console.log('-c\tSpecify config file');
45-
console.log('--config\tSpecify config file');
46-
console.log('-d\tEnable debugging');
47-
console.log('--debug\tEnable debugging');
48-
process.exit(0);
49-
}
50-
51-
// Subcommand completion
52-
if (previousArgs[0] === 'start') {
53-
console.log('-p\tPort to use');
54-
console.log('--port\tPort to use');
55-
console.log('--help\tDisplay help');
34+
// Set up completion using the cac adapter
35+
const completion = await tab(cli);
5636

57-
// Port value completion if --port is the last arg
58-
if (
59-
previousArgs[previousArgs.length - 1] === '--port' ||
60-
previousArgs[previousArgs.length - 1] === '-p'
61-
) {
62-
console.log('3000\tDefault port');
63-
console.log('8080\tAlternative port');
37+
// custom config for options
38+
for (const command of completion.commands.values()) {
39+
for (const [optionName, config] of command.options.entries()) {
40+
if (optionName === '--port') {
41+
config.handler = () => {
42+
return [
43+
{ value: '3000', description: 'Default port' },
44+
{ value: '8080', description: 'Alternative port' },
45+
];
46+
};
6447
}
65-
process.exit(0);
66-
}
6748

68-
if (previousArgs[0] === 'build') {
69-
console.log('-m\tBuild mode');
70-
console.log('--mode\tBuild mode');
71-
console.log('--help\tDisplay help');
49+
if (optionName === '--mode') {
50+
config.handler = () => {
51+
return [
52+
{ value: 'development', description: 'Development mode' },
53+
{ value: 'production', description: 'Production mode' },
54+
{ value: 'test', description: 'Test mode' },
55+
];
56+
};
57+
}
7258

73-
// Mode value completion if --mode is the last arg
74-
if (
75-
previousArgs[previousArgs.length - 1] === '--mode' ||
76-
previousArgs[previousArgs.length - 1] === '-m'
77-
) {
78-
console.log('development\tDevelopment mode');
79-
console.log('production\tProduction mode');
80-
console.log('test\tTest mode');
59+
if (optionName === '--config') {
60+
config.handler = () => {
61+
return [
62+
{ value: 'config.json', description: 'JSON config file' },
63+
{ value: 'config.js', description: 'JavaScript config file' },
64+
];
65+
};
8166
}
82-
process.exit(0);
8367
}
84-
85-
process.exit(0);
86-
} else {
87-
// Parse CLI args
88-
cli.parse();
8968
}
69+
70+
cli.parse();

examples/demo-cli-cac/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "1.0.0",
44
"description": "Demo CLI using CAC for testing tab completions with pnpm",
55
"main": "demo-cli-cac.js",
6+
"type": "module",
67
"bin": {
78
"demo-cli-cac": "./demo-cli-cac.js"
89
},

0 commit comments

Comments
 (0)