Skip to content

Commit

Permalink
Merge branch 'release/1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
RicLeP committed Oct 18, 2022
2 parents 67e2357 + 986c9ad commit 1497cb4
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 2 deletions.
75 changes: 75 additions & 0 deletions src/Console/ComponentListCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Riclep\StoryblokCli\Console;

use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Riclep\StoryblokCli\Traits\GetsComponents;
use Storyblok\ManagementClient;

class ComponentListCommand extends Command
{
use GetsComponents;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'ls:component-list
{--additional-fields= : Additional fields to pull form Storyblok Management API}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'List all Storyblok components for the space.';


/**
* @var ManagementClient
*/
protected ManagementClient $managementClient;


public function __construct()
{
parent::__construct();

$this->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'] ? "<fg=green>true</>" : '<fg=red>false</>',
'has_template' => $c['preview_tmpl'] ? "<fg=green>true</>" : '<fg=red>false</>',
];

$mappedAdditional = collect($c)->only($additionalFields);

return array_merge($mapped, $mappedAdditional->toArray());
});

$this->table(
array_keys($rows->first()),
$rows
);
}

}
2 changes: 1 addition & 1 deletion src/Console/ExportComponentCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
63 changes: 63 additions & 0 deletions src/Console/ExportStoryCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Riclep\StoryblokCli\Console;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Storyblok\ManagementClient;

class ExportStoryCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'ls:export-story {slug}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Save a story as JSON';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
$this->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'));
}
}
}
69 changes: 69 additions & 0 deletions src/Console/ImportStoryCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Riclep\StoryblokCli\Console;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use Storyblok\ManagementClient;

class ImportStoryCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'ls:import-story {filename} {slug}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Import a story from JSON - it will be created in your space’s root';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
$this->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'));
}
}
}
8 changes: 7 additions & 1 deletion src/StoryblokCliServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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,
]);
}

Expand Down

0 comments on commit 1497cb4

Please sign in to comment.