From 366ccaa5770ebc40e293ca310b9b551a8f4f90c5 Mon Sep 17 00:00:00 2001 From: Jerico Aragon Date: Tue, 9 Jul 2024 17:35:52 +0800 Subject: [PATCH 01/15] Add NodeJS service to local-server --- .../class-docker-compose-generator.php | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/inc/composer/class-docker-compose-generator.php b/inc/composer/class-docker-compose-generator.php index 755decf8..17755455 100644 --- a/inc/composer/class-docker-compose-generator.php +++ b/inc/composer/class-docker-compose-generator.php @@ -125,6 +125,10 @@ protected function get_php_reusable() : array { 'tmp:/tmp', ]; + if ( $this->get_config()['nodejs'] ) { + $volumes[] = "{$this->root_dir}/node_modules:/var/www/html/node_modules"; + } + if ( $this->args['xdebug'] !== 'off' ) { $volumes[] = "{$this->config_dir}/xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini"; } @@ -237,6 +241,42 @@ protected function get_service_php() : array { ]; } + /** + * Get the NodeJS container service. + * + * @return array + */ + protected function get_service_nodejs() : array { + $config = $this->get_config(); + + return [ + 'nodejs' => [ + 'image' => 'node:21-bookworm-slim', + 'container_name' => "{$this->project_name}-nodejs", + 'ports' => [ + '3000', + ], + 'volumes' => [ + "../{$config['nodejs']['path']}/:/usr/src/app" + ], + 'working_dir' => '/usr/src/app', + 'command' => '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}", + ], + ] + ]; + } + /** * Webgrind service container for viewing Xdebug profiles. * @@ -813,6 +853,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', @@ -914,6 +958,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'] ?? [] ); From 9ddfe5ec9a5f3c4891169b9651cc4aac7487532b Mon Sep 17 00:00:00 2001 From: Jerico Aragon Date: Tue, 9 Jul 2024 17:44:45 +0800 Subject: [PATCH 02/15] Add Node.js documentation --- docs/nodejs.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 docs/nodejs.md diff --git a/docs/nodejs.md b/docs/nodejs.md new file mode 100644 index 00000000..c057157a --- /dev/null +++ b/docs/nodejs.md @@ -0,0 +1,27 @@ +# 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 refers to the relative path of the project's front-end code. + +This will make the application available at nodejs-my-project.altis.dev. From e505f0e138a69f0e3719aed8a9fb8cba37e5f93d Mon Sep 17 00:00:00 2001 From: Jerico Aragon Date: Tue, 9 Jul 2024 17:46:53 +0800 Subject: [PATCH 03/15] Remove unnecessary node_modules volume --- inc/composer/class-docker-compose-generator.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/inc/composer/class-docker-compose-generator.php b/inc/composer/class-docker-compose-generator.php index 17755455..5923a199 100644 --- a/inc/composer/class-docker-compose-generator.php +++ b/inc/composer/class-docker-compose-generator.php @@ -125,10 +125,6 @@ protected function get_php_reusable() : array { 'tmp:/tmp', ]; - if ( $this->get_config()['nodejs'] ) { - $volumes[] = "{$this->root_dir}/node_modules:/var/www/html/node_modules"; - } - if ( $this->args['xdebug'] !== 'off' ) { $volumes[] = "{$this->config_dir}/xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini"; } @@ -251,7 +247,7 @@ protected function get_service_nodejs() : array { return [ 'nodejs' => [ - 'image' => 'node:21-bookworm-slim', + 'image' => 'node:21-bookworm-slim', 'container_name' => "{$this->project_name}-nodejs", 'ports' => [ '3000', From dc97da5c283e982583254bab825745fbde08a485 Mon Sep 17 00:00:00 2001 From: Jerico Aragon Date: Tue, 9 Jul 2024 18:06:09 +0800 Subject: [PATCH 04/15] Allow setting of Node.js version --- docs/nodejs.md | 6 ++++-- inc/composer/class-docker-compose-generator.php | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/docs/nodejs.md b/docs/nodejs.md index c057157a..ce48b116 100644 --- a/docs/nodejs.md +++ b/docs/nodejs.md @@ -13,7 +13,8 @@ Node.js can be enabled in Local Server by adding `extra.altis.modules.local-serv "modules": { "local-server": { "nodejs": { - "path": "../altis-nodejs-skeleton" + "path": "../altis-nodejs-skeleton", + "version": "21.1" } } } @@ -22,6 +23,7 @@ Node.js can be enabled in Local Server by adding `extra.altis.modules.local-serv } ``` -path refers to the relative path of the project's front-end code. +`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. diff --git a/inc/composer/class-docker-compose-generator.php b/inc/composer/class-docker-compose-generator.php index 5923a199..37a0059d 100644 --- a/inc/composer/class-docker-compose-generator.php +++ b/inc/composer/class-docker-compose-generator.php @@ -245,9 +245,11 @@ protected function get_service_php() : array { protected function get_service_nodejs() : array { $config = $this->get_config(); + $version = (string) $this->get_config()['nodejs']['version'] ?? '20'; + return [ 'nodejs' => [ - 'image' => 'node:21-bookworm-slim', + 'image' => "node:{$version}-bookworm-slim", 'container_name' => "{$this->project_name}-nodejs", 'ports' => [ '3000', From 85c0b8746e2690bdf0f2a3a987fefc69e39719ea Mon Sep 17 00:00:00 2001 From: Jerico Aragon Date: Tue, 9 Jul 2024 18:22:09 +0800 Subject: [PATCH 05/15] Add healthcheck --- inc/composer/class-docker-compose-generator.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/inc/composer/class-docker-compose-generator.php b/inc/composer/class-docker-compose-generator.php index 37a0059d..998fbc63 100644 --- a/inc/composer/class-docker-compose-generator.php +++ b/inc/composer/class-docker-compose-generator.php @@ -249,7 +249,7 @@ protected function get_service_nodejs() : array { return [ 'nodejs' => [ - 'image' => "node:{$version}-bookworm-slim", + 'image' => "node:{$version}-bookworm", 'container_name' => "{$this->project_name}-nodejs", 'ports' => [ '3000', @@ -263,6 +263,15 @@ protected function get_service_nodejs() : array { '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', From e5e8c2d0481948366b20d9cb95e2fe04b68886a2 Mon Sep 17 00:00:00 2001 From: Jerico Aragon Date: Tue, 9 Jul 2024 18:22:22 +0800 Subject: [PATCH 06/15] Echo Node.js URL when startup completes --- inc/composer/class-command.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/inc/composer/class-command.php b/inc/composer/class-command.php index 275d82e6..c9f635d3 100644 --- a/inc/composer/class-command.php +++ b/inc/composer/class-command.php @@ -322,6 +322,13 @@ protected function start( InputInterface $input, OutputInterface $output ) { $output->writeln( 'Startup completed.' ); $output->writeln( 'To access your site visit: ' . $site_url . '' ); + if (static::get_composer_config()['nodejs'] ?? false) { + $tld = $this->get_project_tld(); + $subdomain = $this->get_project_subdomain(); + $hostname = $subdomain . '.' . $tld; + $output->writeln( 'To access your Node.js site visit: https://nodejs-' . $hostname . '' ); + } + $this->check_host_entries( $input, $output ); return 0; From 858158cced167bee9d987e6da31ae3b7d8d4f895 Mon Sep 17 00:00:00 2001 From: Jerico Aragon Date: Wed, 10 Jul 2024 12:59:55 +0800 Subject: [PATCH 07/15] Remove Node.js healthcheck --- inc/composer/class-docker-compose-generator.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/inc/composer/class-docker-compose-generator.php b/inc/composer/class-docker-compose-generator.php index 998fbc63..37a0059d 100644 --- a/inc/composer/class-docker-compose-generator.php +++ b/inc/composer/class-docker-compose-generator.php @@ -249,7 +249,7 @@ protected function get_service_nodejs() : array { return [ 'nodejs' => [ - 'image' => "node:{$version}-bookworm", + 'image' => "node:{$version}-bookworm-slim", 'container_name' => "{$this->project_name}-nodejs", 'ports' => [ '3000', @@ -263,15 +263,6 @@ protected function get_service_nodejs() : array { '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', From fbd0a6b61e1c1f8462ae288068113d44ba8d078d Mon Sep 17 00:00:00 2001 From: Jerico Aragon Date: Wed, 10 Jul 2024 13:01:31 +0800 Subject: [PATCH 08/15] Fix formatting --- docs/nodejs.md | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/docs/nodejs.md b/docs/nodejs.md index ce48b116..4493755d 100644 --- a/docs/nodejs.md +++ b/docs/nodejs.md @@ -8,18 +8,18 @@ Node.js can be enabled in Local Server by adding `extra.altis.modules.local-serv ```json { - "extra": { - "altis": { - "modules": { - "local-server": { - "nodejs": { - "path": "../altis-nodejs-skeleton", - "version": "21.1" - } - } - } - } - } + "extra":{ + "altis":{ + "modules":{ + "local-server":{ + "nodejs":{ + "path":"../altis-nodejs-skeleton", + "version":"21.1" + } + } + } + } + } } ``` From f7b893b3f8e9b124b7bec042b40bc9742c5431ff Mon Sep 17 00:00:00 2001 From: Jerico Aragon Date: Wed, 10 Jul 2024 13:11:28 +0800 Subject: [PATCH 09/15] Read from package.json to determine Node.js version --- inc/composer/class-docker-compose-generator.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/inc/composer/class-docker-compose-generator.php b/inc/composer/class-docker-compose-generator.php index 37a0059d..5ae86089 100644 --- a/inc/composer/class-docker-compose-generator.php +++ b/inc/composer/class-docker-compose-generator.php @@ -245,7 +245,9 @@ protected function get_service_php() : array { protected function get_service_nodejs() : array { $config = $this->get_config(); - $version = (string) $this->get_config()['nodejs']['version'] ?? '20'; + // 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' => [ From c2fee48dcbb87d0813b40168db8888f5f5d6e759 Mon Sep 17 00:00:00 2001 From: Jerico Aragon Date: Wed, 10 Jul 2024 13:21:23 +0800 Subject: [PATCH 10/15] Update Node.js documentation --- docs/nodejs.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/docs/nodejs.md b/docs/nodejs.md index 4493755d..1d3ebf3a 100644 --- a/docs/nodejs.md +++ b/docs/nodejs.md @@ -13,8 +13,7 @@ Node.js can be enabled in Local Server by adding `extra.altis.modules.local-serv "modules":{ "local-server":{ "nodejs":{ - "path":"../altis-nodejs-skeleton", - "version":"21.1" + "path":"../altis-nodejs-skeleton" } } } @@ -24,6 +23,12 @@ Node.js can be enabled in Local Server by adding `extra.altis.modules.local-serv ``` `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. +## 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`. From de954686d599e79198dbcbb16aed99dc092f8a36 Mon Sep 17 00:00:00 2001 From: Jerico Aragon Date: Wed, 10 Jul 2024 13:57:03 +0800 Subject: [PATCH 11/15] Add env vars in Node.js container --- inc/composer/class-docker-compose-generator.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/inc/composer/class-docker-compose-generator.php b/inc/composer/class-docker-compose-generator.php index 5ae86089..3c7fb179 100644 --- a/inc/composer/class-docker-compose-generator.php +++ b/inc/composer/class-docker-compose-generator.php @@ -273,6 +273,10 @@ protected function get_service_nodejs() : array { "traefik.frontend.rule=HostRegexp:nodejs-{$this->hostname}", "traefik.domain=nodejs-{$this->hostname}", ], + 'environment' => [ + 'ALTIS_ENVIRONMENT_NAME' => $this->project_name, + 'ALTIS_ENVIRONMENT_TYPE' => 'local', + ], ] ]; } From 63ae1a1f4e5133c78acd3625dd6aeceab20905de Mon Sep 17 00:00:00 2001 From: Jerico Aragon Date: Wed, 10 Jul 2024 14:01:29 +0800 Subject: [PATCH 12/15] Fix linting issues --- inc/composer/class-command.php | 2 +- inc/composer/class-docker-compose-generator.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/inc/composer/class-command.php b/inc/composer/class-command.php index c9f635d3..36994fc5 100644 --- a/inc/composer/class-command.php +++ b/inc/composer/class-command.php @@ -322,7 +322,7 @@ protected function start( InputInterface $input, OutputInterface $output ) { $output->writeln( 'Startup completed.' ); $output->writeln( 'To access your site visit: ' . $site_url . '' ); - if (static::get_composer_config()['nodejs'] ?? false) { + if ( static::get_composer_config()['nodejs'] ?? false ) { $tld = $this->get_project_tld(); $subdomain = $this->get_project_subdomain(); $hostname = $subdomain . '.' . $tld; diff --git a/inc/composer/class-docker-compose-generator.php b/inc/composer/class-docker-compose-generator.php index 3c7fb179..87b5d919 100644 --- a/inc/composer/class-docker-compose-generator.php +++ b/inc/composer/class-docker-compose-generator.php @@ -245,7 +245,7 @@ protected function get_service_php() : 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 + // 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'; @@ -257,7 +257,7 @@ protected function get_service_nodejs() : array { '3000', ], 'volumes' => [ - "../{$config['nodejs']['path']}/:/usr/src/app" + "../{$config['nodejs']['path']}/:/usr/src/app", ], 'working_dir' => '/usr/src/app', 'command' => 'npm run dev', @@ -277,7 +277,7 @@ protected function get_service_nodejs() : array { 'ALTIS_ENVIRONMENT_NAME' => $this->project_name, 'ALTIS_ENVIRONMENT_TYPE' => 'local', ], - ] + ], ]; } From d100534ff79164b506cfdbad7a36e45b0c5c57ea Mon Sep 17 00:00:00 2001 From: Jerico Aragon Date: Wed, 10 Jul 2024 14:13:47 +0800 Subject: [PATCH 13/15] Install node packages before running dev --- inc/composer/class-docker-compose-generator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/composer/class-docker-compose-generator.php b/inc/composer/class-docker-compose-generator.php index 87b5d919..317a843e 100644 --- a/inc/composer/class-docker-compose-generator.php +++ b/inc/composer/class-docker-compose-generator.php @@ -260,7 +260,7 @@ protected function get_service_nodejs() : array { "../{$config['nodejs']['path']}/:/usr/src/app", ], 'working_dir' => '/usr/src/app', - 'command' => 'npm run dev', + 'command' => 'sh -c "npm install && npm run dev"', 'networks' => [ 'proxy', 'default', From 556d894c4d21fb9e58317cb43889f50870d53d6d Mon Sep 17 00:00:00 2001 From: Jerico Aragon <1062325+jerico@users.noreply.github.com> Date: Wed, 17 Jul 2024 15:33:20 +0800 Subject: [PATCH 14/15] Update docs/nodejs.md Co-authored-by: Mike Little --- docs/nodejs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/nodejs.md b/docs/nodejs.md index 1d3ebf3a..96c90bf3 100644 --- a/docs/nodejs.md +++ b/docs/nodejs.md @@ -22,7 +22,7 @@ Node.js can be enabled in Local Server by adding `extra.altis.modules.local-serv } ``` -`path` refers to the relative path of the project's front-end code. +`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`. From 87c188fe434acb1f803a68651c63a895618c1709 Mon Sep 17 00:00:00 2001 From: Jerico Aragon Date: Wed, 17 Jul 2024 16:39:34 +0800 Subject: [PATCH 15/15] Add nodejs entry in logs function --- inc/composer/class-command.php | 1 + 1 file changed, 1 insertion(+) diff --git a/inc/composer/class-command.php b/inc/composer/class-command.php index 36994fc5..9eebfdd6 100644 --- a/inc/composer/class-command.php +++ b/inc/composer/class-command.php @@ -577,6 +577,7 @@ protected function logs( InputInterface $input, OutputInterface $output ) { 'redis', 's3', 'xray', + 'nodejs', ], 0 );