Skip to content

Commit

Permalink
adds laravel mix and node scripts to fix #1181
Browse files Browse the repository at this point in the history
  • Loading branch information
austintoddj committed Jun 22, 2023
1 parent 129c131 commit 391a7f8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
2 changes: 2 additions & 0 deletions resources/stubs/webpack.mix.stub
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
const mix = require('laravel-mix');

mix.js('resources/js/canvas-ui/app.js', 'public/js/canvas-ui.js').vue()
.sass('resources/sass/canvas-ui.scss', 'public/css/canvas-ui.css');
2 changes: 1 addition & 1 deletion resources/views/ui.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<link rel="dns-prefetch" href="//fonts.gstatic.com">
<link rel="stylesheet" href="//fonts.googleapis.com/css2?family=Karla&family=Merriweather:wght@400;700&display=swap">

<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/highlightjs/cdn-release@10.1.2/build/styles/github.min.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.8.0/build/styles/github.min.css">
</head>
<body class="mb-5">

Expand Down
60 changes: 49 additions & 11 deletions src/Console/UiCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function handle()
'bootstrap' => '^4.6.0',
'highlight.js' => '^10.5.0',
'jquery' => '^3.5.1',
'laravel-mix' => '^6.0.49',
'medium-zoom' => '^1.0.6',
'moment' => '^2.29.1',
'nprogress' => '^0.2.0',
Expand All @@ -60,17 +61,24 @@ public function handle()
] + $packages;
});

$this->updateNodeScripts(function ($scripts) {
return [
'canvas.ui.dev' => 'mix',
'canvas.ui.prod' => 'mix --production'
] + $scripts;
});

// Sass configuration...
copy(dirname(__DIR__, 2).'/resources/sass/ui.scss', resource_path('sass/canvas-ui.scss'));
copy(dirname(__DIR__, 2) . '/resources/sass/ui.scss', resource_path('sass/canvas-ui.scss'));

// Single page application...
(new Filesystem)->copyDirectory(dirname(__DIR__, 2).'/resources/js/ui', resource_path('js/canvas-ui'));
(new Filesystem)->copyDirectory(dirname(__DIR__, 2) . '/resources/js/ui', resource_path('js/canvas-ui'));

$this->updateWebpackConfiguration();
$this->flushNodeModules();

$this->info('Installation complete.');
$this->comment('Please run "npm install && npm run dev" to build your assets.');
$this->comment('Please run "npm install && npm run canvas.ui.dev" to build your assets.');
}

/**
Expand All @@ -82,7 +90,7 @@ public function handle()
*/
protected function updateNodePackages(callable $callback, $dev = true)
{
if (! file_exists(base_path('package.json'))) {
if (!file_exists(base_path('package.json'))) {
return;
}

Expand All @@ -99,7 +107,37 @@ protected function updateNodePackages(callable $callback, $dev = true)

file_put_contents(
base_path('package.json'),
json_encode($packages, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT).PHP_EOL
json_encode($packages, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . PHP_EOL
);
}

/**
* Update the "package.json" scripts.
*
* @param callable $callback
* @param bool $dev
* @return void
*/
protected function updateNodeScripts(callable $callback)
{
if (!file_exists(base_path('package.json'))) {
return;
}

$configurationKey = 'scripts';

$packages = json_decode(file_get_contents(base_path('package.json')), true);

$packages[$configurationKey] = $callback(
array_key_exists($configurationKey, $packages) ? $packages[$configurationKey] : [],
$configurationKey
);

ksort($packages[$configurationKey]);

file_put_contents(
base_path('package.json'),
json_encode($packages, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . PHP_EOL
);
}

Expand All @@ -110,13 +148,13 @@ protected function updateNodePackages(callable $callback, $dev = true)
*/
protected function exportViews()
{
if (file_exists($view = $this->getViewPath('canvas-ui.blade.php')) && ! $this->option('force')) {
if (! $this->confirm('The [canvas-ui.blade.php] view already exists. Do you want to replace it?')) {
if (file_exists($view = $this->getViewPath('canvas-ui.blade.php')) && !$this->option('force')) {
if (!$this->confirm('The [canvas-ui.blade.php] view already exists. Do you want to replace it?')) {
return;
}
}

copy(dirname(__DIR__, 2).'/resources/views/ui.blade.php', $view);
copy(dirname(__DIR__, 2) . '/resources/views/ui.blade.php', $view);
}

/**
Expand All @@ -131,13 +169,13 @@ protected function exportBackend()
str_replace(
'{{namespace}}',
$this->laravel->getNamespace(),
file_get_contents(dirname(__DIR__, 2).'/resources/stubs/controllers/CanvasUiController.stub')
file_get_contents(dirname(__DIR__, 2) . '/resources/stubs/controllers/CanvasUiController.stub')
)
);

file_put_contents(
base_path('routes/web.php'),
file_get_contents(dirname(__DIR__, 2).'/resources/stubs/routes/web.stub'),
file_get_contents(dirname(__DIR__, 2) . '/resources/stubs/routes/web.stub'),
FILE_APPEND
);
}
Expand Down Expand Up @@ -179,7 +217,7 @@ protected function updateWebpackConfiguration()
{
file_put_contents(
base_path('webpack.mix.js'),
file_get_contents(dirname(__DIR__, 2).'/resources/stubs/webpack.mix.stub'),
file_get_contents(dirname(__DIR__, 2) . '/resources/stubs/webpack.mix.stub'),
FILE_APPEND
);
}
Expand Down

0 comments on commit 391a7f8

Please sign in to comment.