Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport #710 to v19 branch #718

Merged
merged 15 commits into from
Dec 16, 2024
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