Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit ce7fa16

Browse files
committed
Update new version, improve with config file
1 parent 746ca3c commit ce7fa16

File tree

5 files changed

+68
-22
lines changed

5 files changed

+68
-22
lines changed

src/Console/CreateJSRoutesCommand.php

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ class CreateJSRoutesCommand extends Command
1515
protected $signature = "route:tojs
1616
{ --name=routes.js : Name of the output file. }
1717
{ --p|path= : Path of the output file. }
18-
{ --w|whitelist= : List of comma separated route names to include (overrides ignore and methods). }
19-
{ --i|ignore=telescope : List of comma separated route names to ignore (overrides methods). }
20-
{ --m|methods=GET : List of comma separated methods accepted by filter. Empty for include all methods. }
18+
{ --i|include= : List of comma separated route names to include (overrides exclude and methods). }
19+
{ --e|exclude= : List of comma separated route names to exclude (overrides methods). }
20+
{ --m|methods= : List of comma separated methods accepted by filter. Empty for include all methods. }
2121
{ --f|force : Overwrite existing routes by default. }";
2222

2323
/**
2424
* The console command description.
2525
*
2626
* @var string
2727
*/
28-
protected $description = "Create object with routes, and a function for its JS use";
28+
protected $description = "Create object with routes, and a function for its JS use. Uses key from configuration file 'jsroutes'.";
2929

3030
/**
3131
* Create a new command instance.
@@ -50,7 +50,7 @@ public function handle()
5050
return $this->includeRoute($route, $key);
5151
})->map(function ($route) {
5252
return [
53-
"uri" => $route->uri
53+
'uri' => $route->uri
5454
];
5555
});
5656

@@ -60,9 +60,9 @@ public function handle()
6060
$content .= json_encode($routes, $jsonFlags);
6161
$content .= ";\n\n";
6262

63-
$content .= file_get_contents(__DIR__ . "/../assets/js/routeFunction.js");
63+
$content .= file_get_contents(__DIR__ . '/../assets/js/routeFunction.js');
6464

65-
$fileName = $this->option("name");
65+
$fileName = config('jsroutes.name', $this->option('name'));
6666
if ($this->createFile($fileName, $content)) {
6767
$this->info($fileName . " created");
6868
}
@@ -72,14 +72,14 @@ public function createFile($fileName, $contents)
7272
{
7373
if (
7474
file_exists($file = $this->getJSPath($fileName)) &&
75-
!$this->option("force")
75+
!$this->option('force')
7676
) {
7777
if (
7878
!$this->confirm(
79-
"The [" . $fileName . "] file already exists. Do you want to replace it?"
79+
'The [' . $fileName . '] file already exists. Do you want to replace it?'
8080
)
8181
) {
82-
$this->error("Error");
82+
$this->error('Error');
8383
return false;
8484
}
8585
}
@@ -90,21 +90,26 @@ public function createFile($fileName, $contents)
9090

9191
private function includeRoute($route, $routeName)
9292
{
93-
$whitelist = explode(",", $this->option("whitelist"));
93+
$include = config('jsroutes.include', explode(',', $this->option('include')));
9494

95-
if (in_array($routeName, $whitelist)) {
95+
if (in_array($routeName, $include)) {
9696
return true;
9797
}
9898

99-
$ignore = explode(",", $this->option("ignore"));
99+
$exclude = config('jsroutes.exclude', explode(',', $this->option('exclude')));
100100

101-
if (in_array($routeName, $ignore)) {
101+
if (in_array($routeName, $exclude)) {
102102
return false;
103103
}
104104

105-
$methods = $this->option("methods");
105+
if (config('jsroutes.methods') !== null) {
106+
$methods = implode(',', config('jsroutes.methods'));
107+
} else {
108+
$methods = $this->option('methods');
109+
}
110+
106111
$valid = empty($methods);
107-
foreach (explode(",", $methods) as $method) {
112+
foreach (explode(',', $methods) as $method) {
108113
$valid |= in_array($method, $route->methods);
109114
}
110115

@@ -113,7 +118,7 @@ private function includeRoute($route, $routeName)
113118

114119
public function getJSPath($fileName)
115120
{
116-
$path = $this->option("path") ?? config('js.path')[0] ?? resource_path('js');
121+
$path = config('jsroutes.path', $this->option('path') ?? config('js.path')[0] ?? resource_path('js'));
117122
return implode(DIRECTORY_SEPARATOR, [$path, $fileName]);
118123
}
119124
}

src/JSRoutesServiceProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
class JSRoutesServiceProvider extends ServiceProvider
88
{
9+
public function boot()
10+
{
11+
$this->mergeConfigFrom(__DIR__ . '/config/jsroutes.php', 'jsroutes');
12+
}
13+
914
public function register()
1015
{
1116
$this->commands([

src/config/jsroutes.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
return [
4+
'path' => './',
5+
'name' => 'routes.js',
6+
'include' => [
7+
'boss.bye'
8+
],
9+
'exclude' => [
10+
'telescope'
11+
],
12+
'methods' => [
13+
'GET'
14+
],
15+
];

tests/CreateJSRoutesCommandTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class CreateJSRoutesCommandTest extends TestCase
1010
protected function getPackageProviders($app)
1111
{
1212
return [
13-
'Halivert\JSRoutes\JSRoutesServiceProvider'
13+
'Halivert\JSRoutes\Tests\Stubs\Providers\JSRoutesServiceProviderTest'
1414
];
1515
}
1616

@@ -54,12 +54,13 @@ protected function getEnvironmentSetUp($app)
5454

5555
public function test_can_create_file()
5656
{
57-
$command = "route:tojs -p ./ -m GET,PATCH -w boss.bye";
58-
$result = "routes.js created";
57+
$name = config('jsroutes.name', 'routes.js');
58+
$command = "route:tojs";
59+
$result = $name . " created";
5960

60-
if (file_exists(realpath("./routes.js"))) {
61+
if (file_exists(realpath("./" . $name))) {
6162
$this->artisan($command)->expectsQuestion(
62-
"The [routes.js] file already exists. Do you want to replace it?",
63+
"The [" . $name . "] file already exists. Do you want to replace it?",
6364
"yes"
6465
)->expectsOutput($result)->assertExitCode(0);
6566
} else {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Halivert\JSRoutes\Tests\Stubs\Providers;
4+
5+
use Halivert\JSRoutes\JSRoutesServiceProvider;
6+
use Illuminate\Support\AggregateServiceProvider;
7+
8+
class JSRoutesServiceProviderTest extends AggregateServiceProvider
9+
{
10+
protected $providers = [
11+
JSRoutesServiceProvider::class,
12+
];
13+
14+
public function register()
15+
{
16+
parent::register();
17+
18+
$this->app['service.loaded'] = true;
19+
}
20+
}

0 commit comments

Comments
 (0)