Skip to content

Commit

Permalink
Merge pull request #718 from humanmade/backport-710-to-v19-branch
Browse files Browse the repository at this point in the history
Backport #710 to v19 branch
  • Loading branch information
mikelittle authored Dec 16, 2024
2 parents 0f01295 + e28c457 commit 55eab90
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
34 changes: 34 additions & 0 deletions docs/nodejs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Node.js

Altis supports running Node.js applications alongside WordPress, utilizing WordPress as a headless API.

## Enabling Node.js in Local Server

Node.js can be enabled in Local Server by adding `extra.altis.modules.local-server.nodejs` in the project's `composer.json`

```json
{
"extra":{
"altis":{
"modules":{
"local-server":{
"nodejs":{
"path":"../altis-nodejs-skeleton"
}
}
}
}
}
}
```

`path` is relative to the directory where `composer.json` lives.

## Setting Node.js Version
Similar to configuring the Altis infrastructure, the Local Server determines the Node.js version to use based on the `engines.node` value found in the `package.json` at the specified `path`.

## Running Development Server
Once configured, the Local Server executes `npm run dev` inside the Node.js container at the specified path. This command watches for changes and recompiles necessary files.

## Accessing the Application
This setup makes the application accessible at `https://nodejs-{project-name}.altis.dev`.
8 changes: 8 additions & 0 deletions inc/composer/class-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,13 @@ protected function start( InputInterface $input, OutputInterface $output ) {
$output->writeln( '<info>Startup completed.</>' );
$output->writeln( '<info>To access your site visit:</> <comment>' . $site_url . '</>' );

if ( static::get_composer_config()['nodejs'] ?? false ) {
$tld = $this->get_project_tld();
$subdomain = $this->get_project_subdomain();
$hostname = $subdomain . '.' . $tld;
$output->writeln( '<info>To access your Node.js site visit:</> <comment>https://nodejs-' . $hostname . '</>' );
}

$this->check_host_entries( $input, $output );

return 0;
Expand Down Expand Up @@ -569,6 +576,7 @@ protected function logs( InputInterface $input, OutputInterface $output ) {
'redis',
's3',
'xray',
'nodejs',
],
0
);
Expand Down
49 changes: 49 additions & 0 deletions inc/composer/class-docker-compose-generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,50 @@ protected function get_service_php() : array {
];
}

/**
* Get the NodeJS container service.
*
* @return array
*/
protected function get_service_nodejs() : array {
$config = $this->get_config();

// Read package.json from nodejs.path to get the Node.js version to use.
$package_json = json_decode( file_get_contents( "{$config['nodejs']['path']}/package.json" ), true );
$version = $package_json['engines']['node'] ?? '20';

return [
'nodejs' => [
'image' => "node:{$version}-bookworm-slim",
'container_name' => "{$this->project_name}-nodejs",
'ports' => [
'3000',
],
'volumes' => [
"../{$config['nodejs']['path']}/:/usr/src/app",
],
'working_dir' => '/usr/src/app',
'command' => 'sh -c "npm install && npm run dev"',
'networks' => [
'proxy',
'default',
],
'labels' => [
'traefik.frontend.priority=1',
'traefik.port=3000',
'traefik.protocol=http',
'traefik.docker.network=proxy',
"traefik.frontend.rule=HostRegexp:nodejs-{$this->hostname}",
"traefik.domain=nodejs-{$this->hostname}",
],
'environment' => [
'ALTIS_ENVIRONMENT_NAME' => $this->project_name,
'ALTIS_ENVIRONMENT_TYPE' => 'local',
],
],
];
}

/**
* Webgrind service container for viewing Xdebug profiles.
*
Expand Down Expand Up @@ -813,6 +857,10 @@ public function get_array() : array {
$services = array_merge( $services, $this->get_service_webgrind() );
}

if ( $this->get_config()['nodejs'] ) {
$services = array_merge( $services, $this->get_service_nodejs() );
}

// Default compose configuration.
$config = [
// 'version' => '2.5',
Expand Down Expand Up @@ -914,6 +962,7 @@ protected function get_config() : array {
'ignore-paths' => [],
'php' => '8.1',
'mysql' => '8.0',
'nodejs' => $modules['nodejs'] ?? false,
];

return array_merge( $defaults, $modules['local-server'] ?? [] );
Expand Down

0 comments on commit 55eab90

Please sign in to comment.