Allow setting build tool (npm/yarn) and next.js version. (#639)

* Allow setting build tool (npm/yarn) and next.js version.
This commit is contained in:
Thomas E Lackey 2023-11-10 17:44:25 -06:00 committed by GitHub
parent 042b413598
commit 414b887036
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 52 additions and 26 deletions

View File

@ -28,28 +28,15 @@ RUN \
# [Optional] Uncomment this section to install additional OS packages. # [Optional] Uncomment this section to install additional OS packages.
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& apt-get -y install --no-install-recommends jq gettext-base && apt-get -y install --no-install-recommends jq gettext-base moreutils
# [Optional] Uncomment if you want to install an additional version of node using nvm
# ARG EXTRA_NODE_VERSION=10
# RUN su node -c "source /usr/local/share/nvm/nvm.sh && nvm install ${EXTRA_NODE_VERSION}"
# We do this to get a yq binary from the published container, for the correct architecture we're building here
# COPY --from=docker.io/mikefarah/yq:latest /usr/bin/yq /usr/local/bin/yq
COPY /scripts /scripts
# [Optional] Uncomment if you want to install more global node modules # [Optional] Uncomment if you want to install more global node modules
# RUN su node -c "npm install -g <your-package-list-here>" # RUN su node -c "npm install -g <your-package-list-here>"
# RUN mkdir -p /config
# COPY ./config.yml /config
# Install simple web server for now (use nginx perhaps later)
# RUN yarn global add http-server
# Expose port for http # Expose port for http
EXPOSE 3000 EXPOSE 3000
COPY /scripts /scripts
# Default command sleeps forever so docker doesn't kill it # Default command sleeps forever so docker doesn't kill it
CMD ["/scripts/start-serving-app.sh"] CMD ["/scripts/start-serving-app.sh"]

View File

@ -1,4 +1,8 @@
FROM cerc/nextjs-base:local FROM cerc/nextjs-base:local
ARG CERC_NEXT_VERSION=latest
ARG CERC_BUILD_TOOL
WORKDIR /app WORKDIR /app
COPY . . COPY . .
RUN rm -rf node_modules build .next* RUN rm -rf node_modules build .next*

View File

@ -8,6 +8,15 @@ WORK_DIR="${1:-./}"
SRC_DIR="${2:-.next}" SRC_DIR="${2:-.next}"
TRG_DIR="${3:-.next-r}" TRG_DIR="${3:-.next-r}"
CERC_BUILD_TOOL="${CERC_BUILD_TOOL}"
if [ -z "$CERC_BUILD_TOOL" ]; then
if [ -f "yarn.lock" ]; then
CERC_BUILD_TOOL=npm
else
CERC_BUILD_TOOL=yarn
fi
fi
cd "${WORK_DIR}" || exit 1 cd "${WORK_DIR}" || exit 1
rm -rf "$TRG_DIR" rm -rf "$TRG_DIR"

View File

@ -4,8 +4,17 @@ if [ -n "$CERC_SCRIPT_DEBUG" ]; then
set -x set -x
fi fi
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) CERC_NEXT_VERSION="${CERC_NEXT_VERSION:-^14.0.2}"
CERC_BUILD_TOOL="${CERC_BUILD_TOOL}"
if [ -z "$CERC_BUILD_TOOL" ]; then
if [ -f "yarn.lock" ] && [ ! -f "package-lock.json" ]; then
CERC_BUILD_TOOL=yarn
else
CERC_BUILD_TOOL=npm
fi
fi
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
WORK_DIR="${1:-/app}" WORK_DIR="${1:-/app}"
cd "${WORK_DIR}" || exit 1 cd "${WORK_DIR}" || exit 1
@ -20,6 +29,7 @@ if [ $? -ne 0 ]; then
fi fi
js-beautify next.config.dist > next.config.js js-beautify next.config.dist > next.config.js
echo "" >> next.config.js
WEBPACK_REQ_LINE=$(grep -n "require([\'\"]webpack[\'\"])" next.config.js | cut -d':' -f1) WEBPACK_REQ_LINE=$(grep -n "require([\'\"]webpack[\'\"])" next.config.js | cut -d':' -f1)
if [ -z "$WEBPACK_REQ_LINE" ]; then if [ -z "$WEBPACK_REQ_LINE" ]; then
@ -58,7 +68,7 @@ if [ -n "$WEBPACK_CONF_LINE" ]; then
cat > next.config.js.3 <<EOF cat > next.config.js.3 <<EOF
$WEBPACK_CONF_VAR.plugins.push(new webpack.DefinePlugin(envMap)); $WEBPACK_CONF_VAR.plugins.push(new webpack.DefinePlugin(envMap));
EOF EOF
NEXT_SECTION_LINE=$((WEBPACK_CONF_LINE - 1)) NEXT_SECTION_LINE=$((WEBPACK_CONF_LINE))
elif [ -n "$ENV_LINE" ]; then elif [ -n "$ENV_LINE" ]; then
head -$(( ${ENV_LINE} - 1 )) next.config.js > next.config.js.2 head -$(( ${ENV_LINE} - 1 )) next.config.js > next.config.js.2
cat > next.config.js.3 <<EOF cat > next.config.js.3 <<EOF
@ -67,7 +77,7 @@ elif [ -n "$ENV_LINE" ]; then
return config; return config;
}, },
EOF EOF
NEXT_SECTION_ADJUSTMENT=2 NEXT_SECTION_ADJUSTMENT=1
NEXT_SECTION_LINE=$ENV_LINE NEXT_SECTION_LINE=$ENV_LINE
else else
echo "WARNING: Cannot find location to insert environment variable map in next.config.js" 1>&2 echo "WARNING: Cannot find location to insert environment variable map in next.config.js" 1>&2
@ -88,7 +98,14 @@ fi
cat package.dist | jq '.scripts.cerc_compile = "next experimental-compile"' | jq '.scripts.cerc_generate = "next experimental-generate"' > package.json cat package.dist | jq '.scripts.cerc_compile = "next experimental-compile"' | jq '.scripts.cerc_generate = "next experimental-generate"' > package.json
npm install || exit 1 CUR_NEXT_VERSION="`jq -r '.dependencies.next' package.json`"
npm run cerc_compile || exit 1
if [ "$CERC_NEXT_VERSION" != "keep" ] && [ "$CUR_NEXT_VERSION" != "$CERC_NEXT_VERSION" ]; then
echo "Changing 'next' version from $CUR_NEXT_VERSION to $CERC_NEXT_VERSION (set with --build-arg CERC_NEXT_VERSION)"
cat package.json | jq ".dependencies.next = \"$CERC_NEXT_VERSION\"" | sponge package.json
fi
$CERC_BUILD_TOOL install || exit 1
$CERC_BUILD_TOOL run cerc_compile || exit 1
exit 0 exit 0

