From b4b289748aff7c7225296132288ab7792f64da77 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Sun, 12 Mar 2023 12:23:25 +0000 Subject: [PATCH] feat: add initial `node` support Refs: #5 --- .../Command/BuildConfigurationCommand.php | 14 +++++++++++++ src/Enum/Language.php | 1 + src/Validator/ConfigurationValidator.php | 13 ++++++++++-- templates/docker-compose.yaml.twig | 15 ++++++++++++++ templates/env.example.twig | 4 ++++ templates/node/.yarnrc.twig | 3 +++ templates/node/Dockerfile.twig | 20 +++++++++++++++++++ 7 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 templates/node/.yarnrc.twig create mode 100644 templates/node/Dockerfile.twig diff --git a/src/Console/Command/BuildConfigurationCommand.php b/src/Console/Command/BuildConfigurationCommand.php index 6333633..20b0956 100644 --- a/src/Console/Command/BuildConfigurationCommand.php +++ b/src/Console/Command/BuildConfigurationCommand.php @@ -101,6 +101,11 @@ final class BuildConfigurationCommand extends Command $this->filesToGenerate->push(['php/docker-entrypoint-php', 'tools/docker/images/php/root/usr/local/bin/docker-entrypoint-php']); } + if (self::isNode(Arr::get($configurationData, 'language'))) { + $this->filesToGenerate->push(['node/.yarnrc', '.yarnrc']); + $this->filesToGenerate->push(['node/Dockerfile', 'Dockerfile']); + } + if (self::isCaddy(Arr::get($configurationData, 'web.type'))) { $this->filesystem->mkdir("{$this->outputDir}/tools/docker/images/web/root/etc/caddy"); $this->filesToGenerate->push(['web/caddy/Caddyfile', 'tools/docker/images/web/root/etc/caddy/Caddyfile']); @@ -166,6 +171,15 @@ final class BuildConfigurationCommand extends Command return strtoupper($webServer) === WebServer::NGINX->name; } + private static function isNode(?string $language): bool + { + if (is_null($language)) { + return false; + } + + return strtoupper($language) === Language::NODE->name; + } + private static function isPhp(?string $language): bool { if (is_null($language)) { diff --git a/src/Enum/Language.php b/src/Enum/Language.php index c236db4..0312c84 100644 --- a/src/Enum/Language.php +++ b/src/Enum/Language.php @@ -6,5 +6,6 @@ namespace OliverDaviesLtd\BuildConfigs\Enum; enum Language { + case NODE; case PHP; } diff --git a/src/Validator/ConfigurationValidator.php b/src/Validator/ConfigurationValidator.php index 28a8c3e..ce4b1a6 100644 --- a/src/Validator/ConfigurationValidator.php +++ b/src/Validator/ConfigurationValidator.php @@ -28,13 +28,20 @@ final class ConfigurationValidator implements ValidatorInterface 'language' => [ new Assert\NotNull(), new Assert\Type('string'), - new Assert\Choice(['node', 'php']), + new Assert\Choice([ + 'node', + 'php', + ]), ], 'type' => [ new Assert\NotNull(), new Assert\Type('string'), - new Assert\Choice(['drupal-project', 'fractal', 'php-library']), + new Assert\Choice([ + 'drupal-project', + 'fractal', + 'php-library', + ]), ], 'project_root' => [ @@ -53,6 +60,8 @@ final class ConfigurationValidator implements ValidatorInterface // TODO: this should be a boolean if present. 'justfile' => new Assert\Optional(), + 'node' => new Assert\Optional(), + 'php' => new Assert\Optional(), 'web' => new Assert\Optional(), diff --git a/templates/docker-compose.yaml.twig b/templates/docker-compose.yaml.twig index 4e5c9a5..2458904 100644 --- a/templates/docker-compose.yaml.twig +++ b/templates/docker-compose.yaml.twig @@ -40,6 +40,7 @@ services: profiles: [web] {% endif %} +{% if "php" == language %} php: <<: *default-app build: @@ -52,6 +53,20 @@ services: - database {% endif -%} profiles: [php] +{% elseif "node" == language %} + node: + <<: *default-app + build: + context: . + target: build + volumes: + - .:{{ project_root }} + {% if "database" in dockerCompose.services|keys -%} + depends_on: + - database + {% endif -%} + profiles: [node] +{% endif %} {% if "database" in dockerCompose.services|keys %} database: diff --git a/templates/env.example.twig b/templates/env.example.twig index 9b3e8eb..2b259cf 100644 --- a/templates/env.example.twig +++ b/templates/env.example.twig @@ -4,7 +4,11 @@ export DOCKER_UID=1000 {% if dockerCompose %} export COMPOSE_PROJECT_NAME={{ name }} +{% if "php" == language %} export COMPOSE_PROFILES=web,php,database +{% elseif "node" == language %} +export COMPOSE_PROFILES=node +{% endif %} export DOCKER_WEB_VOLUME=.:{{ project_root }} {% endif %} diff --git a/templates/node/.yarnrc.twig b/templates/node/.yarnrc.twig new file mode 100644 index 0000000..689960c --- /dev/null +++ b/templates/node/.yarnrc.twig @@ -0,0 +1,3 @@ +# {{ managedText | raw }} + +--modules-folder /node_modules diff --git a/templates/node/Dockerfile.twig b/templates/node/Dockerfile.twig new file mode 100644 index 0000000..fc5bf8f --- /dev/null +++ b/templates/node/Dockerfile.twig @@ -0,0 +1,20 @@ +FROM node:{{ node.version }} AS base + +WORKDIR {{ project_root }} + +RUN mkdir /node_modules \ + && chown node:node -R {{ project_root }} /node_modules + +COPY --chown=node:node package*.json *yarn* {{ project_root }} + +USER node + +################################################################################ + +FROM base AS build + +RUN yarn install --frozen-lockfile + +COPY --chown=node:node . . + +CMD ["bash"]