-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
5 changed files
with
215 additions
and
2 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,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 | ||
); | ||
} | ||
|
||
} |
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,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')); | ||
} | ||
} | ||
} |
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,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')); | ||
} | ||
} | ||
} |
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