Skip to content

Commit 9167e95

Browse files
authored
fix: improve theme selection and validation in TokensCommand (#77)
1 parent d814853 commit 9167e95

File tree

1 file changed

+96
-53
lines changed

1 file changed

+96
-53
lines changed

src/Console/Command/Theme/TokensCommand.php

Lines changed: 96 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -61,29 +61,59 @@ protected function configure(): void
6161
*/
6262
protected function executeCommand(InputInterface $input, OutputInterface $output): int
6363
{
64-
$themeCode = $input->getArgument('themeCode');
64+
$themeCode = $this->selectTheme($input->getArgument('themeCode'));
65+
if ($themeCode === null) {
66+
return Cli::RETURN_FAILURE;
67+
}
68+
69+
$themePath = $this->validateHyvaTheme($themeCode);
70+
if ($themePath === null) {
71+
return Cli::RETURN_FAILURE;
72+
}
73+
74+
$tailwindPath = $this->validateTailwindDirectory($themePath, $themeCode);
75+
if ($tailwindPath === null) {
76+
return Cli::RETURN_FAILURE;
77+
}
78+
6579
$isVerbose = $this->isVerbose($output);
80+
if (!$this->generateTokens($tailwindPath, $themeCode, $isVerbose)) {
81+
return Cli::RETURN_FAILURE;
82+
}
6683

67-
if (empty($themeCode)) {
68-
$themes = $this->themeList->getAllThemes();
69-
$options = array_map(fn($theme) => $theme->getCode(), $themes);
84+
$this->handleOutputFile($tailwindPath, $themePath, $themeCode);
7085

71-
$themeCodePrompt = new SelectPrompt(
72-
label: 'Select theme to generate tokens for',
73-
options: $options,
74-
scroll: 10,
75-
hint: 'Arrow keys to navigate, Enter to confirm',
76-
);
86+
return Cli::RETURN_SUCCESS;
87+
}
7788

78-
try {
79-
$themeCode = $themeCodePrompt->prompt();
80-
\Laravel\Prompts\Prompt::terminal()->restoreTty();
81-
} catch (\Exception $e) {
82-
$this->io->error('Interactive mode failed: ' . $e->getMessage());
83-
return Cli::RETURN_FAILURE;
84-
}
89+
private function selectTheme(?string $themeCode): ?string
90+
{
91+
if (!empty($themeCode)) {
92+
return $themeCode;
8593
}
8694

95+
$themes = $this->themeList->getAllThemes();
96+
$options = array_map(fn($theme) => $theme->getCode(), $themes);
97+
98+
$themeCodePrompt = new SelectPrompt(
99+
label: 'Select theme to generate tokens for',
100+
options: $options,
101+
scroll: 10,
102+
hint: 'Arrow keys to navigate, Enter to confirm',
103+
);
104+
105+
try {
106+
$themeCode = $themeCodePrompt->prompt();
107+
\Laravel\Prompts\Prompt::terminal()->restoreTty();
108+
return $themeCode;
109+
} catch (\Exception $e) {
110+
$this->io->error('Interactive mode failed: ' . $e->getMessage());
111+
return null;
112+
}
113+
}
114+
115+
private function validateHyvaTheme(string $themeCode): ?string
116+
{
87117
$themePath = $this->themePath->getPath($themeCode);
88118
if ($themePath === null) {
89119
// Try to suggest similar themes
@@ -111,31 +141,39 @@ protected function executeCommand(InputInterface $input, OutputInterface $output
111141
$this->io->info("Using theme: $themeCode");
112142
}
113143

114-
// Check if this is a Hyvä theme
115144
$builder = $this->builderPool->getBuilder($themePath);
116145
if ($builder === null || $builder->getName() !== 'HyvaThemes') {
117146
$this->io->error("Theme $themeCode is not a Hyvä theme. This command only works with Hyvä themes.");
118-
return Cli::RETURN_FAILURE;
147+
return null;
119148
}
120149

150+
return $themePath;
151+
}
152+
153+
private function validateTailwindDirectory(string $themePath, string $themeCode): ?string
154+
{
121155
$tailwindPath = rtrim($themePath, '/') . '/web/tailwind';
156+
122157
if (!$this->fileDriver->isDirectory($tailwindPath)) {
123158
$this->io->error("Tailwind directory not found in: $tailwindPath");
124-
return Cli::RETURN_FAILURE;
159+
return null;
125160
}
126161

127-
// Check if node_modules exists
128162
if (!$this->fileDriver->isDirectory($tailwindPath . '/node_modules')) {
129163
$this->io->warning('Node modules not found. Please run: bin/magento mageforge:theme:build ' . $themeCode);
130-
return Cli::RETURN_FAILURE;
164+
return null;
131165
}
132166

167+
return $tailwindPath;
168+
}
169+
170+
private function generateTokens(string $tailwindPath, string $themeCode, bool $isVerbose): bool
171+
{
133172
if ($isVerbose) {
134173
$this->io->section("Generating Hyvä design tokens for theme: $themeCode");
135174
$this->io->text("Working directory: $tailwindPath");
136175
}
137176

138-
// Change to tailwind directory and run npx hyva-tokens
139177
$currentDir = getcwd();
140178
chdir($tailwindPath);
141179

@@ -145,44 +183,49 @@ protected function executeCommand(InputInterface $input, OutputInterface $output
145183
}
146184

147185
$this->shell->execute('npx hyva-tokens');
148-
149186
chdir($currentDir);
150187

151-
// Determine output path based on theme location
152-
$isVendorTheme = str_contains($themePath, '/vendor/');
153-
$sourceFilePath = $tailwindPath . '/generated/hyva-tokens.css';
154-
155-
if ($isVendorTheme) {
156-
// Store in var/generated/hyva-token/{ThemeCode}/ for vendor themes
157-
$varGeneratedPath = $currentDir . '/var/generated/hyva-token/' . str_replace('/', '/', $themeCode);
188+
return true;
189+
} catch (\Exception $e) {
190+
chdir($currentDir);
191+
$this->io->error('Failed to generate Hyvä design tokens: ' . $e->getMessage());
192+
return false;
193+
}
194+
}
158195

159-
if (!$this->fileDriver->isDirectory($varGeneratedPath)) {
160-
$this->fileDriver->createDirectory($varGeneratedPath, 0755);
161-
}
196+
private function handleOutputFile(string $tailwindPath, string $themePath, string $themeCode): void
197+
{
198+
$isVendorTheme = str_contains($themePath, '/vendor/');
199+
$sourceFilePath = $tailwindPath . '/generated/hyva-tokens.css';
200+
201+
if ($isVendorTheme) {
202+
$generatedFilePath = $this->copyToVarGenerated($sourceFilePath, $themeCode);
203+
$this->io->success('Hyvä design tokens generated successfully.');
204+
$this->io->note('This is a vendor theme. Tokens have been saved to var/generated/hyva-token/ instead.');
205+
$this->io->text('Generated file: ' . $generatedFilePath);
206+
} else {
207+
$this->io->success('Hyvä design tokens generated successfully.');
208+
$this->io->text('Generated file: ' . $sourceFilePath);
209+
}
162210

163-
$generatedFilePath = $varGeneratedPath . '/hyva-tokens.css';
211+
$this->io->newLine();
212+
}
164213

165-
// Copy file to var/generated location
166-
if ($this->fileDriver->isExists($sourceFilePath)) {
167-
$this->fileDriver->copy($sourceFilePath, $generatedFilePath);
168-
}
214+
private function copyToVarGenerated(string $sourceFilePath, string $themeCode): string
215+
{
216+
$currentDir = getcwd();
217+
$varGeneratedPath = $currentDir . '/var/generated/hyva-token/' . str_replace('/', '/', $themeCode);
169218

170-
$this->io->success('Hyvä design tokens generated successfully.');
171-
$this->io->note('This is a vendor theme. Tokens have been saved to var/generated/hyva-token/ instead.');
172-
$this->io->text('Generated file: ' . $generatedFilePath);
173-
} else {
174-
$generatedFilePath = $sourceFilePath;
175-
$this->io->success('Hyvä design tokens generated successfully.');
176-
$this->io->text('Generated file: ' . $generatedFilePath);
177-
}
219+
if (!$this->fileDriver->isDirectory($varGeneratedPath)) {
220+
$this->fileDriver->createDirectory($varGeneratedPath, 0755);
221+
}
178222

179-
$this->io->newLine();
223+
$generatedFilePath = $varGeneratedPath . '/hyva-tokens.css';
180224

181-
return Cli::RETURN_SUCCESS;
182-
} catch (\Exception $e) {
183-
chdir($currentDir);
184-
$this->io->error('Failed to generate Hyvä design tokens: ' . $e->getMessage());
185-
return Cli::RETURN_FAILURE;
225+
if ($this->fileDriver->isExists($sourceFilePath)) {
226+
$this->fileDriver->copy($sourceFilePath, $generatedFilePath);
186227
}
228+
229+
return $generatedFilePath;
187230
}
188231
}

0 commit comments

Comments
 (0)