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

[Feat] Add Support for Custom Package Versions in updateNodePackages Method #414

Closed
wants to merge 1 commit into from
Closed
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
57 changes: 30 additions & 27 deletions src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ protected function installTests()
return false;
}

(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/'.$stubStack.'/pest-tests/Feature', base_path('tests/Feature'));
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/'.$stubStack.'/pest-tests/Unit', base_path('tests/Unit'));
(new Filesystem)->copy(__DIR__.'/../../stubs/'.$stubStack.'/pest-tests/Pest.php', base_path('tests/Pest.php'));
(new Filesystem)->copyDirectory(__DIR__ . '/../../stubs/' . $stubStack . '/pest-tests/Feature', base_path('tests/Feature'));
(new Filesystem)->copyDirectory(__DIR__ . '/../../stubs/' . $stubStack . '/pest-tests/Unit', base_path('tests/Unit'));
(new Filesystem)->copy(__DIR__ . '/../../stubs/' . $stubStack . '/pest-tests/Pest.php', base_path('tests/Pest.php'));
} else {
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/'.$stubStack.'/tests/Feature', base_path('tests/Feature'));
(new Filesystem)->copyDirectory(__DIR__ . '/../../stubs/' . $stubStack . '/tests/Feature', base_path('tests/Feature'));
}

return true;
Expand All @@ -118,17 +118,17 @@ protected function installMiddleware($names, $group = 'web', $modifier = 'append
$bootstrapApp = file_get_contents(base_path('bootstrap/app.php'));

$names = collect(Arr::wrap($names))
->filter(fn ($name) => ! Str::contains($bootstrapApp, $name))
->filter(fn($name) => ! Str::contains($bootstrapApp, $name))
->whenNotEmpty(function ($names) use ($bootstrapApp, $group, $modifier) {
$names = $names->map(fn ($name) => "$name")->implode(','.PHP_EOL.' ');
$names = $names->map(fn($name) => "$name")->implode(',' . PHP_EOL . ' ');

$bootstrapApp = str_replace(
'->withMiddleware(function (Middleware $middleware) {',
'->withMiddleware(function (Middleware $middleware) {'
.PHP_EOL." \$middleware->$group($modifier: ["
.PHP_EOL." $names,"
.PHP_EOL.' ]);'
.PHP_EOL,
. PHP_EOL . " \$middleware->$group($modifier: ["
. PHP_EOL . " $names,"
. PHP_EOL . ' ]);'
. PHP_EOL,
$bootstrapApp,
);

Expand All @@ -147,17 +147,17 @@ protected function installMiddlewareAliases($aliases)
$bootstrapApp = file_get_contents(base_path('bootstrap/app.php'));

$aliases = collect($aliases)
->filter(fn ($alias) => ! Str::contains($bootstrapApp, $alias))
->filter(fn($alias) => ! Str::contains($bootstrapApp, $alias))
->whenNotEmpty(function ($aliases) use ($bootstrapApp) {
$aliases = $aliases->map(fn ($name, $alias) => "'$alias' => $name")->implode(','.PHP_EOL.' ');
$aliases = $aliases->map(fn($name, $alias) => "'$alias' => $name")->implode(',' . PHP_EOL . ' ');

$bootstrapApp = str_replace(
'->withMiddleware(function (Middleware $middleware) {',
'->withMiddleware(function (Middleware $middleware) {'
.PHP_EOL.' $middleware->alias(['
.PHP_EOL." $aliases,"
.PHP_EOL.' ]);'
.PHP_EOL,
. PHP_EOL . ' $middleware->alias(['
. PHP_EOL . " $aliases,"
. PHP_EOL . ' ]);'
. PHP_EOL,
$bootstrapApp,
);

Expand Down Expand Up @@ -236,10 +236,12 @@ protected function removeComposerPackages(array $packages, $asDev = false)
/**
* Update the dependencies in the "package.json" file.
*
* @param callable $callback
* @param bool $dev
* @param array $customPackages
* @return void
*/
protected static function updateNodePackages(callable $callback, $dev = true)
protected static function updateNodePackages(callable $callback, $dev = true, array $customPackages = [])
{
if (! file_exists(base_path('package.json'))) {
return;
Expand All @@ -249,16 +251,17 @@ protected static function updateNodePackages(callable $callback, $dev = true)

$packages = json_decode(file_get_contents(base_path('package.json')), true);

$packages[$configurationKey] = $callback(
array_key_exists($configurationKey, $packages) ? $packages[$configurationKey] : [],
$configurationKey
);
// Merge existing packages with custom packages
$existingPackages = array_key_exists($configurationKey, $packages) ? $packages[$configurationKey] : [];
$mergedPackages = array_merge($existingPackages, $customPackages);

$packages[$configurationKey] = $callback($mergedPackages, $configurationKey);

ksort($packages[$configurationKey]);

file_put_contents(
base_path('package.json'),
json_encode($packages, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT).PHP_EOL
json_encode($packages, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . PHP_EOL
);
}

Expand All @@ -281,7 +284,7 @@ protected static function updateNodeScripts(callable $callback)

file_put_contents(
base_path('package.json'),
json_encode($content, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT).PHP_EOL
json_encode($content, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . PHP_EOL
);
}

Expand Down Expand Up @@ -340,12 +343,12 @@ protected function runCommands($commands)
try {
$process->setTty(true);
} catch (RuntimeException $e) {
$this->output->writeln(' <bg=yellow;fg=black> WARN </> '.$e->getMessage().PHP_EOL);
$this->output->writeln(' <bg=yellow;fg=black> WARN </> ' . $e->getMessage() . PHP_EOL);
}
}

$process->run(function ($type, $line) {
$this->output->write(' '.$line);
$this->output->write(' ' . $line);
});
}

Expand All @@ -369,7 +372,7 @@ protected function removeDarkClasses(Finder $finder)
protected function promptForMissingArgumentsUsing()
{
return [
'stack' => fn () => select(
'stack' => fn() => select(
label: 'Which Breeze stack would you like to install?',
options: [
'blade' => 'Blade with Alpine',
Expand Down Expand Up @@ -402,7 +405,7 @@ protected function afterPromptingForMissingArguments(InputInterface $input, Outp
'typescript' => 'TypeScript',
'eslint' => 'ESLint with Prettier',
]
))->each(fn ($option) => $input->setOption($option, true));
))->each(fn($option) => $input->setOption($option, true));
} elseif (in_array($stack, ['blade', 'livewire', 'livewire-functional'])) {
$input->setOption('dark', confirm(
label: 'Would you like dark mode support?',
Expand Down