View File

@ -5,6 +5,15 @@ fi
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
CERC_BUILD_TOOL="${CERC_BUILD_TOOL}"
if [ -z "$CERC_BUILD_TOOL" ]; then
if [ -f "yarn.lock" ] && [ ! -f "package-lock.json" ]; then
CERC_BUILD_TOOL=yarn
else
CERC_BUILD_TOOL=npm
fi
fi
CERC_WEBAPP_FILES_DIR="${CERC_WEBAPP_FILES_DIR:-/app}" CERC_WEBAPP_FILES_DIR="${CERC_WEBAPP_FILES_DIR:-/app}"
cd "$CERC_WEBAPP_FILES_DIR" cd "$CERC_WEBAPP_FILES_DIR"
@ -31,4 +40,4 @@ if [ "$CERC_NEXTJS_SKIP_GENERATE" != "true" ]; then
fi fi
fi fi
npm start . -p ${CERC_LISTEN_PORT:-3000} $CERC_BUILD_TOOL start . -p ${CERC_LISTEN_PORT:-3000}

View File

@ -24,7 +24,7 @@ git clone https://git.vdb.to/cerc-io/test-progressive-web-app.git $CERC_REPO_BAS
# Test webapp command execution # Test webapp command execution
$TEST_TARGET_SO build-webapp --source-repo $CERC_REPO_BASE_DIR/test-progressive-web-app $TEST_TARGET_SO build-webapp --source-repo $CERC_REPO_BASE_DIR/test-progressive-web-app
UUID=`uuidgen` CHECK="SPECIAL_01234567890_TEST_STRING"
set +e set +e
@ -34,7 +34,7 @@ wget -O test.before -m http://localhost:3000
docker remove -f $CONTAINER_ID docker remove -f $CONTAINER_ID
CONTAINER_ID=$(docker run -p 3000:3000 -e CERC_WEBAPP_DEBUG=$UUID -d cerc/test-progressive-web-app:local) CONTAINER_ID=$(docker run -p 3000:3000 -e CERC_WEBAPP_DEBUG=$CHECK -d cerc/test-progressive-web-app:local)
sleep 3 sleep 3
wget -O test.after -m http://localhost:3000 wget -O test.after -m http://localhost:3000
@ -43,7 +43,7 @@ docker remove -f $CONTAINER_ID
echo "###########################################################################" echo "###########################################################################"
echo "" echo ""
grep "$UUID" test.before > /dev/null grep "$CHECK" test.before > /dev/null
if [ $? -ne 1 ]; then if [ $? -ne 1 ]; then
echo "BEFORE: FAILED" echo "BEFORE: FAILED"
exit 1 exit 1
@ -51,7 +51,7 @@ else
echo "BEFORE: PASSED" echo "BEFORE: PASSED"
fi fi
grep "$UUID" test.after > /dev/null grep "$CHECK" test.after > /dev/null
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "AFTER: FAILED" echo "AFTER: FAILED"
exit 1 exit 1