-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcli.ts
More file actions
82 lines (68 loc) · 2.55 KB
/
cli.ts
File metadata and controls
82 lines (68 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env node
import cac from 'cac';
import { script } from '../src/t.js';
import tab from '../src/cac.js';
import { setupCompletionForPackageManager } from './completion-handlers';
import { PackageManagerCompletion } from './package-manager-completion.js';
const packageManagers = ['npm', 'pnpm', 'yarn', 'bun'];
const shells = ['zsh', 'bash', 'fish', 'powershell'];
async function main() {
const cli = cac('tab');
// TODO: aren't these conditions are already handled by cac?
const args = process.argv.slice(2);
if (args.length >= 2 && args[1] === 'complete') {
const packageManager = args[0];
if (!packageManagers.includes(packageManager)) {
console.error(`Error: Unsupported package manager "${packageManager}"`);
console.error(
`Supported package managers: ${packageManagers.join(', ')}`
);
process.exit(1);
}
const dashIndex = process.argv.indexOf('--');
if (dashIndex !== -1) {
// Use the new PackageManagerCompletion wrapper
const completion = new PackageManagerCompletion(packageManager);
await setupCompletionForPackageManager(packageManager, completion);
const toComplete = process.argv.slice(dashIndex + 1);
await completion.parse(toComplete);
process.exit(0);
} else {
console.error(`Error: Expected '--' followed by command to complete`);
console.error(
`Example: ${packageManager} exec @bomb.sh/tab ${packageManager} complete -- command-to-complete`
);
process.exit(1);
}
}
cli
.command(
'<packageManager> <shell>',
'Generate shell completion script for a package manager'
)
.action(async (packageManager, shell) => {
if (!packageManagers.includes(packageManager)) {
console.error(`Error: Unsupported package manager "${packageManager}"`);
console.error(
`Supported package managers: ${packageManagers.join(', ')}`
);
process.exit(1);
}
if (!shells.includes(shell)) {
console.error(`Error: Unsupported shell "${shell}"`);
console.error(`Supported shells: ${shells.join(', ')}`);
process.exit(1);
}
generateCompletionScript(packageManager, shell);
});
tab(cli);
cli.parse();
}
function generateCompletionScript(packageManager: string, shell: string) {
const name = packageManager;
const executable = process.env.npm_config_user_agent
? `npx --yes @bomb.sh/tab ${packageManager}`
: `node ${process.argv[1]} ${packageManager}`;
script(shell as any, name, executable);
}
main().catch(console.error);