Skip to content

Commit f8be62b

Browse files
committed
refactor: Ngrok::start method to test ngrok for errors before actually running the command for real.
- Refactor `start` method and test ngrok for errors with the command, and only run the command for real in a new CMD if the test was successful. Otherwise, we need to output the errors to the terminal. This is required now, since ngrok shipped with valet may be "too old" for some accounts. The ngrok executable will be updated to the latest version in the upcoming 3.2.0 version. But the user can always update valet's ngrok themselves by running `valet ngrok update`. - Added `testNgrok` method to run the command (without a new CMD) through Symfony's Process which returns the output.
1 parent cea78fe commit f8be62b

1 file changed

Lines changed: 33 additions & 1 deletion

File tree

cli/Valet/Ngrok.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,28 @@ public function start(string $site, int $port, $debug = false, array $options =
7171

7272
$newCMD = ($debug) ? "" : "start $newCMDtitle";
7373

74-
$this->cli->passthru("$newCMD $ngrokCommand");
74+
// Test ngrok to make sure there's no errors.
75+
$testNgrok = $this->testNgrok($ngrokCommand);
76+
77+
// If test was successful and no errors occurred...
78+
if ($testNgrok->isSuccessful()) {
79+
// Run the command again for real this time in a new CMD.
80+
$this->cli->passthru("$newCMD $ngrokCommand");
81+
}
82+
else {
83+
// Get the errors.
84+
$errors = $testNgrok->getOutput();
85+
86+
// Output the errors.
87+
error($errors . "\n");
88+
89+
// If errors have the ngrok error code of ERR_NGROK_121,
90+
// then the error is ngrok executable is "too old" and needs updating.
91+
// So output to prompt the user they can update it themselves with the valet command.
92+
if (strstr($errors, "ERR_NGROK_121")) {
93+
info("To update ngrok yourself, please run `valet ngrok update` and then upgrade the config file by running `valet ngrok config upgrade`\n");
94+
}
95+
}
7596
}
7697

7798
/**
@@ -171,4 +192,15 @@ protected function hasAuthToken(): bool {
171192
}
172193
return false;
173194
}
195+
196+
/**
197+
* Test the ngrok command to make sure there's no ngrok errors
198+
*
199+
* @param string $ngrokCommand The ngrok command to run
200+
*
201+
* @return \Valet\ProcessOutput
202+
*/
203+
private function testNgrok($ngrokCommand) {
204+
return $this->cli->run("$ngrokCommand");
205+
}
174206
}

0 commit comments

Comments
 (0)