From 391ef82c1adf4254bc0865ac87c10cc5d84eab96 Mon Sep 17 00:00:00 2001 From: Maicol Battistini Date: Mon, 14 Feb 2022 18:14:06 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20Aggiunto=20comando=20per=20?= =?UTF-8?q?facilitare=20la=20pubblicazione=20degli=20assets?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/PublishModulesAssets.php | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 app/Console/Commands/PublishModulesAssets.php diff --git a/app/Console/Commands/PublishModulesAssets.php b/app/Console/Commands/PublishModulesAssets.php new file mode 100644 index 000000000..d842b5c1d --- /dev/null +++ b/app/Console/Commands/PublishModulesAssets.php @@ -0,0 +1,116 @@ +controller->getModules() + ->pluck('extra.osm-modules') + ->map(fn($item) => reset($item)) + ->pluck('moduleName'); + + $selected = $this->option('module'); + if ($selected) { + $modules = $modules->filter(fn(string $slug) => in_array($slug, $selected, true)); + if ($modules->isEmpty()) { + $this->error('No modules found with the given slug'); + + return Command::FAILURE; + } + } + + if (!$selected && !$this->option('all')) { + $modules = $this->choice( + 'Which modules do you want to publish assets? (separate by comma for multiple values)', + $modules->toArray(), + null, + null, + true + ); + } + + // Check boolean options + $dev = $this->option('D'); + $force = $this->option('force'); + + foreach ($modules as $module) { + $tag = "$module:assets"; + if ($dev) { + $tag .= '-dev'; + } + + $this->call('vendor:publish', [ + '--tag' => $tag, + '--force' => $force, + ]); + + $patch_failures = new Collection(); + + $dirs = ServiceProvider::pathsToPublish(null, $tag); + collect($dirs) + ->flatMap(fn($dir) => File::allFiles($dir)) + ->filter(fn(SplFileInfo $file) => $file->getExtension() === 'js') + ->each(function (SplFileInfo $file) use ($patch_failures) { + $content = Str::of($file->getContents()) + ->replaceMatches("/from [\"']openstamanager[\"']/", "from '../../../index.js'") + ->replaceMatches( + "/from [\"']@(?[\w.-]+)\/(?[\w.-]+)[\"']/", + "from '../../$1/$2/index.js'" + ); + + if (!File::put($file->getPathname(), $content)) { + $patch_failures->push($file->getPathname()); + } + }); + + if ($patch_failures->isNotEmpty()) { + $this->error("Failed to patch the following assets of '$module' module:"); + $this->error($patch_failures->implode("\n")); + } else { + $this->info("Successfully patched the assets of '$module' module."); + } + } + + return Command::SUCCESS; + } +}