Skip to content

Commit 5a06127

Browse files
committed
refactor: Improve install.php script robustness and input handling
Enhances the installation script with: - Handling piped input scenarios - Improved input processing in prompt() function - Removed redundant comments - Simplified error handling for file downloads - Streamlined script logic and readability
1 parent 3c32e55 commit 5a06127

1 file changed

Lines changed: 11 additions & 21 deletions

File tree

install.php

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
* This script downloads and installs Cursor rules into your project's .cursor/rules directory.
99
*/
1010

11-
// ANSI color codes for terminal output
1211
const COLORS = [
1312
'red' => "\033[0;31m",
1413
'green' => "\033[0;32m",
@@ -17,32 +16,29 @@
1716
'reset' => "\033[0m",
1817
];
1918

20-
/**
21-
* Print colored message to terminal.
22-
*/
2319
function colorize(string $message, string $color): string {
2420
return COLORS[$color] . $message . COLORS['reset'];
2521
}
2622

27-
/**
28-
* Print message to terminal.
29-
*/
3023
function println(string $message = ''): void {
3124
echo $message . PHP_EOL;
3225
}
3326

34-
/**
35-
* Get user input with optional default value.
36-
*/
3727
function prompt(string $message, string $default = ''): string {
28+
// Check if input is piped
29+
$isPiped = !posix_isatty(STDIN);
30+
if ($isPiped) {
31+
return $default;
32+
}
33+
3834
echo $message . ($default ? " [{$default}]" : '') . ': ';
39-
$input = trim(fgets(STDIN));
40-
return $input ?: $default;
35+
$input = fgets(STDIN);
36+
if ($input === false) {
37+
return $default;
38+
}
39+
return trim($input) ?: $default;
4140
}
4241

43-
/**
44-
* Download a file from URL.
45-
*/
4642
function downloadFile(string $url, string $destination): bool {
4743
$ch = curl_init($url);
4844
if ($ch === FALSE) {
@@ -67,7 +63,6 @@ function downloadFile(string $url, string $destination): bool {
6763
return $success !== FALSE;
6864
}
6965

70-
// Script start
7166
println(colorize('Cursor Rules Installer', 'blue'));
7267
println('================================');
7368
println();
@@ -93,7 +88,6 @@ function downloadFile(string $url, string $destination): bool {
9388
'vue-best-practices.mdc',
9489
];
9590

96-
// Check if target directory exists
9791
if (!file_exists($targetDir)) {
9892
println(colorize("Notice: Directory '{$targetDir}' does not exist.", 'yellow'));
9993
println("It will be created during installation.");
@@ -114,7 +108,6 @@ function downloadFile(string $url, string $destination): bool {
114108
println();
115109
println('Starting installation...');
116110

117-
// Create directory if it doesn't exist
118111
if (!file_exists($targetDir)) {
119112
if (!mkdir($targetDir, 0755, TRUE)) {
120113
println(colorize("Error: Failed to create directory '{$targetDir}'", 'red'));
@@ -123,7 +116,6 @@ function downloadFile(string $url, string $destination): bool {
123116
println(colorize("Created directory: {$targetDir}", 'green'));
124117
}
125118

126-
// Download and install rules
127119
$installedFiles = [];
128120
foreach ($ruleFiles as $file) {
129121
$url = "{$baseUrl}/{$file}";
@@ -137,7 +129,6 @@ function downloadFile(string $url, string $destination): bool {
137129
}
138130
}
139131

140-
// Installation summary
141132
println();
142133
println(colorize('Installation Complete!', 'green'));
143134
println('--------------------------------');
@@ -149,5 +140,4 @@ function downloadFile(string $url, string $destination): bool {
149140
println(colorize('Cursor rules have been installed successfully!', 'green'));
150141
println('You can now use these rules in your project.');
151142

152-
// Self-delete
153143
@unlink(__FILE__);

0 commit comments

Comments
 (0)