|
1
|
|
|
<?php namespace Arcanedev\Assets\Pipes\Init; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\Assets\Helpers\Stub; |
|
4
|
|
|
use Arcanedev\Assets\Helpers\Workspaces; |
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Illuminate\Support\Facades\File; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class ExtractNpmDependencies |
|
10
|
|
|
* |
|
11
|
|
|
* @package Arcanedev\Assets\Pipes\Init |
|
12
|
|
|
* @author ARCANEDEV <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class ExtractNpmDependencies extends AbstractPipe |
|
15
|
|
|
{ |
|
16
|
|
|
/* ----------------------------------------------------------------- |
|
17
|
|
|
| Properties |
|
18
|
|
|
| ----------------------------------------------------------------- |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
/** @var array */ |
|
22
|
|
|
protected $except = [ |
|
23
|
|
|
'cross-env', |
|
24
|
|
|
'laravel-mix', |
|
25
|
|
|
]; |
|
26
|
|
|
|
|
27
|
|
|
/* ----------------------------------------------------------------- |
|
28
|
|
|
| Main Method |
|
29
|
|
|
| ----------------------------------------------------------------- |
|
30
|
|
|
*/ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param array $passable |
|
34
|
|
|
* @param \Closure $next |
|
35
|
|
|
* |
|
36
|
|
|
* @return mixed |
|
37
|
|
|
*/ |
|
38
|
|
|
public function handle(array $passable, Closure $next) |
|
39
|
|
|
{ |
|
40
|
|
|
if ( ! file_exists(base_path('package.json'))) |
|
41
|
|
|
return false; |
|
42
|
|
|
|
|
43
|
|
|
$old = $this->getSourceFile(); |
|
44
|
|
|
$new = $this->getStubFile($passable)->toArray(); |
|
45
|
|
|
|
|
46
|
|
|
foreach (['devDependencies', 'dependencies'] as $key) { |
|
47
|
|
|
list($remaining, $extracted) = $this->partitionNpmDependencies($old[$key] ?? []); |
|
48
|
|
|
|
|
49
|
|
|
$old[$key] = $remaining; |
|
50
|
|
|
$new[$key] = array_merge($new[$key] ?? [], $extracted); |
|
51
|
|
|
|
|
52
|
|
|
ksort($old[$key]); |
|
53
|
|
|
ksort($new[$key]); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$old['workspaces'] = array_map(function ($directory) { |
|
57
|
|
|
return "{$directory}/*"; |
|
58
|
|
|
}, Workspaces::getAllRootDirectories()); |
|
59
|
|
|
|
|
60
|
|
|
$this->saveJson('package.json', $old); |
|
61
|
|
|
$this->saveJson($passable['path'].'/package.json', $new); |
|
62
|
|
|
|
|
63
|
|
|
return $next($passable); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/* ----------------------------------------------------------------- |
|
67
|
|
|
| Other Methods |
|
68
|
|
|
| ----------------------------------------------------------------- |
|
69
|
|
|
*/ |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get the package.json in the base folder. |
|
73
|
|
|
* |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function getSourceFile() |
|
77
|
|
|
{ |
|
78
|
|
|
return json_decode(file_get_contents(base_path('package.json')), true); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get the stub file. |
|
83
|
|
|
* |
|
84
|
|
|
* @param array $passable |
|
85
|
|
|
* |
|
86
|
|
|
* @return \Arcanedev\Assets\Helpers\Stub |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function getStubFile(array $passable) |
|
89
|
|
|
{ |
|
90
|
|
|
return Stub::make('module/package.stub') |
|
91
|
|
|
->replace([ |
|
92
|
|
|
'{{root}}', |
|
93
|
|
|
'{{name}}', |
|
94
|
|
|
],[ |
|
95
|
|
|
$passable['root-package'], |
|
96
|
|
|
$passable['name'], |
|
97
|
|
|
]); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Split the dependencies. |
|
102
|
|
|
* |
|
103
|
|
|
* @param array $dependencies |
|
104
|
|
|
* |
|
105
|
|
|
* @return array |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function partitionNpmDependencies(array $dependencies) |
|
108
|
|
|
{ |
|
109
|
|
|
return collect($dependencies)->partition(function ($version, $dependency) { |
|
110
|
|
|
return in_array($dependency, $this->except); |
|
111
|
|
|
})->toArray(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Save the content as a json file. |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $path |
|
118
|
|
|
* @param array $content |
|
119
|
|
|
* |
|
120
|
|
|
* @return int |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function saveJson($path, array $content) |
|
123
|
|
|
{ |
|
124
|
|
|
$content = array_filter($content); |
|
125
|
|
|
|
|
126
|
|
|
return File::put( |
|
127
|
|
|
base_path($path), |
|
128
|
|
|
json_encode($content, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . PHP_EOL |
|
129
|
|
|
); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|