Skip to content

Commit

Permalink
✨ 'out' command
Browse files Browse the repository at this point in the history
⬆️ upgraded dependencies
🎨 pint

Signed-off-by: bnomei <[email protected]>
  • Loading branch information
bnomei committed Aug 12, 2024
1 parent a19145c commit 6ff67b6
Show file tree
Hide file tree
Showing 13 changed files with 255 additions and 194 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ This plugin comes with a [few commands](https://github.com/bnomei/kirby3-janitor
- `janitor:job`, run a callback
- `janitor:maintenance`, toggle maintenance mode
- `janitor:open`, triggers opening of an URL in panel
- `janitor:out`, sends a message to the CLI output stream
- `janitor:pipe`, map input argument to output argument
- `janitor:render`, render a certain page or all pages (to create thumb jobs)
- `janitor:thumbs`, process thumb jobs of a certain page or all pages
Expand Down
4 changes: 4 additions & 0 deletions classes/Janitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ public function option(?string $key = null): mixed

public function command(string $command): array
{
if (php_sapi_name() !== 'cli' && ! Str::contains($command, ' --quiet')) {
$command .= ' --quiet';
}

[$name, $args] = Janitor::parseCommand($command);
$args = Janitor::resolveQueriesInCommand($args); // like a "lazy/smart" `{( page.callme )}`

Expand Down
4 changes: 2 additions & 2 deletions commands/backupzip.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
$output = str_replace('{{ timestamp }}', (string) (time()), $output);
Dir::make(dirname($output));

$zip = new ZipArchive();
$zip = new ZipArchive;
if ($zip->open($output, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) !== true) {
$cli->error('Failed to create: '.$output);
janitor()->data($cli->arg('command'), [
Expand All @@ -68,7 +68,7 @@
} else {
$roots = explode(',', $roots);
}
$finder = new Finder();
$finder = new Finder;
$finder->files()->ignoreDotFiles(false)->in($roots);
if (! empty($cli->arg('date'))) {
$finder->date($cli->arg('date'));
Expand Down
5 changes: 4 additions & 1 deletion commands/flush.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
'description' => 'Flush a cache',
'args' => [
'name' => [
'prefix' => 'n',
'longPrefix' => 'name',
'description' => 'Name of the cache',
'defaultValue' => 'pages',
],
],
'command' => static function (CLI $cli): void {
$name = $cli->argOrPrompt('name', 'Which cache should be emptied? (press <Enter> to clear the pages cache)', false);
$name = $cli->arg('name');
$name = empty($name) ? 'pages' : $name;

$cli->kirby()->cache($name)->flush();
Expand Down
52 changes: 52 additions & 0 deletions commands/out.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

if (! class_exists('Bnomei\Janitor')) {
require_once __DIR__.'/../classes/Janitor.php';
}

use Bnomei\Janitor;
use Kirby\CLI\CLI;
use Kirby\Cms\User;

return [
'description' => 'Prints text to the terminal',
'args' => [
'msg' => [
'longPrefix' => 'msg',
'description' => 'Message',
'defaultValue' => '',
],
'level' => [
'prefix' => 'l',
'longPrefix' => 'level',
'description' => 'Message level like out/success/error',
'defaultValue' => 'out',
],
'time' => [
'prefix' => 't',
'longPrefix' => 'time',
'description' => 'Prefix message with timestamp',
'noValue' => true,
],
] + Janitor::ARGS, // page, file, user, site, data, model
'command' => static function (CLI $cli): void {
$time = $cli->arg('time') ? '['.date('Y-m-d H:i:s').'] ' : '';
$level = match ($cli->arg('level')) {
'success' => 'success',
'error' => 'error',
default => 'out',
};
$user = Janitor::resolveModel($cli->arg('user'));
$user = $user instanceof User ? ' ('.$user->email().') ' : '';
$msg = sprintf('%s%s%s', $time, $user, Janitor::query($cli->arg('msg').$cli->arg('data'), Janitor::resolveModel($cli->arg('model'))));

$cli->{$level}($msg);

janitor()->data($cli->arg('command'), [
'status' => 200,
'message' => $msg,
]);
},
];
4 changes: 2 additions & 2 deletions commands/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private function getPageIDs(CLI $cli): array
$ids[] = $page->id(); // this should not fully load the page yet
}
} else { // performance optimized way to get ids for `site.index`
$finder = new Finder();
$finder = new Finder;
$finder->directories()
->in($cli->kirby()->roots()->content());
foreach ($finder as $folder) {
Expand Down Expand Up @@ -203,6 +203,6 @@ private function remotePageContent(string $pageId): ?string
],
] + Janitor::ARGS, // page, file, user, site, data, model
'command' => static function (CLI $cli): void {
(new JanitorRenderCommand())->run($cli);
(new JanitorRenderCommand)->run($cli);
},
];
4 changes: 2 additions & 2 deletions commands/thumbs.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@
Dir::make($root); // make sure folder exists

// find existing thumbs
$finder = new Finder();
$finder = new Finder;
$finder->files()
->in($root)
->name('/\.(?:avif|gif|jpeg|jpg|png|webp)$/');
$cli->blue(iterator_count($finder).' existing thumbs');

// job files
$finder = new Finder();
$finder = new Finder;
$finder->files()
->in($root)
->ignoreDotFiles(false)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "bnomei/kirby3-janitor",
"type": "kirby-plugin",
"version": "4.2.0",
"version": "4.3.0",
"license": "MIT",
"homepage": "https://github.com/bnomei/kirby3-janitor",
"description": "Kirby Plugin for running commands like cleaning the cache from within the Panel, PHP code or a cronjob",
Expand Down
Loading

0 comments on commit 6ff67b6

Please sign in to comment.