Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tailwind theme create improvements #1255

Merged
merged 3 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 56 additions & 26 deletions modules/cms/console/CreateTheme.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php namespace Cms\Console;

use InvalidArgumentException;
use System\Classes\Asset\PackageManager;
use Winter\Storm\Exception\SystemException;
use Winter\Storm\Scaffold\GeneratorCommand;

class CreateTheme extends GeneratorCommand
Expand Down Expand Up @@ -59,10 +61,6 @@ class CreateTheme extends GeneratorCommand
'scaffold/theme/less/version.stub' => 'version.yaml',
],
'tailwind' => [
'scaffold/theme/tailwind/assets/src/css/base.stub' => 'assets/src/css/base.css',
'scaffold/theme/tailwind/assets/src/css/custom.stub' => 'assets/src/css/custom.css',
'scaffold/theme/tailwind/assets/src/css/theme.stub' => 'assets/src/css/theme.css',
'scaffold/theme/tailwind/assets/src/js/theme.stub' => 'assets/src/js/theme.js',
'scaffold/theme/tailwind/lang/en/lang.stub' => 'lang/en/lang.php',
'scaffold/theme/tailwind/layouts/default.stub' => 'layouts/default.htm',
'scaffold/theme/tailwind/pages/404.stub' => 'pages/404.htm',
Expand Down Expand Up @@ -155,33 +153,65 @@ public function makeStubs(): void
parent::makeStubs();

if ($this->scaffold === 'tailwind') {
// Set up the vite config files
$this->call('vite:create', [
'packageName' => 'theme-' . $this->getNameInput(),
'--no-interaction' => true,
'--force' => true,
'--silent' => true,
'--tailwind' => true
]);
// @TODO: allow support for mix here
$this->tailwindPostCreate('vite');
}
}

$this->info('Installing NPM dependencies...');
protected function tailwindPostCreate(string $processor): void
{
if ($this->call('npm:version', ['--silent' => true, '--compatible' => true]) !== 0) {
throw new SystemException(sprintf(
'NPM is not installed or is outdated, please ensure NPM >= v7.0 is available and then manually set up %s.',
$processor
));
}

$commands = [
// Set up the vite config files
$processor . ':create' => [
'message' => 'Generating ' . $processor . ' + tailwind config...',
'args' => [
'packageName' => 'theme-' . $this->getNameInput(),
'--no-interaction' => true,
'--force' => true,
'--silent' => true,
'--tailwind' => true
]
],
// Ensure all require packages are available for the new theme and add the new theme to our npm workspaces
$this->call('vite:install', [
'assetPackage' => ['theme-' . $this->getNameInput()],
'--no-interaction' => true,
'--silent' => true,
'--disable-tty' => true
]);
$processor . ':install' => [
'message' => 'Installing NPM dependencies...',
'args' => [
'assetPackage' => ['theme-' . $this->getNameInput()],
'--no-interaction' => true,
'--silent' => false,
'--disable-tty' => true
]
],
// Run an initial compile to ensure styles are available for first load
$processor . ':compile' => [
'message' => 'Compiling your theme...',
'args' => [
'--package' => ['theme-' . $this->getNameInput()],
'--no-interaction' => true,
'--silent' => true,
]
]
];

$this->info('Compiling your theme...');
foreach ($commands as $command => $data) {
$this->info($data['message']);

// Run an initial compile to ensure styles are available for first load
$this->call('vite:compile', [
'--package' => ['theme-' . $this->getNameInput()],
'--no-interaction' => true,
'--silent' => true,
]);
// Handle commands throwing errors
if ($this->call($command, $data['args']) !== 0) {
throw new SystemException(sprintf('Post create command `%s` failed, please review manually.', $command));
}

// Force PackageManger to reset available packages
if ($command === $processor . ':create') {
PackageManager::forgetInstance();
}
}
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

1 change: 1 addition & 0 deletions modules/system/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ protected function registerConsole()
$this->registerConsoleCommand('npm.run', Console\Asset\Npm\NpmRun::class);
$this->registerConsoleCommand('npm.install', Console\Asset\Npm\NpmInstall::class);
$this->registerConsoleCommand('npm.update', Console\Asset\Npm\NpmUpdate::class);
$this->registerConsoleCommand('npm.version', Console\Asset\Npm\NpmVersion::class);
}

/*
Expand Down
74 changes: 74 additions & 0 deletions modules/system/console/asset/npm/NpmVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace System\Console\Asset\Npm;

use Symfony\Component\Process\Process;
use System\Console\Asset\Npm\NpmCommand;

class NpmVersion extends NpmCommand
{
const NPM_MINIMUM_SUPPORTED_VERSION = '7.0';

/**
* @var string|null The default command name for lazy loading.
*/
protected static $defaultName = 'npm:version';

/**
* @var string The name and signature of this command.
*/
protected $signature = 'npm:version
{--c|compatible : Report compatible version via exit code.}
{--s|silent : Silent mode.}
{--disable-tty : Disable tty mode}';

/**
* @var string The console command description.
*/
protected $description = 'Runs a script in a given package.';

/**
* Execute the console command.
*/
public function handle(): int
{
$process = new Process(
['npm', '--version'],
base_path(),
['NODE_ENV' => $this->getNodeEnv()],
null,
null
);

$output = '';

$exit = $process->run(function ($status, $stdout) use (&$output) {
$output .= $stdout;
});

$output = trim($output);

// Npm failed for some reason, report to user
if ($exit !== 0) {
$this->error('NPM exited with error: ' . $output);
return $exit;
}

// Report the version to user
if (!$this->option('silent')) {
$this->info($output);
}

// If the user has not requested a compatibility check, then return 0
if (!$this->option('compatible')) {
return 0;
}

// If the version of npm is less than the required minimum, then return fail
if (version_compare($output, static::NPM_MINIMUM_SUPPORTED_VERSION, '<')) {
return 1;
}

return 0;
}
}
Loading