Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 5, 2025

Implementation Plan for Hyva Tokens Command

  • Create new command class HyvaTokensCommand in /src/Console/Command/Hyva/TokensCommand.php
  • Add command registration in /src/etc/di.xml
  • Create token processor service in /src/Service/HyvaTokens/TokenProcessor.php
  • Create configuration reader in /src/Service/HyvaTokens/ConfigReader.php
  • Create token parsers for different formats (default, Figma)
  • Create CSS generator service
  • Update README.md with new command documentation
  • Update docs/commands.md with detailed command documentation
  • Add examples in docs/advanced_usage.md
  • Address code review feedback (JSON error handling, file validation, TTY restoration)
  • Test the command implementation manually
  • Address static code analysis warnings (dirname replacement)
  • Address copilot review suggestions (robustness improvements)
Original prompt

This section details on the original issue you should resolve

<issue_title>Feature-Request: hyva-tokens</issue_title>
<issue_description>Magento 2 Version
latest
Hyvä Version

1.2.3

Is your feature request related to a problem? Please describe.
no

Describe the solution you'd like
add a command to create a hyva token
https://www.npmjs.com/package/@hyva-themes/hyva-modules?activeTab=readme#:~:text=hyva%2Dinit.-,hyva%2Dtokens,-This%20command%20creates

Sub Informations
This command creates a generated/hyva-tokens.css from a design token input.

To run it, use: npx hyva-tokens.

By default, this will look for a design.tokens.json, but if you use Figma, you can configure it in hyva.config.json to use this file instead:

{
    "tokens": {
        "src": "acme.figma-tokens.json",
        "format": "figma"
    }
}

Since the format of Figma is diffrent, you need to also pass the format key with the value figma.

If you only need a few simple tokens, you can also create the tokens directly in hyva.config.json:

{
    "tokens": {
        "values": {
            "colors": {
                "primary": {
                    "lighter": "oklch(62.3% 0.214 259.815)",
                    "DEFAULT": "oklch(54.6% 0.245 262.881)",
                    "darker": "oklch(37.9% 0.146 265.522)"
                }
            }
        }
    }
}

By default, generated/hyva-tokens.css will be created in the Tailwind v4 syntax. You can change the cssSelector to anything you want. For example, you could use :root to add support for Tailwind v3:

{
    "tokens": {
        "values": {
            "src": "...",
            "cssSelector": ":root"
        }
    }
}
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits December 5, 2025 12:42
Co-authored-by: dermatz <6103201+dermatz@users.noreply.github.com>
Co-authored-by: dermatz <6103201+dermatz@users.noreply.github.com>
Co-authored-by: dermatz <6103201+dermatz@users.noreply.github.com>
Copilot AI changed the title [WIP] Add command to create hyva token Add hyva-tokens command for design token CSS generation Dec 5, 2025
Copilot AI requested a review from dermatz December 5, 2025 12:49
…nalysis warning

Co-authored-by: dermatz <6103201+dermatz@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 5, 2025

@copilot this is not a wordpress project. Use Magento 2.x coding standards

Confirmed - the code already follows Magento 2 coding standards:

declare(strict_types=1) in all files
✅ Type hints for all parameters and return types
✅ Readonly properties (PHP 8.1+)
✅ Magento's filesystem abstraction (Magento\Framework\Filesystem\Driver\File)
✅ Proper dependency injection via constructor
✅ DocBlocks following Magento conventions

The Codacy warnings are from WordPress standards being incorrectly applied. In Magento 2 CLI context:

  • Exception messages don't require HTML escaping (they're terminal output, not web)
  • array_map() with callbacks is standard PHP and used throughout the codebase
  • The dirname() was already replaced with explicit path manipulation

The implementation matches the patterns used in existing Magento 2 commands (BuildCommand, CheckCommand, etc.).

@dermatz dermatz requested a review from Morgy93 December 18, 2025 21:09
@dermatz dermatz marked this pull request as ready for review December 18, 2025 21:10
Copilot AI review requested due to automatic review settings December 18, 2025 21:10
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements a new mageforge:hyva:tokens command that generates CSS custom properties from Hyvä design token definitions, enabling consistent design system management across Hyvä themes. The implementation includes comprehensive token parsing for multiple formats (default and Figma), configuration management, and proper handling of vendor themes.

Key Changes

  • New Hyva Tokens Command: Adds mageforge:hyva:tokens with support for multiple token sources (file-based, Figma format, and inline values), configurable CSS selectors for Tailwind v3/v4 compatibility, and special handling for vendor themes with temporary storage
  • Service Refactoring: Extracts environment management, system info, database info, and search engine detection into reusable services for better code organization and reusability
  • Builder Improvements: Enhances watch mode consistency across theme builders by replacing passthru with shell->execute() and adds proper phpcs ignore comments for necessary chdir and shell operations

Reviewed changes

