22
33namespace Modular \Modular \Console ;
44
5+ use Illuminate \Support \Str ;
56use Illuminate \Console \Command ;
7+ use Illuminate \Support \Facades \File ;
68use Illuminate \Filesystem \Filesystem ;
7- use Illuminate \Support \Str ;
9+ use Illuminate \Support \Facades \ Artisan ;
810
911class MakeModuleCommand extends Command
1012{
@@ -18,27 +20,30 @@ public function handle(): ?int
1820 {
1921 $ this ->setModuleName ();
2022
21- $ this ->comment ('Creating Module ' .$ this ->moduleName );
23+ $ this ->comment ('Creating Module ' . $ this ->moduleName );
24+
25+ if ($ this ->createModuleDirectoryStructure ()) {
26+ $ this ->createServiceProvider ();
2227
23- $ this ->createModuleDirectoryStructure ();
24- $ this ->createServiceProvider ();
28+ $ params = [
29+ 'moduleName ' => $ this ->moduleName ,
30+ 'resourceName ' => $ this ->moduleName ,
31+ ];
2532
26- $ params = [
27- ' moduleName ' => $ this ->moduleName ,
28- ' resourceName ' => $ this ->moduleName ,
29- ] ;
33+ $ this -> call ( ' modular:make-controller ' , $ params );
34+ $ this ->call ( ' modular:make-validate ' , $ params );
35+ $ this ->call ( ' modular:make-model ' , $ params );
36+ $ this -> call ( ' modular:make-route ' , $ params ) ;
3037
31- $ this ->call ('modular:make-controller ' , $ params );
32- $ this ->call ('modular:make-validate ' , $ params );
33- $ this ->call ('modular:make-model ' , $ params );
34- $ this ->call ('modular:make-route ' , $ params );
38+ $ this ->call ('modular:make-migration ' , ['moduleName ' => $ this ->moduleName , 'migrationName ' => "create {$ this ->moduleName }s_table " ]);
3539
36- $ this ->call ('modular:make-migration ' , ['moduleName ' => $ this ->moduleName , 'migrationName ' => "create {$ this ->moduleName }s_table " ]);
40+ $ this ->call ('modular:make-page ' , $ params );
41+ $ this ->call ('modular:make-test ' , $ params );
3742
38- $ this -> call ( ' modular:make-page ' , $ params ) ;
39- $ this -> call ( ' modular:make-test ' , $ params );
43+ return self :: SUCCESS ;
44+ }
4045
41- return self :: SUCCESS ;
46+ return false ;
4247 }
4348
4449 private function setModulename (): void
@@ -48,7 +53,7 @@ private function setModulename(): void
4853
4954 private function createServiceProvider (): void
5055 {
51- $ stub = file_get_contents (__DIR__ . '/../../stubs/module-stub/modules/ModuleServiceProvider.stub ' );
56+ $ stub = file_get_contents (__DIR__ . '/../../stubs/module-stub/modules/ModuleServiceProvider.stub ' );
5257
5358 $ stub = str_replace ('{{ moduleName }} ' , $ this ->moduleName , $ stub );
5459 $ stub = str_replace ('{{ resourceName }} ' , Str::camel ($ this ->moduleName ), $ stub );
@@ -58,14 +63,26 @@ private function createServiceProvider(): void
5863 file_put_contents ($ path , $ stub );
5964 }
6065
61- private function createModuleDirectoryStructure (): void
66+ private function createModuleDirectoryStructure (): bool
6267 {
63- (new Filesystem )->makeDirectory (base_path ("modules/ {$ this ->moduleName }" ));
64- (new Filesystem )->makeDirectory (base_path ("modules/ {$ this ->moduleName }/Http/Controllers " ), 0755 , true );
65- (new Filesystem )->makeDirectory (base_path ("modules/ {$ this ->moduleName }/Http/Requests " ));
66- (new Filesystem )->makeDirectory (base_path ("modules/ {$ this ->moduleName }/Models " ));
67- (new Filesystem )->makeDirectory (base_path ("modules/ {$ this ->moduleName }/routes " ));
68- (new Filesystem )->makeDirectory (base_path ("modules/ {$ this ->moduleName }/Tests " ));
69- (new Filesystem )->makeDirectory (base_path ("modules/ {$ this ->moduleName }/Database/Migrations " ), 0755 , true );
68+ $ modulePath = base_path ("modules/ {$ this ->moduleName }" );
69+
70+ // Check if the module directory already exists
71+ if (File::exists ($ modulePath )) {
72+ // Output an error message to the console
73+ $ this ->info ("Module {$ this ->moduleName } already exists. SKIPPING " );
74+ return false ;
75+ }
76+
77+ // Create the necessary directories
78+ (new Filesystem )->makeDirectory ($ modulePath );
79+ (new Filesystem )->makeDirectory ("{$ modulePath }/Http/Controllers " , 0755 , true );
80+ (new Filesystem )->makeDirectory ("{$ modulePath }/Http/Requests " );
81+ (new Filesystem )->makeDirectory ("{$ modulePath }/Models " );
82+ (new Filesystem )->makeDirectory ("{$ modulePath }/routes " );
83+ (new Filesystem )->makeDirectory ("{$ modulePath }/Tests " );
84+ (new Filesystem )->makeDirectory ("{$ modulePath }/Database/Migrations " , 0755 , true );
85+
86+ return true ;
7087 }
7188}
0 commit comments