-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·97 lines (86 loc) · 2.52 KB
/
index.js
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
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env node
const { program } = require('commander');
const { installPHP } = require('./lib/commands/install');
const { uninstallPHP } = require('./lib/commands/uninstall');
const { listPHPVersions } = require('./lib/commands/list');
const { usePHPVersion } = require('./lib/commands/use');
const { autoSwitchPHPVersion } = require('./lib/utils/phpvmrc');
const packageJson = require('./package.json');
// autoSwitchPHPVersion(); // Auto-switch PHP version based on .phpvmrc
/**
* Configure the output for the program.
*/
program.configureOutput({
writeOut: (str) => process.stdout.write(str),
writeErr: (str) => process.stderr.write(str),
outputError: (str) => process.stderr.write(`Error: ${str}`), // More descriptive error output
});
/**
* Set the program name, description, and version.
*/
program
.name(packageJson.name)
.description(packageJson.description)
.version(packageJson.version);
/**
* Command to install a specific PHP version.
*/
program
.command('install <version>')
.alias('i')
.description('Install a specific PHP version')
.action((version) => installPHP(version));
/**
* Command to uninstall a specific PHP version.
*/
program
.command('uninstall <version>')
.alias('rm')
.description('Uninstall a specific PHP version')
.action((version) => uninstallPHP(version, program));
/**
* Command to list all installed PHP versions.
*/
program
.command('list')
.alias('ls')
.description('List installed PHP versions')
.action(() => listPHPVersions(program));
/**
* Command to switch to a specific PHP version.
*/
program
.command('use <version>')
.alias('switch')
.description('Switch to a specific PHP version')
.action((version) => {
try {
usePHPVersion(version, program);
console.log(`Switched to PHP ${version}`);
} catch (err) {
console.error(`Failed to switch PHP version: ${err.message}`);
process.exit(1); // Exit with an error code on failure
}
});
/**
* Command to simulate an error.
*/
program
.command('error')
.description('Simulate an error')
.action(() => {
process.stderr.write('Something went wrong!\n');
process.exit(1); // Exit with error code 1 to indicate failure
});
/**
* Global error handling for invalid commands.
*/
program.on('command:*', (invalidCommand) => {
process.stderr.write(`Invalid command: ${invalidCommand.join(' ')}\n`);
program.outputHelp(); // Suggest available commands
process.exit(1); // Exit with error code
});
/**
* Parse the command-line arguments.
*/
program.parse(process.argv);