From 986c9ad82003dd39873abe1afc7e99e288f231cd Mon Sep 17 00:00:00 2001 From: Richard Le Poidevin Date: Tue, 18 Oct 2022 14:51:29 +0100 Subject: [PATCH] Migrate commands from Laravel Storyblok --- src/Console/ComponentListCommand.php | 75 ++++++++++++++++++++++++++ src/Console/ExportComponentCommand.php | 2 +- src/Console/ExportStoryCommand.php | 63 ++++++++++++++++++++++ src/Console/ImportStoryCommand.php | 69 ++++++++++++++++++++++++ src/StoryblokCliServiceProvider.php | 8 ++- 5 files changed, 215 insertions(+), 2 deletions(-) create mode 100644 src/Console/ComponentListCommand.php create mode 100644 src/Console/ExportStoryCommand.php create mode 100644 src/Console/ImportStoryCommand.php diff --git a/src/Console/ComponentListCommand.php b/src/Console/ComponentListCommand.php new file mode 100644 index 0000000..43c32a1 --- /dev/null +++ b/src/Console/ComponentListCommand.php @@ -0,0 +1,75 @@ +managementClient = new ManagementClient(config('storyblok-cli.oauth_token')); + } + + /** + * Execute the console command. + * + * @return void + */ + public function handle() + { + $this->requestComponents(); + + $additionalFields = $this->option('additional-fields') ? + Str::of($this->option('additional-fields'))->explode(',') + : collect(); + + $rows = $this->sbComponents->map(function ($c) use ($additionalFields) { + $mapped = [ + 'name' => $c['name'], + 'display_name' => $c['display_name'], + 'has_image' => $c['image'] ? "true" : 'false', + 'has_template' => $c['preview_tmpl'] ? "true" : 'false', + ]; + + $mappedAdditional = collect($c)->only($additionalFields); + + return array_merge($mapped, $mappedAdditional->toArray()); + }); + + $this->table( + array_keys($rows->first()), + $rows + ); + } + +} diff --git a/src/Console/ExportComponentCommand.php b/src/Console/ExportComponentCommand.php index 0838f41..cf7d9d4 100644 --- a/src/Console/ExportComponentCommand.php +++ b/src/Console/ExportComponentCommand.php @@ -87,7 +87,7 @@ protected function exportComponent($componentName) Storage::put($componentName . '.json', json_encode($component, JSON_THROW_ON_ERROR)); - $this->info($componentName . '.json exported'); + $this->info('Saved to storage: ' . $componentName . '.json'); return $component; } diff --git a/src/Console/ExportStoryCommand.php b/src/Console/ExportStoryCommand.php new file mode 100644 index 0000000..d82b885 --- /dev/null +++ b/src/Console/ExportStoryCommand.php @@ -0,0 +1,63 @@ +client = new ManagementClient(config('storyblok-cli.oauth_token')); + + parent::__construct(); + } + + /** + * Execute the console command. + * + * @return int + */ + public function handle() + { + $storyExists = $this->client->get('spaces/' . config('storyblok-cli.space_id') . '/stories/', [ + 'with_slug' => $this->argument('slug') + ])->getBody()['stories']; + + if ($storyExists) { + $filename = 'storyblok-' . Str::of($this->argument('slug'))->replace('/', '-')->slug() . '.json'; + + $story = $this->client->get('spaces/' . config('storyblok-cli.space_id') . '/stories/' . $storyExists[0]['id'])->getBody(); + + $json = json_encode($story); + + Storage::put($filename, $json); + + $this->info('Saved to storage: ' . $filename); + } else { + $this->warn('There is no story for your slug: ' . $this->argument('slug')); + } + } +} diff --git a/src/Console/ImportStoryCommand.php b/src/Console/ImportStoryCommand.php new file mode 100644 index 0000000..d911ef6 --- /dev/null +++ b/src/Console/ImportStoryCommand.php @@ -0,0 +1,69 @@ +client = new ManagementClient(config('storyblok-cli.oauth_token')); + + parent::__construct(); + } + + /** + * Execute the console command. + * + * @return int + */ + public function handle() + { + // TODO - interactive console for selecting save folder? + + $storyExists = $this->client->get('spaces/' . config('storyblok-cli.space_id') . '/stories/', [ + 'with_slug' => $this->argument('slug') + ])->getBody()['stories']; + + if (!$storyExists) { + $source = json_decode(Storage::get($this->argument('filename')), true); + + $story = [ + "story" => [ + "name" => $source['story']['name'] . ' (Imported)', + "slug" => $this->argument('slug'), + "content" => $source['story']['content'], + ], + "publish" => 1 + ]; + + $importedStory = $this->client->post('spaces/' . config('storyblok-cli.space_id') . '/stories/', $story)->getBody()['story']; + + $this->info('Imported into Storyblok: ' . $importedStory['name']); + } else { + $this->warn('Story already exists for: ' . $this->argument('slug')); + } + } +} diff --git a/src/StoryblokCliServiceProvider.php b/src/StoryblokCliServiceProvider.php index 3e17040..dac4918 100644 --- a/src/StoryblokCliServiceProvider.php +++ b/src/StoryblokCliServiceProvider.php @@ -3,9 +3,12 @@ namespace Riclep\StoryblokCli; use Illuminate\Support\ServiceProvider; +use Riclep\StoryblokCli\Console\ComponentListCommand; use Riclep\StoryblokCli\Console\DiffComponentCommand; use Riclep\StoryblokCli\Console\ExportComponentCommand; +use Riclep\StoryblokCli\Console\ExportStoryCommand; use Riclep\StoryblokCli\Console\ImportComponentCommand; +use Riclep\StoryblokCli\Console\ImportStoryCommand; class StoryblokCliServiceProvider extends ServiceProvider { @@ -15,9 +18,12 @@ class StoryblokCliServiceProvider extends ServiceProvider public function boot() { $this->commands([ + ComponentListCommand::class, DiffComponentCommand::class, ExportComponentCommand::class, - ImportComponentCommand::class + ExportStoryCommand::class, + ImportComponentCommand::class, + ImportStoryCommand::class, ]); }