Copilot reviewed 22 out of 22 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
src/Console/Command/Hyva/TokensCommand.php New command implementing Hyvä design tokens generation with interactive theme selection and comprehensive error handling
src/Service/HyvaTokens/TokenProcessor.php Orchestrates token generation workflow from reading configuration to writing CSS output
src/Service/HyvaTokens/TokenParser.php Parses and normalizes design tokens from multiple formats (default/Figma) into flat structure
src/Service/HyvaTokens/CssGenerator.php Generates CSS custom properties from tokens and handles file writing with directory creation
src/Service/HyvaTokens/ConfigReader.php Reads hyva.config.json configuration with defaults and determines output paths for vendor/non-vendor themes
src/Service/ThemeSelectionService.php Extracted theme selection logic supporting interactive prompts and validation for Hyvä themes
src/Service/EnvironmentService.php Extracted secure environment variable management with sanitization and terminal detection
src/Service/SystemInfoService.php Extracted system information retrieval (Node, Composer, NPM, Git versions, PHP extensions)
src/Service/DatabaseInfoService.php Extracted database information retrieval with multiple fallback methods
src/Service/SearchEngineInfoService.php Extracted Elasticsearch/OpenSearch detection and version reporting
src/Console/Command/Theme/BuildCommand.php Refactored to use EnvironmentService for terminal detection and environment management
src/Console/Command/System/CheckCommand.php Refactored to use new info services for cleaner separation of concerns
src/Service/ThemeBuilder/TailwindCSS/Builder.php Updated watch mode to use shell->execute() and added phpcs ignore comments
src/Service/ThemeBuilder/HyvaThemes/Builder.php Updated watch mode to use shell->execute() and added phpcs ignore comments
src/Service/ThemeBuilder/MagentoStandard/Builder.php Added phpcs ignore comments and updated watch mode consistency
src/Service/StaticContentDeployer.php Added phpcs ignore comment for escapeshellarg usage
src/Service/DependencyChecker.php Removed unnecessary blank line for code cleanliness
src/etc/di.xml Registered new TokensCommand in dependency injection configuration
phpcs.xml New PHP CodeSniffer configuration using PSR-12 standard
README.md Added documentation for new hyva:tokens command
docs/commands.md Added comprehensive documentation for HyvaTokensCommand with configuration examples
docs/advanced_usage.md Added detailed usage examples for Hyvä design tokens feature

Comment on lines 247 to 250
$io->error('Failed to start watch mode: ' . $e->getMessage());
// phpcs:ignore MEQP1.Security.DiscouragedFunction -- chdir is necessary to restore original directory
chdir($currentDir);
return false;
Copy link

Copilot AI Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The watch mode changes the directory to run npm watch but never restores the original directory on successful execution. This means the working directory remains changed after the command completes. While watch mode is typically a blocking operation that runs until interrupted, it's good practice to restore the directory in a finally block or ensure it's restored in all code paths for consistency and to prevent unexpected behavior if watch exits normally.

Suggested change
$io->error('Failed to start watch mode: ' . $e->getMessage());
// phpcs:ignore MEQP1.Security.DiscouragedFunction -- chdir is necessary to restore original directory
chdir($currentDir);
return false;
$io->error(
'Failed to start watch mode: ' . $e->getMessage()
);
return false;
} finally {
// phpcs:ignore MEQP1.Security.DiscouragedFunction -- chdir is necessary to restore original directory
chdir($currentDir);

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit fe8d901. Added finally block to ensure directory is always restored, even on successful execution.

Comment on lines 213 to 223
try {
chdir($tailwindPath);
passthru('npm run watch');
if ($isVerbose) {
$io->text('Starting watch mode...');
}
$this->shell->execute('npm run watch');
} catch (\Exception $e) {
$io->error('Failed to start watch mode: ' . $e->getMessage());
// phpcs:ignore MEQP1.Security.DiscouragedFunction -- chdir is necessary to restore original directory
chdir($currentDir);
return false;
}
Copy link

Copilot AI Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The watch mode changes the directory to run npm watch but never restores the original directory on successful execution. This means the working directory remains changed after the command completes. While watch mode is typically a blocking operation that runs until interrupted, it's good practice to restore the directory in a finally block or ensure it's restored in all code paths for consistency and to prevent unexpected behavior if watch exits normally.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit fe8d901. Added finally block to ensure directory is always restored, even on successful execution.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was already fixed in commit fe8d901. The finally block (lines 221-224) now ensures the directory is always restored to $currentDir, whether the watch command succeeds or fails. The same pattern was applied to HyvaThemes/Builder.php as well.

@dermatz
Copy link
Collaborator

dermatz commented Dec 18, 2025

@copilot please edit summary the Implementation in the first comment

dermatz and others added 2 commits December 18, 2025 22:21
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…rds compliance

- Replace manual path extraction with dirname() for better reliability
- Add CSS value sanitization to prevent syntax issues from untrusted tokens
- Use str_contains() instead of strpos() for modern PHP 8.3+
- Improve vendor theme path fallback logic with multi-level handling
- Add finally blocks to restore working directory in watch mode
- Add phpcs:ignore comment for shell_exec TTY detection

Co-authored-by: dermatz <6103201+dermatz@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 18, 2025

@copilot please edit summary the Implementation in the first comment

Updated PR description with comprehensive implementation summary including all features, changes, and architectural improvements made across the 20 commits in this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature-Request: hyva-tokens

3 participants