Skip to content

Commit 1497cb4

Browse files
committed
Merge branch 'release/1.0.0'
2 parents 67e2357 + 986c9ad commit 1497cb4

5 files changed

+215
-2
lines changed

src/Console/ComponentListCommand.php

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Riclep\StoryblokCli\Console;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Str;
7+
use Riclep\StoryblokCli\Traits\GetsComponents;
8+
use Storyblok\ManagementClient;
9+
10+
class ComponentListCommand extends Command
11+
{
12+
use GetsComponents;
13+
14+
/**
15+
* The name and signature of the console command.
16+
*
17+
* @var string
18+
*/
19+
protected $signature = 'ls:component-list
20+
{--additional-fields= : Additional fields to pull form Storyblok Management API}';
21+
22+
/**
23+
* The console command description.
24+
*
25+
* @var string
26+
*/
27+
protected $description = 'List all Storyblok components for the space.';
28+
29+
30+
/**
31+
* @var ManagementClient
32+
*/
33+
protected ManagementClient $managementClient;
34+
35+
36+
public function __construct()
37+
{
38+
parent::__construct();
39+
40+
$this->managementClient = new ManagementClient(config('storyblok-cli.oauth_token'));
41+
}
42+
43+
/**
44+
* Execute the console command.
45+
*
46+
* @return void
47+
*/
48+
public function handle()
49+
{
50+
$this->requestComponents();
51+
52+
$additionalFields = $this->option('additional-fields') ?
53+
Str::of($this->option('additional-fields'))->explode(',')
54+
: collect();
55+
56+
$rows = $this->sbComponents->map(function ($c) use ($additionalFields) {
57+
$mapped = [
58+
'name' => $c['name'],
59+
'display_name' => $c['display_name'],
60+
'has_image' => $c['image'] ? "<fg=green>true</>" : '<fg=red>false</>',
61+
'has_template' => $c['preview_tmpl'] ? "<fg=green>true</>" : '<fg=red>false</>',
62+
];
63+
64+
$mappedAdditional = collect($c)->only($additionalFields);
65+
66+
return array_merge($mapped, $mappedAdditional->toArray());
67+
});
68+
69+
$this->table(
70+
array_keys($rows->first()),
71+
$rows
72+
);
73+
}
74+
75+
}

src/Console/ExportComponentCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ protected function exportComponent($componentName)
8787

8888
Storage::put($componentName . '.json', json_encode($component, JSON_THROW_ON_ERROR));
8989

90-
$this->info($componentName . '.json exported');
90+
$this->info('Saved to storage: ' . $componentName . '.json');
9191

9292
return $component;
9393
}

src/Console/ExportStoryCommand.php

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Riclep\StoryblokCli\Console;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\Storage;
7+
use Illuminate\Support\Str;
8+
use Storyblok\ManagementClient;
9+
10+
class ExportStoryCommand extends Command
11+
{
12+
/**
13+
* The name and signature of the console command.
14+
*
15+
* @var string
16+
*/
17+
protected $signature = 'ls:export-story {slug}';
18+
19+
/**
20+
* The console command description.
21+
*
22+
* @var string
23+
*/
24+
protected $description = 'Save a story as JSON';
25+
26+
/**
27+
* Create a new command instance.
28+
*
29+
* @return void
30+
*/
31+
public function __construct()
32+
{
33+
$this->client = new ManagementClient(config('storyblok-cli.oauth_token'));
34+
35+
parent::__construct();
36+
}
37+
38+
/**
39+
* Execute the console command.
40+
*
41+
* @return int
42+
*/
43+
public function handle()
44+
{
45+
$storyExists = $this->client->get('spaces/' . config('storyblok-cli.space_id') . '/stories/', [
46+
'with_slug' => $this->argument('slug')
47+
])->getBody()['stories'];
48+
49+
if ($storyExists) {
50+
$filename = 'storyblok-' . Str::of($this->argument('slug'))->replace('/', '-')->slug() . '.json';
51+
52+
$story = $this->client->get('spaces/' . config('storyblok-cli.space_id') . '/stories/' . $storyExists[0]['id'])->getBody();
53+
54+
$json = json_encode($story);
55+
56+
Storage::put($filename, $json);
57+
58+
$this->info('Saved to storage: ' . $filename);
59+
} else {
60+
$this->warn('There is no story for your slug: ' . $this->argument('slug'));
61+
}
62+
}
63+
}

src/Console/ImportStoryCommand.php

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Riclep\StoryblokCli\Console;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\Storage;
7+
use Storyblok\ManagementClient;
8+
9+
class ImportStoryCommand extends Command
10+
{
11+
/**
12+
* The name and signature of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $signature = 'ls:import-story {filename} {slug}';
17+
18+
/**
19+
* The console command description.
20+
*
21+
* @var string
22+
*/
23+
protected $description = 'Import a story from JSON - it will be created in your space’s root';
24+
25+
/**
26+
* Create a new command instance.
27+
*
28+
* @return void
29+
*/
30+
public function __construct()
31+
{
32+
$this->client = new ManagementClient(config('storyblok-cli.oauth_token'));
33+
34+
parent::__construct();
35+
}
36+
37+
/**
38+
* Execute the console command.
39+
*
40+
* @return int
41+
*/
42+
public function handle()
43+
{
44+
// TODO - interactive console for selecting save folder?
45+
46+
$storyExists = $this->client->get('spaces/' . config('storyblok-cli.space_id') . '/stories/', [
47+
'with_slug' => $this->argument('slug')
48+
])->getBody()['stories'];
49+
50+
if (!$storyExists) {
51+
$source = json_decode(Storage::get($this->argument('filename')), true);
52+
53+
$story = [
54+
"story" => [
55+
"name" => $source['story']['name'] . ' (Imported)',
56+
"slug" => $this->argument('slug'),
57+
"content" => $source['story']['content'],
58+
],
59+
"publish" => 1
60+
];
61+
62+
$importedStory = $this->client->post('spaces/' . config('storyblok-cli.space_id') . '/stories/', $story)->getBody()['story'];
63+
64+
$this->info('Imported into Storyblok: ' . $importedStory['name']);
65+
} else {
66+
$this->warn('Story already exists for: ' . $this->argument('slug'));
67+
}
68+
}
69+
}

src/StoryblokCliServiceProvider.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
namespace Riclep\StoryblokCli;
44

55
use Illuminate\Support\ServiceProvider;
6+
use Riclep\StoryblokCli\Console\ComponentListCommand;
67
use Riclep\StoryblokCli\Console\DiffComponentCommand;
78
use Riclep\StoryblokCli\Console\ExportComponentCommand;
9+
use Riclep\StoryblokCli\Console\ExportStoryCommand;
810
use Riclep\StoryblokCli\Console\ImportComponentCommand;
11+
use Riclep\StoryblokCli\Console\ImportStoryCommand;
912

1013
class StoryblokCliServiceProvider extends ServiceProvider
1114
{
@@ -15,9 +18,12 @@ class StoryblokCliServiceProvider extends ServiceProvider
1518
public function boot()
1619
{
1720
$this->commands([
21+
ComponentListCommand::class,
1822
DiffComponentCommand::class,
1923
ExportComponentCommand::class,
20-
ImportComponentCommand::class
24+
ExportStoryCommand::class,
25+
ImportComponentCommand::class,
26+
ImportStoryCommand::class,
2127
]);
2228
}
2329

0 commit comments

Comments
 (0)