Skip to content

Commit eb837d6

Browse files
committed
Updated BuildAssets command
- Added force option to run `uf-clean` (delete installed assets and cached data) - Check for `bundle.result.json` in production mode
1 parent bcb978f commit eb837d6

1 file changed

Lines changed: 47 additions & 9 deletions

File tree

app/system/Bakery/Command/BuildAssets.php

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ protected function configure()
3535
$this->setName("build-assets")
3636
->setDescription("Build the assets using node and npm")
3737
->setHelp("The build directory contains the scripts and configuration files required to download Javascript, CSS, and other assets used by UserFrosting. This command will install Gulp, Bower, and several other required npm packages locally. With <info>npm</info> set up with all of its required packages, it can be use it to automatically download and install the assets in the correct directories. For more info, see <comment>https://learn.userfrosting.com/basics/installation</comment>")
38-
->addOption("compile", "c", InputOption::VALUE_NONE, "Compile the assets and asset bundles for production environment");
38+
->addOption("compile", "c", InputOption::VALUE_NONE, "Compile the assets and asset bundles for production environment")
39+
->addOption("force", "f", InputOption::VALUE_NONE, "Force assets compilation by deleting cached data and installed assets before proceeding");
3940
}
4041

4142
/**
@@ -49,17 +50,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
4950
// Set $path
5051
$this->buildPath = $this->projectRoot . "/build";
5152

53+
// Delete cached data is requested
54+
if ($input->getOption('force')) {
55+
$this->clean();
56+
}
57+
5258
// Perform tasks
5359
$this->npmInstall();
5460
$this->assetsInstall();
5561

56-
// Get current env mode
57-
// N.B.: Need to touch the config service first to load dotenv values
58-
$config = $this->ci->config;
59-
$mode = getenv("UF_MODE") ?: '';
60-
6162
// Compile if requested
62-
if ($input->getOption('compile') || $mode == "production") {
63+
if ($input->getOption('compile') || $this->isProduction()) {
6364
$this->buildAssets();
6465
}
6566

@@ -127,12 +128,49 @@ protected function checkAssets()
127128
$this->io->section("Testing assets installation");
128129

129130
// Get path and vendor files
130-
$vendorPath = \UserFrosting\APP_DIR . '/' . \UserFrosting\SPRINKLES_DIR_NAME . "/core/assets/vendor/*";
131+
$vendorPath = \UserFrosting\APP_DIR . \UserFrosting\DS . \UserFrosting\SPRINKLES_DIR_NAME . "/core/assets/vendor/*";
131132
$coreVendorFiles = glob($vendorPath);
132133

133134
if (!$coreVendorFiles){
134-
$this->io->error("Assets building seems to have failed. Directory `$vendorPath` is empty, but it shouldn't be. Check the above log for any errors.");
135+
$this->io->error("Assets installation seems to have failed. Directory `$vendorPath` is empty, but it shouldn't be. Check the above log for any errors.");
135136
exit(1);
136137
}
138+
139+
// Check that `bundle.result.json` is present in production mode
140+
$config = $this->ci->config;
141+
$resultFile = \UserFrosting\ROOT_DIR . \UserFrosting\DS . \UserFrosting\BUILD_DIR_NAME . \UserFrosting\DS . $config['assets.compiled.schema'];
142+
if ($this->isProduction() && !file_exists($resultFile)) {
143+
$this->io->error("Assets building seems to have failed. File `$resultFile` not found. This file is required for production envrionement. Check the above log for any errors.");
144+
exit(1);
145+
}
146+
}
147+
148+
/**
149+
* Run the `uf-clean` command to delete installed assets, delete compiled
150+
* bundle config file and delete compiled assets
151+
*
152+
* @access protected
153+
* @return void
154+
*/
155+
protected function clean()
156+
{
157+
$this->io->section("Cleaning cached data");
158+
$this->io->writeln("> <comment>npm run uf-clean</comment>");
159+
passthru("npm run uf-clean --prefix " . $this->buildPath);
160+
}
161+
162+
/**
163+
* Return if the app is in production mode
164+
*
165+
* @access protected
166+
* @return bool
167+
*/
168+
protected function isProduction()
169+
{
170+
// N.B.: Need to touch the config service first to load dotenv values
171+
$config = $this->ci->config;
172+
$mode = getenv("UF_MODE") ?: '';
173+
174+
return ($mode == "production");
137175
}
138176
}

0 commit comments

Comments
 (0)