Skip to content

Commit

Permalink
Merge pull request #14 from gwleuverink/feature/install-command
Browse files Browse the repository at this point in the history
Feature/install command
  • Loading branch information
gwleuverink committed May 29, 2024
2 parents d7aceba + e7a9f3e commit bfb4b5b
Show file tree
Hide file tree
Showing 15 changed files with 356 additions and 55 deletions.
14 changes: 8 additions & 6 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: ''
assignees: ''

title: ""
labels: ""
assignees: ""
---

Please provide the following info. You may run `artisan bundle:version` to dump all relevant package versions.

- Bundle: #.#.#
- Bun: #.#.#
- Laravel: #.#.#
- PHP: #.#.#
- Operating System: e.g. macOS 14.2.1
- Operating System: e.g. macOS 14.2.1

**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
Steps to reproduce the behavior:

1. Run `npm install ...`
2. Add import component to template '....'
2. Add import component to template '....'
3. Use the import in the following code '...'
4. See error

Expand Down
1 change: 1 addition & 0 deletions .github/workflows/browser-tests.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: browser-tests

on:
workflow_dispatch:
pull_request:
branches: [development, main]
workflow_run:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: tests

on:
workflow_dispatch:
pull_request:
branches: [development, main]
workflow_run:
Expand Down
8 changes: 8 additions & 0 deletions docs/advanced-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ You may configure what paths are scanned by publishing the Bundle config file an

`artisan bundle:clear` Clear all bundled scripts.

### bundle:version

`artisan bundle:version` Dumps Bundle's version & it's dependencies (Bun & optionally LightningCSS & Sass).

### bundle:install

`artisan bundle:install` Prompts you through installing Bun & optional CSS loader dependencies.

## Testing fake

When writing Unit or Feature tests in your application you don't need Bundle to process & serve your imports.
Expand Down
8 changes: 6 additions & 2 deletions docs/css-loading.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ We provide a custom CSS loader plugin that just works™. Built on top of [Light
You'll need to install Lightning CSS as a dependency.

```bash
npm install lightningcss --save-dev
php artisan bundle:install
```

You will be prompted to select a CSS loading method. Choose `CSS`.

Afterwards you may use `x-import` to load css files directly. Bundle transpiles it and injects it on your page with zero effort.

```html
Expand Down Expand Up @@ -44,9 +46,11 @@ npm install lightningcss --save-dev
You can use Bundle to compile [Sass](https://sass-lang.com/) on the fly. You'll need to install both Sass & Lightning CSS in your project. Bundle takes care of the rest.

```bash
npm install sass lightningcss --save-dev
php artisan bundle:install
```

You will be prompted to select a CSS loading method. Choose `Sass`.

{: .note }

> Due to a unresolved issue Bun is not able to auto-install LightningCSS & Sass on the fly. When this issue is fixed you won't have to install these dependencies yourself. Bun will automatically install them when needed 💅
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ composer require leuverink/bundle
```

```bash
npm install bun --save-dev
php artisan bundle:install
```

This is all you need to start using Bundle!
Expand Down
118 changes: 118 additions & 0 deletions src/Commands/Install.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace Leuverink\Bundle\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Process;

use function Laravel\Prompts\note;
use function Laravel\Prompts\intro;
use function Laravel\Prompts\outro;
use function Laravel\Prompts\select;
use function Laravel\Prompts\confirm;
use function Laravel\Prompts\warning;
use function Laravel\Prompts\progress;

class Install extends Command
{
protected $signature = 'bundle:install';
protected $description = 'Installs Bun, LightningCSS & Sass';

protected $installCommands = [];

public function handle(): int
{
$this->callSilent('bundle:clear');

$this->printAscii();
$this->printIntro();

$this->promptToInstallBun();
$this->promptToInstallCssLoading();

$this->install();

$this->call('bundle:version');

$this->printOutro();

return static::SUCCESS;
}

protected function printAscii()
{
note(<<< TEXT
____ __ __ _ __ ____ __ ______
/ __ ) / / / // | / // __ \ / / / ____/
/ __ |/ / / // |/ // / / // / / __/
/ /_/ // /_/ // /| // /_/ // /___ / /___
/_____/ \____//_/ |_//_____//_____//_____/
TEXT);
}

protected function printIntro()
{
intro(PHP_EOL . ' Thank you for installing Bundle! This wizard will set everything up for you. ' . PHP_EOL);
}

protected function promptToInstallBun()
{
$confirmed = confirm(
default: true,
label: 'Do you want to install Bun?',
hint: 'Bun needs to be installed in order to use Bundle. Skip this if you\'ve installed it manually.'
);

if ($confirmed) {
$this->installCommands[] = 'npm install bun@^1 --save-dev';
}
}

protected function promptToInstallCssLoading()
{
$choice = select(
label: 'Would you like to use CSS loading?',
options: [
'css' => 'CSS (installs LightningCSS)',
'sass' => 'Sass (installs Sass & LightningCSS)',
'none' => 'None',
],
default: 'css',
);

match ($choice) {
'css' => $this->installCommands[] = 'npm install lightningcss --save-dev',
'sass' => $this->installCommands = array_merge($this->installCommands, [
'npm install lightningcss --save-dev',
'npm install sass --save-dev',
]),
default => null,
};
}

protected function install()
{
if (empty($this->installCommands)) {
warning('Nothing to install.');

return;
}

progress(
label: 'Installing dependencies.',
steps: $this->installCommands,
callback: function ($command, $progress) {
$progress->hint($command);

Process::run($command);
},
hint: 'This may take some time.',
);
}

protected function printOutro()
{
outro(PHP_EOL . " You're all set! Check out https://laravel-bundle.dev/ to get started! " . PHP_EOL);
}
}
43 changes: 43 additions & 0 deletions src/Commands/Version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Leuverink\Bundle\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Process;

