-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcli.ts
More file actions
86 lines (71 loc) · 2.66 KB
/
cli.ts
File metadata and controls
86 lines (71 loc) · 2.66 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
83
84
85
86
#!/usr/bin/env node
import cac from 'cac';
import { script, Completion } from '../src/index.js';
import tab from '../src/cac.js';
import { setupCompletionForPackageManager } from './completion-handlers';
const packageManagers = ['npm', 'pnpm', 'yarn', 'bun'];
const shells = ['zsh', 'bash', 'fish', 'powershell'];
async function main() {
const cli = cac('tab');
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) {
const completion = new Completion();
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 @bombsh/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);
});
const completion = tab(cli);
cli.parse();
}
// function generateCompletionScript(packageManager: string, shell: string) {
// const name = packageManager;
// const executable = process.env.npm_execpath
// ? `${packageManager} exec @bombsh/tab ${packageManager}`
// : `node ${process.argv[1]} ${packageManager}`;
// script(shell as any, name, executable);
// }
function generateCompletionScript(packageManager: string, shell: string) {
const name = packageManager;
// this always points at the actual file on disk (TESTING)
const executable = `node ${process.argv[1]} ${packageManager}`;
script(shell as any, name, executable);
}
main().catch(console.error);