Skip to content

Commit 3779b38

Browse files
committed
code refactoring
1 parent b872cbd commit 3779b38

File tree

3 files changed

+143
-60
lines changed

3 files changed

+143
-60
lines changed

src/Commands/MakeThemeCommand.php

+21-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
use Illuminate\Support\Str;
88
use Qirolab\Theme\Presets\PresetExport;
99
use Qirolab\Theme\Presets\Traits\AuthScaffolding;
10+
use Qirolab\Theme\Presets\Traits\PackagesTrait;
1011
use Qirolab\Theme\Theme;
1112

1213
class MakeThemeCommand extends Command
1314
{
1415
use AuthScaffolding;
16+
use PackagesTrait;
1517

1618
/**
1719
* @var string
@@ -110,17 +112,34 @@ protected function askCssFramework()
110112

111113
protected function askJsFramework()
112114
{
115+
$jsFrameworks = $this->getAllowedJsFrameworks();
116+
113117
$jsFramework = $this->choice(
114118
'Select Javascript Framework',
115-
['Vue 2', 'Vue 3', 'React', 'Skip'],
116-
$default = 'Vue 2',
119+
$jsFrameworks,
120+
$jsFrameworks[0], // Default value
117121
$maxAttempts = null,
118122
$allowMultipleSelections = false
119123
);
120124

121125
return $jsFramework;
122126
}
123127

128+
public function getAllowedJsFrameworks() :array
129+
{
130+
$vueVersion = $this->getVueVersion($dev = true) ?? $this->getVueVersion($dev = false) ;
131+
132+
if ($vueVersion && $this->versionLessThan($vueVersion, '3.0.0')) {
133+
return ['Vue 2', 'React', 'Skip'];
134+
}
135+
136+
if ($vueVersion && $this->versionGreaterOrEqual($vueVersion, '3.0.0')) {
137+
return ['Vue 3', 'React', 'Skip'];
138+
}
139+
140+
return ['Vue 2', 'Vue 3', 'React', 'Skip'];
141+
}
142+
124143
public function askAuthScaffolding()
125144
{
126145
$authScaffolding = $this->choice(

src/Presets/Traits/PackagesTrait.php

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
namespace Qirolab\Theme\Presets\Traits;
4+
5+
trait PackagesTrait
6+
{
7+
public function getPackages()
8+
{
9+
if (! file_exists(base_path('package.json'))) {
10+
return;
11+
}
12+
13+
return json_decode(file_get_contents(base_path('package.json')), true);
14+
}
15+
16+
/**
17+
* Update the "package.json" file.
18+
*
19+
* @param bool $dev
20+
*
21+
* @return null|$this
22+
*/
23+
protected function updatePackages($dev = true)
24+
{
25+
if ($packages = $this->getPackages()) {
26+
$configurationKey = $dev ? 'devDependencies' : 'dependencies';
27+
28+
$packages[$configurationKey] = static::updatePackageArray(
29+
array_key_exists($configurationKey, $packages) ? $packages[$configurationKey] : []
30+
);
31+
32+
ksort($packages[$configurationKey]);
33+
34+
file_put_contents(
35+
base_path('package.json'),
36+
json_encode($packages, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . PHP_EOL
37+
);
38+
39+
return $this;
40+
}
41+
42+
return null;
43+
}
44+
45+
/**
46+
* @param string $package
47+
* @param bool $dev
48+
* @return null|string
49+
*/
50+
public function getPackageVersion($package, $dev = true)
51+
{
52+
if ($packages = $this->getPackages()) {
53+
$configurationKey = $dev ? 'devDependencies' : 'dependencies';
54+
55+
$version = $packages[$configurationKey][$package] ?? null;
56+
57+
return $this->getVersion($version);
58+
}
59+
60+
return null;
61+
}
62+
63+
/**
64+
* @param string $str
65+
* @return null|string
66+
*/
67+
protected function getVersion($str)
68+
{
69+
preg_match("/\s*((?:[0-9]+\.?)+)/i", $str, $matches);
70+
71+
return $matches[1] ?? null;
72+
}
73+
74+
/**
75+
* @param bool $dev
76+
* @return null|string
77+
*/
78+
public function getMixVersion($dev = true)
79+
{
80+
return $this->getPackageVersion('laravel-mix', $dev);
81+
}
82+
83+
/**
84+
* @param bool $dev
85+
* @return null|string
86+
*/
87+
public function getVueVersion($dev = true)
88+
{
89+
return $this->getPackageVersion('vue', $dev);
90+
}
91+
92+
/**
93+
* @param string $actual
94+
* @param string $compare
95+
* @return bool
96+
*/
97+
public function versionLessThan($actual, $compare)
98+
{
99+
return version_compare($actual, $compare, '<');
100+
}
101+
102+
/**
103+
* @param string $actual
104+
* @param string $compare
105+
* @return bool
106+
*/
107+
public function versionGreaterThan($actual, $compare)
108+
{
109+
return version_compare($actual, $compare, '>');
110+
}
111+
112+
/**
113+
* @param string $actual
114+
* @param string $compare
115+
* @return bool
116+
*/
117+
public function versionGreaterOrEqual($actual, $compare)
118+
{
119+
return version_compare($actual, $compare, '>=');
120+
}
121+
}

src/Presets/Traits/PresetTrait.php

+1-58
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
trait PresetTrait
99
{
1010
use HandleFiles;
11+
use PackagesTrait;
1112

1213
/**
1314
* @var PresetExport
@@ -33,62 +34,4 @@ public function jsPreset()
3334
{
3435
return $this->exporter->jsPreset();
3536
}
36-
37-
/**
38-
* Update the "package.json" file.
39-
*
40-
* @param bool $dev
41-
*
42-
* @return null|$this
43-
*/
44-
protected function updatePackages($dev = true)
45-
{
46-
if (! file_exists(base_path('package.json'))) {
47-
return;
48-
}
49-
50-
$configurationKey = $dev ? 'devDependencies' : 'dependencies';
51-
52-
$packages = json_decode(file_get_contents(base_path('package.json')), true);
53-
54-
$packages[$configurationKey] = static::updatePackageArray(
55-
array_key_exists($configurationKey, $packages) ? $packages[$configurationKey] : []
56-
);
57-
58-
ksort($packages[$configurationKey]);
59-
60-
file_put_contents(
61-
base_path('package.json'),
62-
json_encode($packages, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . PHP_EOL
63-
);
64-
65-
return $this;
66-
}
67-
68-
/**
69-
* @return null|string
70-
*/
71-
public function getMixVersion()
72-
{
73-
if (! file_exists(base_path('package.json'))) {
74-
return;
75-
}
76-
77-
$packages = json_decode(file_get_contents(base_path('package.json')), true);
78-
79-
$mixVersion = $packages['devDependencies']['laravel-mix'] ?? null;
80-
81-
return $this->getVersion($mixVersion);
82-
}
83-
84-
/**
85-
* @param string $str
86-
* @return string
87-
*/
88-
protected function getVersion($str)
89-
{
90-
preg_match("/\s*((?:[0-9]+\.?)+)/i", $str, $matches);
91-
92-
return $matches[1];
93-
}
9437
}

0 commit comments

Comments
 (0)