-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4fb9897
commit 0a54d91
Showing
10 changed files
with
234 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const chokidar = require('chokidar'); | ||
|
||
const paths = JSON.parse(process.argv[2]); | ||
const options = JSON.parse(process.argv[3] || {}); | ||
|
||
const watcher = chokidar.watch(paths, { | ||
ignoreInitial: true, | ||
...options | ||
}); | ||
|
||
watcher | ||
.on('add', path => console.log(`fileCreated - ${path}`)) | ||
.on('change', path => console.log(`fileUpdated - ${path}`)) | ||
.on('unlink', path => console.log(`fileDeleted - ${path}`)) | ||
.on('addDir', path => console.log(`directoryCreated - ${path}`)) | ||
.on('unlinkDir', path => console.log(`directoryDeleted - ${path}`)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
/** | ||
* Part of cati project. | ||
* | ||
* @copyright Copyright (C) 2023 __ORGANIZATION__. | ||
* @license __LICENSE__ | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Windwalker\Core\CliServer; | ||
|
||
use Symfony\Component\Process\PhpExecutableFinder; | ||
use Windwalker\Core\Filesystem\FileWatcher; | ||
|
||
/** | ||
* Trait CliServerTrait | ||
*/ | ||
trait CliServerTrait | ||
{ | ||
protected function getPhpBinary(): false|string | ||
{ | ||
return (new PhpExecutableFinder())->find(); | ||
} | ||
|
||
public function createFileWatcher(string $mainFile): FileWatcher | ||
{ | ||
$paths = (array) $this->app->config('reactor.watch'); | ||
|
||
$paths[] = $mainFile; | ||
|
||
return FileWatcher::paths(...$paths) | ||
->usePolling(false) | ||
->awaitWriteFinish(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?php | ||
|
||
/** | ||
* Part of cati project. | ||
* | ||
* @copyright Copyright (C) 2023 __ORGANIZATION__. | ||
* @license __LICENSE__ | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Windwalker\Core\Filesystem; | ||
|
||
use Spatie\Watcher\Watch; | ||
use Symfony\Component\Process\ExecutableFinder; | ||
use Symfony\Component\Process\Process; | ||
use Windwalker\Utilities\Options\OptionAccessTrait; | ||
|
||
/** | ||
* The FileWatcher class. | ||
*/ | ||
class FileWatcher extends Watch | ||
{ | ||
use OptionAccessTrait; | ||
|
||
protected ?Process $process = null; | ||
|
||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
|
||
$this->options['usePolling'] = true; | ||
} | ||
|
||
public static function path(string $path): self | ||
{ | ||
return (new static())->setPaths($path); | ||
} | ||
|
||
public static function paths(...$paths): self | ||
{ | ||
return (new static())->setPaths($paths); | ||
} | ||
|
||
public function isRunning(): bool | ||
{ | ||
return $this->process?->isRunning(); | ||
} | ||
|
||
public function getChangedInfo(): string | ||
{ | ||
return $this->process?->getIncrementalOutput(); | ||
} | ||
|
||
public function hasChanged(): bool | ||
{ | ||
return (bool) $this->getChangedInfo(); | ||
} | ||
|
||
public function onFileChanged(callable $handler): self | ||
{ | ||
$this->onFileCreated[] = $handler; | ||
$this->onFileUpdated[] = $handler; | ||
$this->onFileDeleted[] = $handler; | ||
|
||
return $this; | ||
} | ||
|
||
public function doActions(string $output): void | ||
{ | ||
$this->actOnOutput($output); | ||
} | ||
|
||
public function listen(): static | ||
{ | ||
$this->process = $this->getWatchProcess(); | ||
|
||
return $this; | ||
} | ||
|
||
protected function getWatchProcess(): Process | ||
{ | ||
$command = [ | ||
(new ExecutableFinder())->find('node'), | ||
dirname(__DIR__, 3) . '/bin/file-watcher.js', | ||
json_encode($this->paths, JSON_THROW_ON_ERROR), | ||
json_encode($this->options, JSON_THROW_ON_ERROR), | ||
]; | ||
|
||
$process = new Process( | ||
command: $command, | ||
timeout: null, | ||
); | ||
|
||
$process->start(); | ||
|
||
return $process; | ||
} | ||
|
||
public function stop(int $timeout = 10, int $signal = null): ?int | ||
{ | ||
return $this->getRunningProcess()->stop($timeout, $signal); | ||
} | ||
|
||
public function getRunningProcess(): Process | ||
{ | ||
if (!$this->process) { | ||
throw new \RuntimeException('Process not exists'); | ||
} | ||
|
||
if (!$this->process->isRunning()) { | ||
throw new \RuntimeException('Process not running.'); | ||
} | ||
|
||
return $this->process; | ||
} | ||
|
||
public function getProcess(): ?Process | ||
{ | ||
return $this->process; | ||
} | ||
|
||
public function usePolling(bool $value): static | ||
{ | ||
return $this->setOption('usePolling', $value); | ||
} | ||
|
||
public function awaitWriteFinish(int $stabilityThreshold = 2000, int $pollInterval = 100): static | ||
{ | ||
return $this->setOption('awaitWriteFinish', compact('stabilityThreshold', 'pollInterval')); | ||
} | ||
|
||
public function disableAwaitWriteFinish(): static | ||
{ | ||
unset($this->options['awaitWriteFinish']); | ||
|
||
return $this; | ||
} | ||
} |