class Version extends Command
{
protected $signature = 'bundle:version';
protected $description = 'Lists Bundle and it\'s dependencies versions.';

public function handle(): int
{
$this->components->twoColumnDetail(' <fg=green;options=bold>Back-end</>');
$this->components->twoColumnDetail('Bundle', $this->composerPackageVersion('leuverink/bundle'));
$this->components->twoColumnDetail('Laravel', phpversion());
$this->components->twoColumnDetail('PHP', $this->laravel->version());

$this->newLine();
$this->components->twoColumnDetail(' <fg=green;options=bold>Front-end</>');
$this->components->twoColumnDetail('Bun', $this->npmPackageVersion('bun'));
$this->components->twoColumnDetail('LightningCSS', $this->npmPackageVersion('lightningcss'));
$this->components->twoColumnDetail('Sass', $this->npmPackageVersion('sass'));

return static::SUCCESS;
}

protected function composerPackageVersion(string $name): string
{
$packageInfo = json_decode(Process::run("composer show {$name} --format=json")->output());
$versions = data_get($packageInfo, 'versions', ['Not installed']);

return head($versions);
}

protected function npmPackageVersion(string $name): string
{
$packageInfo = json_decode(Process::run("npm list {$name} --json")->output());

return data_get($packageInfo, "dependencies.{$name}.version", 'Not installed');
}
}
4 changes: 4 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Route;
use Leuverink\Bundle\Bundlers\Bun\Bun;
use Leuverink\Bundle\Commands\Install;
use Leuverink\Bundle\Commands\Version;
use Leuverink\Bundle\Components\Import;
use Illuminate\Foundation\Http\Events\RequestHandled;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
Expand Down Expand Up @@ -64,6 +66,8 @@ protected function injectCore()

protected function registerCommands()
{
$this->commands(Install::class);
$this->commands(Version::class);
$this->commands(Build::class);
$this->commands(Clear::class);
}
Expand Down
82 changes: 82 additions & 0 deletions tests/Feature/Commands/InstallCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

use Illuminate\Process\PendingProcess;
use Illuminate\Support\Facades\Process;

beforeEach(function () {
Process::fake();
});

it('follows the happy path')
->artisan('bundle:install')
->expectsQuestion('Do you want to install Bun?', true)
->expectsQuestion('Would you like to use CSS loading?', 'none')
->assertSuccessful();

it('installs Bun when selected', function () {
$this->artisan('bundle:install')
->expectsQuestion('Do you want to install Bun?', true)
->expectsQuestion('Would you like to use CSS loading?', 'none')
->assertSuccessful();

Process::assertRan('npm install bun@^1 --save-dev');
});

it('doesnt install Bun when not selected', function () {
$this->artisan('bundle:install')
->expectsQuestion('Do you want to install Bun?', false)
->expectsQuestion('Would you like to use CSS loading?', 'none')
->assertSuccessful();

Process::assertDidntRun('npm install bun@^1 --save-dev');
});

it('installs LightningCSS when selected', function () {
$this->artisan('bundle:install')
->expectsQuestion('Do you want to install Bun?', false)
->expectsQuestion('Would you like to use CSS loading?', 'css')
->assertSuccessful();

Process::assertRan('npm install lightningcss --save-dev');
});

it('doesnt install LightningCSS when not selected', function () {
$this->artisan('bundle:install')
->expectsQuestion('Do you want to install Bun?', false)
->expectsQuestion('Would you like to use CSS loading?', 'none')
->assertSuccessful();

Process::assertDidntRun('npm install lightningcss --save-dev');
});

it('installs Sass when selected', function () {
$this->artisan('bundle:install')
->expectsQuestion('Do you want to install Bun?', false)
->expectsQuestion('Would you like to use CSS loading?', 'sass')
->assertSuccessful();

Process::assertRan('npm install lightningcss --save-dev');
Process::assertRan('npm install sass --save-dev');
});

it('doesnt install Sass when not selected', function () {
$this->artisan('bundle:install')
->expectsQuestion('Do you want to install Bun?', false)
->expectsQuestion('Would you like to use CSS loading?', 'none')
->assertSuccessful();

Process::assertDidntRun('npm install lightningcss --save-dev');
Process::assertDidntRun('npm install sass --save-dev');
});

it('doesnt install anything when nothing selected', function () {
$this->artisan('bundle:install')
->expectsQuestion('Do you want to install Bun?', false)
->expectsQuestion('Would you like to use CSS loading?', 'none')
->assertSuccessful();

// Assert for absence of npm install commands
Process::assertDidntRun(function (PendingProcess $process) {
return str($process->command)->startsWith('npm install');
});
});
11 changes: 11 additions & 0 deletions tests/Feature/Commands/VersionCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

it('runs without crashing')
->artisan('bundle:version')
->expectsOutputToContain('Bundle')
->expectsOutputToContain('Laravel')
->expectsOutputToContain('PHP')
->expectsOutputToContain('Bun')
->expectsOutputToContain('LightningCSS')
->expectsOutputToContain('Sass')
->assertSuccessful();
1 change: 0 additions & 1 deletion workbench/node_modules/.bin/bun

This file was deleted.

1 change: 0 additions & 1 deletion workbench/node_modules/.bin/bunx

This file was deleted.

Loading

0 comments on commit bfb4b5b

Please sign in to comment.