Skip to content

Commit

Permalink
streamline start command
Browse files Browse the repository at this point in the history
  • Loading branch information
braceyourself committed Feb 2, 2025
1 parent 452d363 commit 1676915
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 18 deletions.
19 changes: 19 additions & 0 deletions bin/compose
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,26 @@ case "$1" in
shift 1
setupTraefik


# check if node_modules is installed
if [ ! -d "node_modules" ]; then
echo -e "${YELLOW}\nInstalling node modules...${NC}"
dockerCompose --docker run --rm npm npm install
fi


dockerCompose --docker "up -d --force-recreate --remove-orphans -t0 $*"

echo -ne "${YELLOW}Waiting for $COMPOSE_DOMAIN.${NC}"
#wait until domain returns 200
until curl --output /dev/null --silent --head --fail $COMPOSE_DOMAIN; do
echo -ne "${YELLOW}.${NC}"
sleep 1
done

# echo in green
echo -e "${GREEN}\nReady!${NC}"

;;
up)
shift 1
Expand Down
40 changes: 38 additions & 2 deletions src/Commands/ComposePublishCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,20 @@ public function handle()
'custom' => 'Choose my own setup'
], default: 'default');



$this->setEnv('COMPOSE_PHP_VERSION', $this->getDefaultPhpVersion());
$this->setEnv('USER_ID', $user_id);
$this->setEnv('GROUP_ID', $group_id);
$this->setEnv('COMPOSE_NAME', $dir_name);
$this->setEnv('COMPOSE_ROUTER', $dir_name);
$this->setEnv('COMPOSE_PROFILES', 'local');
$this->setEnv('COMPOSE_NETWORK', 'traefik_default');
$this->setEnv('COMPOSE_PHP_IMAGE', "ethanabrace/php:{$this->getPhpVersion()}", 'services.php.image');
$this->setEnv('COMPOSE_DOMAIN', $this->getComposeDomain());
$this->setEnv('COMPOSE_PHP_IMAGE', "ethanabrace/php:{$this->getPhpVersion()}");
$this->ensureViteServerSettings();

$connection_name = $this->setEnv('DB_CONNECTION', $this->getDatabaseConnectionName(), 'database.default');
$connection_name = $this->setEnv('DB_CONNECTION', $this->getDatabaseConnectionName());

if ($connection_name !== 'sqlite') {
$this->setEnv('DB_HOST', $this->getDbHost($connection_name), "database.connections.{$connection_name}.host");
Expand Down Expand Up @@ -211,4 +216,35 @@ private function getDbPassword(mixed $connection_name)
};
}

private function getDefaultPhpVersion()
{
if($this->setup_type == 'default'){
return $this->getPhpVersions()->first();
}

return fn() => select("Select PHP Version:", $this->getPhpVersions());
}

private function ensureViteServerSettings()
{
$needs_config = !$this->getViteConfig()->contains('server:');
if($this->setup_type == 'default' && $needs_config){
$this->addViteServerSettings();
}


}

private function getComposeDomain()
{
if($this->setup_type == 'default'){
return str(config('compose.name'))->slug() . ".localhost";
}

return fn() => text("What domain name would you like to use?",
default: str(config('compose.name'))->slug() . ".localhost",
hint: "This will be used to view your application in the browser"
);
}

}
39 changes: 23 additions & 16 deletions src/Concerns/HasNodeServices.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,11 @@ trait HasNodeServices
private function npmServiceDefinition($config, $env = 'local'): array
{
if (file_exists(base_path('vite.config.js'))) {
$vite_config_content = str(file_get_contents(base_path('vite.config.js')));
// match contents of defineConfig({ ... })
if (!$vite_config_content->contains('server:')) {
if (!$this->getViteConfig()->contains('server:')) {
warning("It looks like you're using Vite, but you haven't defined server settings in your vite.config.js file.");
if (confirm("Would you like to add server settings to your vite.config.js file?")) {
$vite_config_content = $vite_config_content->replace(
'defineConfig({',
<<<EOF
defineConfig({
server: {
hmr: {
host: 'hmr.{$this->getDomainName()}',
port: 80
},
},
EOF
);

file_put_contents(base_path('vite.config.js'), $vite_config_content);
$this->addViteServerSettings();

info("HMR Server settings have been added to your vite.config.js file.");
warning("Be sure to review the settings to ensure they are correct.");
Expand Down Expand Up @@ -62,4 +48,25 @@ private function npmServiceDefinition($config, $env = 'local'): array
'profiles' => ['local']
])->merge($config)->toArray();
}

protected function addViteServerSettings()
{
file_put_contents(base_path('vite.config.js'), $this->getViteConfig()->replace(
'defineConfig({',
<<<EOF
defineConfig({
server: {
hmr: {
host: 'hmr.{$this->getDomainName()}',
port: 80
},
},
EOF
));
}

public function getViteConfig()
{
return str(file_get_contents(base_path('vite.config.js')));
}
}
1 change: 1 addition & 0 deletions src/Concerns/InteractsWithEnvFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public function getConfigKeyForEnv($key)
'DB_USERNAME' => "database.connections.".config('database.default').".username",
'DB_PASSWORD' => "database.connections.".config('database.default').".password",
'COMPOSE_PHP_IMAGE' => 'compose.services.php.image',
'COMPOSE_PHP_VERSION' => 'compose.services.php.version',
default => str($key)->lower()->replace('compose_', 'compose.')->value()
};
}
Expand Down

0 comments on commit 1676915

Please sign in to comment.