owenvoke / blade-fontawesome

A package to easily make use of Font Awesome in your Laravel Blade views.
https://packagist.org/packages/owenvoke/blade-fontawesome
MIT License
162 stars 27 forks source link

Font Awesome Pro copy icons from custom kits #79

Open noquierouser opened 2 months ago

noquierouser commented 2 months ago

Description

I followed the procedure to copy Pro icons, but it seems the copying process doesn't support custom kits (subsets and custom icons) made on FontAwesome's website.

As far as I can tell, the sync process copies icons from node_modules/@fortawesome/fontawesome-* directories, but custom kits are installed in node_modules/@awesome.me/kit-* directory.

Now, as I see copying from Pro is different from copying from Kit, I don't actually know if this is a Feature Request or a Bug, so I left it as is.

Possible implementation

Maybe a new command might be needed, like:

php artisan blade-fontawesome:sync-icons --kit

Now, I don't know if there is a use case where multiple kits are downloaded and if those kits might overlap in icon sets.

belzaaron commented 1 month ago

This should only require the pro option to optionally have a kit-id passed through to it, so after installing the kit via NPM - boom.

I know this is really just an override but, I haven't put together a PR for it.

I've registered this in my bootstrap/app.php,

<?php

namespace App\Console\Commands\FontAwesome;

use DirectoryIterator;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use OwenVoke\BladeFontAwesome\Actions\CompileSvgsAction;

class SyncBladeIconsCommand extends Command
{
    protected $signature = 'blade-fontawesome:sync-icons
                          {directory? : The root directory containing the npm Font Awesome fonts}
                          {--free : Use the fontawesome-free npm package}
                          {--pro= : Use the fontawesome-pro npm package, or specify a specific kit}';

    protected $description = 'Synchronise Font Awesome icons from npm';

    public function handle(): ?int
    {
        /** @phpstan-ignore-next-line */
        $baseDirectory = (string) ($this->argument('directory') ?? base_path());

        $proSourcePath = "{$baseDirectory}/node_modules/@fortawesome/fontawesome-pro/svgs";

        if (is_string($kitId = $this->option('pro'))) {
            $proSourcePath = "{$baseDirectory}/node_modules/@awesome.me/kit-{$kitId}/icons/svgs";
        }

        $freeSourcePath = "{$baseDirectory}/node_modules/@fortawesome/fontawesome-free/svgs";

        if ($this->option('pro')) {
            $fullSourcePath = $proSourcePath;
        } elseif ($this->option('free')) {
            $fullSourcePath = $freeSourcePath;
        } else {
            $fullSourcePath = collect([
                $proSourcePath,
                $freeSourcePath,
            ])->filter(fn (string $path): bool => is_dir($path))->first() ?? $proSourcePath;
        }

        if (! is_dir($fullSourcePath)) {
            $this->warn("Unable to find Font Awesome SVGs in '{$baseDirectory}'");

            return static::FAILURE;
        }

        $destinationPath = resource_path('icons/blade-fontawesome');

        if (! File::copyDirectory($fullSourcePath, $destinationPath)) {
            $this->warn("Unable to find Font Awesome SVGs in '{$baseDirectory}'");

            return static::FAILURE;
        }

        $sets = [];

        foreach (new DirectoryIterator($destinationPath) as $directory) {
            if ($directory->isDot() || ! $directory->isDir()) {
                continue;
            }

            (new CompileSvgsAction($directory->getPathname(), $directory->getPathname()))->execute();

            $sets[] = $directory->getBasename();
        }

        $this->info('Successfully updated Font Awesome SVGs');
        $this->line("- From: {$fullSourcePath}");
        $this->line("- To: {$destinationPath}");

        $this->line("\nSets copied: ".implode(', ', $sets));

        return static::SUCCESS;
    }
}
owenvoke commented 1 month ago

I think I'd prefer this as an additional --kit=<id> flag. That way it's more obvious. Feel free to PR this if you'd like to.