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

Conversation

tal7aouy
Copy link

This PR enhances the updateNodePackages method in the InstallCommand class to support custom package versions. This feature allows users to specify custom versions of packages they want to install, providing greater flexibility and control over their project's dependencies.

Changes Made

  • Enhanced updateNodePackages Method:
    • Added a new parameter $customPackages to accept an array of custom package versions.
    • Merged existing packages with custom packages before updating the package.json file.

Code Changes

Updated updateNodePackages Method

/**
 * 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, array $customPackages = [])
{
    if (! file_exists(base_path('package.json'))) {
        return;
    }

    $configurationKey = $dev ? 'devDependencies' : 'dependencies';

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

    // 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
    );
}

Example Usage in handle Method

public function handle()
{
    // Other setup code...

    // Update Node packages with custom versions
    $this->updateNodePackages(function ($packages) {
        return [
            '@vue/server-renderer' => '^3.4.0',
        ] + $packages;
    }, true, [
        'custom-package' => '^1.0.0',
        'another-package' => '^2.1.0',
    ]);

    // Other setup code...
}

This enhancement provides more flexibility and control over project dependencies by allowing custom package versions to be specified during the update process. This feature is particularly useful for projects that require specific versions of certain packages.

@tal7aouy tal7aouy changed the title Add Support for Custom Package Versions in updateNodePackages Method [Feat] Add Support for Custom Package Versions in updateNodePackages Method Sep 21, 2024
@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

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.

2 participants