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

Add Node.js Support #710

Merged
merged 16 commits into from
Jul 19, 2024
Merged
29 changes: 29 additions & 0 deletions docs/nodejs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 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",
"version": "21.1"
}
}
}
}
}
}
```

`path` refers to the relative path of the project's front-end code.
`version` refers to the version of Node.js to use

This will make the application available at nodejs-my-project.altis.dev.
7 changes: 7 additions & 0 deletions inc/composer/class-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,13 @@
$output->writeln( '<info>Startup completed.</>' );
$output->writeln( '<info>To access your site visit:</> <comment>' . $site_url . '</>' );

if (static::get_composer_config()['nodejs'] ?? false) {

Check failure on line 325 in inc/composer/class-command.php

View check run for this annotation

HM Linter / hmlinter

inc/composer/class-command.php#L325

No space after opening parenthesis is prohibited
Raw output
{
  "line": 325,
  "column": 9,
  "severity": "error",
  "message": "No space after opening parenthesis is prohibited",
  "source": "WordPress.WhiteSpace.ControlStructureSpacing.NoSpaceAfterOpenParenthesis"
}

Check failure on line 325 in inc/composer/class-command.php

View check run for this annotation

HM Linter / hmlinter

inc/composer/class-command.php#L325

No space before closing parenthesis is prohibited
Raw output
{
  "line": 325,
  "column": 61,
  "severity": "error",
  "message": "No space before closing parenthesis is prohibited",
  "source": "WordPress.WhiteSpace.ControlStructureSpacing.NoSpaceBeforeCloseParenthesis"
}
$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
52 changes: 52 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,53 @@
];
}

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

$version = (string) $this->get_config()['nodejs']['version'] ?? '20';

return [
'nodejs' => [
'image' => "node:{$version}-bookworm",
'container_name' => "{$this->project_name}-nodejs",
'ports' => [
'3000',
],
'volumes' => [
"../{$config['nodejs']['path']}/:/usr/src/app"

Check failure on line 258 in inc/composer/class-docker-compose-generator.php

View check run for this annotation

HM Linter / hmlinter

inc/composer/class-docker-compose-generator.php#L258

Each array item in a multi-line array declaration must end in a comma
Raw output
{
  "line": 258,
  "column": 21,
  "severity": "error",
  "message": "Each array item in a multi-line array declaration must end in a comma",
  "source": "WordPress.Arrays.CommaAfterArrayItem.NoComma"
}
],
'working_dir' => '/usr/src/app',
'command' => 'npm run dev',
'networks' => [
'proxy',
'default',
],
'healthcheck' => [
'test' => [
'CMD-SHELL',
'curl --silent --fail localhost:3000 || exit 1',
],
'interval' => '5s',
'timeout' => '5s',
'retries' => 25,
],
'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}",
],
]

Check failure on line 283 in inc/composer/class-docker-compose-generator.php

View check run for this annotation

HM Linter / hmlinter

inc/composer/class-docker-compose-generator.php#L283

Each array item in a multi-line array declaration must end in a comma
Raw output
{
  "line": 283,
  "column": 13,
  "severity": "error",
  "message": "Each array item in a multi-line array declaration must end in a comma",
  "source": "WordPress.Arrays.CommaAfterArrayItem.NoComma"
}
];
}

/**
* Webgrind service container for viewing Xdebug profiles.
*
Expand Down Expand Up @@ -813,6 +860,10 @@
$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 +965,7 @@
'ignore-paths' => [],
'php' => '8.1',
'mysql' => '8.0',
'nodejs' => $modules['nodejs'] ?? false,
];

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