From 709127eac26e8eecbb7d38667937864da8c2a79e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steven=20Weing=C3=A4rtner?= Date: Fri, 6 Dec 2024 20:44:44 +0000 Subject: [PATCH] Make different run environments more flexible - Differentiate between npm / yarn - Take lock files into consideration - Use package.json start script to give more flexibility on how to start the app --- digital_alchemy/Dockerfile | 8 +++-- digital_alchemy/config.yaml | 6 ++-- digital_alchemy/install.sh | 30 ------------------- digital_alchemy/run.sh | 58 ++++++++++++++++++++++--------------- 4 files changed, 43 insertions(+), 59 deletions(-) delete mode 100755 digital_alchemy/install.sh diff --git a/digital_alchemy/Dockerfile b/digital_alchemy/Dockerfile index eaf71c3..8342a36 100644 --- a/digital_alchemy/Dockerfile +++ b/digital_alchemy/Dockerfile @@ -1,8 +1,12 @@ ARG BUILD_FROM FROM $BUILD_FROM -COPY install.sh / -RUN /install.sh +COPY --from=node:22-alpine /usr/local/bin /usr/local/bin +COPY --from=node:22-alpine /usr/local/lib/node_modules /usr/local/lib/node_modules +COPY --from=oven/bun:alpine /usr/local/bin /usr/local/bin + +RUN rm -f /usr/local/bin/yarn /usr/local/bin/yarnpkg \ + && npm i -g yarn # Copy data for add-on COPY run.sh / diff --git a/digital_alchemy/config.yaml b/digital_alchemy/config.yaml index 6424999..a45f7e6 100644 --- a/digital_alchemy/config.yaml +++ b/digital_alchemy/config.yaml @@ -14,16 +14,14 @@ arch: options: app_root: /share/digital_alchemy - main: src/main.ts node_env: production - run_mode: tsx + environment: node_npm packages: [] schema: app_root: str - main: str node_env: str - run_mode: list(node|tsx|bun) + environment: list(node_npm|node_yarn|bun|deno) packages: - str diff --git a/digital_alchemy/install.sh b/digital_alchemy/install.sh deleted file mode 100755 index 7ce9f59..0000000 --- a/digital_alchemy/install.sh +++ /dev/null @@ -1,30 +0,0 @@ -#!/bin/sh -ARCH=$(uname -m) - -## 1) Bun -# See issue for details on why this exists / when it will be replaced with something more sane -# https://github.com/oven-sh/bun/issues/5545#issuecomment-1722461083 -if [ "$(uname -m)" = "aarch64" ]; then - # aarch64 - wget https://raw.githubusercontent.com/squishyu/alpine-pkg-glibc-aarch64-bin/master/glibc-2.26-r1.apk - apk add --no-cache --allow-untrusted --force-overwrite glibc-2.26-r1.apk - rm glibc-2.26-r1.apk -else - # x86_64 - wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.28-r0/glibc-2.28-r0.apk - wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub - apk add --no-cache --force-overwrite glibc-2.28-r0.apk - rm glibc-2.28-r0.apk -fi - -## 2) Node -if [ "$ARCH" = "x86_64" ]; then - NODE_VERSION=22.10.0 - apk add --no-cache curl - curl -fsSL https://unofficial-builds.nodejs.org/download/release/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64-musl.tar.xz | tar -xJf - -C /usr/local --strip-components=1; -else - apk add --no-cache curl nodejs npm -fi - -## 3) Install runtimes -npm i -g tsx nodemon bun diff --git a/digital_alchemy/run.sh b/digital_alchemy/run.sh index d7a2d4e..8ecee33 100644 --- a/digital_alchemy/run.sh +++ b/digital_alchemy/run.sh @@ -12,38 +12,50 @@ if bashio::config.has_value 'packages'; then done fi - # Fetch configuration options APP_ROOT=$(bashio::config 'app_root') -APP_MAIN=$(bashio::config 'main') -RUN_MODE=$(bashio::config 'run_mode') +ENVIRONMENT=$(bashio::config 'environment') + +function prepareStart() { + if [[ -z $(jq -r '.scripts.start // empty' 'package.json') ]]; then + bashio::exit.nok "No start script found in package.json" + fi + + PACKAGE_NAME=$(jq -r '.name' package.json) + bashio::log.info "Starting ${PACKAGE_NAME}..." +} # Navigate to the app root cd "${APP_ROOT}" || bashio::exit.nok "Could not navigate to application root: ${APP_ROOT}" # Validate package.json exists if [[ ! -f "package.json" ]]; then - bashio::exit.nok "package.json not found in APP_ROOT:'${APP_ROOT}'" + bashio::exit.nok "package.json not found in APP_ROOT: '${APP_ROOT}'" fi - if [[ "${RUN_MODE}" == "bun" ]]; then - # npm install -g bun - bun --version - bun install - bun run "$APP_MAIN" -else - corepack enable && corepack prepare yarn@stable --activate - yarn config set compressionLevel mixed - yarn config set nodeLinker node-modules - yarn install - - # Extract package name from package.json - PACKAGE_NAME=$(jq -r '.name' package.json) - bashio::log.info "Starting ${PACKAGE_NAME}..." - - # Check run_mode and execute the corresponding command - if [[ "${RUN_MODE}" == "tsx" ]]; then - npx tsx "$APP_MAIN" +if [[ "${ENVIRONMENT}" == "node_npm" ]]; then + bashio::log.info "Installing dependencies..." + if [[ -f "package-lock.json" ]]; then + npm ci else - node "$APP_MAIN" + npm install fi + + prepareStart + bashio::log.info " ...running npm start" + + npm start +elif [[ "${ENVIRONMENT}" == "node_yarn" ]]; then + bashio::log.info "Installing dependencies..." + yarn install + + prepareStart + bashio::log.info " ...running yarn start" + yarn start +elif [[ "${ENVIRONMENT}" == "bun" ]]; then + bashio::log.info "Installing dependencies..." + bun install --frozen-lockfile --production + + prepareStart + bashio::log.info " ...running bun run start" + bun run start fi