Skip to content

Commit 1cb49d6

Browse files
committed
Issue #4. Throw exception if component or mixin already exists.
1 parent a226680 commit 1cb49d6

File tree

5 files changed

+77
-1
lines changed

5 files changed

+77
-1
lines changed

src/Commands/MakeComponent.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public function handle()
3333

3434
$fullPath = resource_path("{$path}/{$name}");
3535

36+
$this->checkFileExists($filesystem, $fullPath, $name);
37+
3638
$stub = $this->getStub($filesystem);
3739

3840
$filesystem->put($fullPath, $stub);

src/Commands/MakeMixin.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public function handle()
3333

3434
$fullPath = resource_path("{$path}/{$name}");
3535

36+
$this->checkFileExists($filesystem, $fullPath, $name);
37+
3638
$stub = $this->getStub($filesystem);
3739

3840
$filesystem->put($fullPath, $stub);

src/Commands/VueGeneratorsCommand.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use VueGenerators\Paths;
66
use Illuminate\Console\Command;
77
use Illuminate\Filesystem\Filesystem;
8+
use VueGenerators\Exceptions\ResourceAlreadyExists;
89

910
class VueGeneratorsCommand extends Command
1011
{
@@ -30,4 +31,11 @@ protected function createPath(Filesystem $filesystem, $type)
3031

3132
return $path;
3233
}
34+
35+
protected function checkFileExists(Filesystem $filesystem, $path, $name)
36+
{
37+
if ($filesystem->exists($path)) {
38+
throw ResourceAlreadyExists::fileExists($name);
39+
}
40+
}
3341
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace VueGenerators\Exceptions;
4+
5+
use Exception;
6+
7+
class ResourceAlreadyExists extends Exception
8+
{
9+
/**
10+
* File of given name already exists at path.
11+
*
12+
* @param string $name
13+
*
14+
* @return static
15+
*/
16+
public static function fileExists($name)
17+
{
18+
return new static(
19+
"File {$name} already exists at path."
20+
);
21+
}
22+
}

tests/CommandTest.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function it_saves_component_file_to_specified_path()
7676
/**
7777
* @test
7878
*/
79-
public function it_saves_componets_to_path_set_in_config()
79+
public function it_saves_components_to_path_set_in_config()
8080
{
8181
app()['config']->set('vue-generators.paths.components', 'custom/path');
8282

@@ -91,6 +91,27 @@ public function it_saves_componets_to_path_set_in_config()
9191
$this->assertFileExists($file);
9292
}
9393

94+
/**
95+
* @test
96+
*
97+
* @expectedException VueGenerators\Exceptions\ResourceAlreadyExists
98+
* @expectedExceptionMessage File NewComponent.vue already exists at path.
99+
*/
100+
public function it_doesnt_overwrite_components_that_already_exist()
101+
{
102+
$file = resource_path('assets/js/components/NewComponent.vue');
103+
104+
Artisan::call('vueg:component', [
105+
'name' => 'NewComponent'
106+
]);
107+
108+
$this->assertFileExists($file);
109+
110+
Artisan::call('vueg:component', [
111+
'name' => 'NewComponent'
112+
]);
113+
}
114+
94115
/**
95116
* @test
96117
*/
@@ -175,4 +196,25 @@ public function it_saves_mixins_to_path_set_in_config()
175196

176197
$this->assertFileExists($file);
177198
}
199+
200+
/**
201+
* @test
202+
*
203+
* @expectedException VueGenerators\Exceptions\ResourceAlreadyExists
204+
* @expectedExceptionMessage File NewMixin.js already exists at path.
205+
*/
206+
public function it_doesnt_overwrite_mixin_that_already_exist()
207+
{
208+
$file = resource_path('assets/js/mixins/NewMixin.js');
209+
210+
Artisan::call('vueg:mixin', [
211+
'name' => 'NewMixin'
212+
]);
213+
214+
$this->assertFileExists($file);
215+
216+
Artisan::call('vueg:mixin', [
217+
'name' => 'NewMixin'
218+
]);
219+
}
178220
}

0 commit comments

Comments
 (0)