• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

eliashaeussler / version-bumper / 12732622834

12 Jan 2025 10:18AM UTC coverage: 89.29% (+0.5%) from 88.745%
12732622834

Pull #17

github

web-flow
Merge 43d7c218e into 0b06fcb19
Pull Request #17: [!!!][FEATURE] Introduce config presets

100 of 110 new or added lines in 6 files covered. (90.91%)

4 existing lines in 1 file now uncovered.

717 of 803 relevant lines covered (89.29%)

3.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.48
/src/Config/Preset/NpmPackagePreset.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Composer package "eliashaeussler/version-bumper".
7
 *
8
 * Copyright (C) 2024-2025 Elias Häußler <elias@haeussler.dev>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace EliasHaeussler\VersionBumper\Config\Preset;
25

26
use EliasHaeussler\VersionBumper\Config;
27
use Symfony\Component\OptionsResolver;
28

29
use function implode;
30
use function sprintf;
31

32
/**
33
 * NpmPackagePreset.
34
 *
35
 * @author Elias Häußler <elias@haeussler.dev>
36
 * @license GPL-3.0-or-later
37
 */
38
final class NpmPackagePreset implements Preset
39
{
40
    /**
41
     * @var array{packageName: string, path: string|null}
42
     */
43
    private array $options;
44

45
    public function __construct(array $options)
2✔
46
    {
47
        $this->options = $this->resolveOptions($options);
2✔
48
    }
49

50
    public function getConfig(): Config\VersionBumperConfig
1✔
51
    {
52
        $filesToModify = [
1✔
53
            new Config\FileToModify(
1✔
54
                $this->getPath('package.json'),
1✔
55
                [
1✔
56
                    new Config\FilePattern('"version": "{%version%}"'),
1✔
57
                ],
1✔
58
                true,
1✔
59
            ),
1✔
60
            new Config\FileToModify(
1✔
61
                $this->getPath('package-lock.json'),
1✔
62
                [
1✔
63
                    new Config\FilePattern(
1✔
64
                        sprintf(
1✔
65
                            '"name": "%s",\s+"version": "{%%version%%}"',
1✔
66
                            $this->options['packageName'],
1✔
67
                        ),
1✔
68
                    ),
1✔
69
                ],
1✔
70
                true,
1✔
71
            ),
1✔
72
        ];
1✔
73

74
        return new Config\VersionBumperConfig(filesToModify: $filesToModify);
1✔
75
    }
76

NEW
77
    public static function getIdentifier(): string
×
78
    {
NEW
79
        return 'npm-package';
×
80
    }
81

NEW
82
    public static function getDescription(): string
×
83
    {
NEW
84
        return 'NPM package, managed by package.json and package-lock.json';
×
85
    }
86

87
    private function getPath(string $filename): string
1✔
88
    {
89
        return implode('/', [$this->options['path'], $filename]);
1✔
90
    }
91

92
    /**
93
     * @param array<string, mixed> $options
94
     *
95
     * @return array{packageName: string, path: string|null}
96
     */
97
    private function resolveOptions(array $options): array
2✔
98
    {
99
        $optionsResolver = new OptionsResolver\OptionsResolver();
2✔
100
        $optionsResolver->define('packageName')
2✔
101
            ->allowedTypes('string')
2✔
102
            ->required()
2✔
103
        ;
2✔
104
        $optionsResolver->define('path')
2✔
105
            ->allowedTypes('string', 'null')
2✔
106
            ->default(null)
2✔
107
        ;
2✔
108

109
        /* @phpstan-ignore return.type */
110
        return $optionsResolver->resolve($options);
2✔
111
    }
112
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc