Skip to content

feat(commerce): daff.json specifies driver version for angular.json #2

feat(commerce): daff.json specifies driver version for angular.json

feat(commerce): daff.json specifies driver version for angular.json #2

name: Daffodil Daff Config
on:
pull_request:
branches:
- develop
workflow_call:
inputs:
ref:
required: false
type: string
node-version:
required: false
type: string
default: '22.21.x'
angular-version:
required: false
type: string
description: "Angular version constraint (e.g. '^20'). Defaults to the @angular/core version in package.json."
secrets:
NX_KEY:
required: true
AZURE_NX_CACHE_READONLY_CONNECTION_STRING:
required: true
env:
WORK_DIR: /tmp/daffodil-daff-cli-test
TEST_VERSION: 0.0.0-test.${{ github.run_id }}
REGISTRY_PORT: 4873
NX_KEY: ${{ secrets.NX_KEY }}
jobs:
test:
name: "daff.json (${{ matrix.driver }})"
runs-on: ubuntu-latest
services:
verdaccio:
image: verdaccio/verdaccio
ports:
- 4873:4873
options: >-
--health-cmd "node -e \"require('http').get('http://localhost:4873/-/ping',r=>process.exit(r.statusCode===200?0:1)).on('error',()=>process.exit(1))\""
--health-interval 5s
--health-timeout 3s
--health-retries 10
strategy:
fail-fast: false
matrix:
driver: [magento, demo]
steps:
- uses: actions/checkout@v6
with:
fetch-depth: 1
ref: ${{ inputs.ref && inputs.ref || github.sha }}
- name: Resolve Angular version
id: angular-version
env:
INPUT_VERSION: ${{ inputs.angular-version }}
run: |
if [ -n "$INPUT_VERSION" ]; then
VERSION="$INPUT_VERSION"
else
VERSION=$(jq -r '.dependencies["@angular/core"]' package.json | sed -E 's/\.[0-9]+\.[0-9]+//g')
fi
echo "version=${VERSION}" >> "$GITHUB_OUTPUT"
- uses: graycoreio/github-actions/setup-node@main
with:
node-version: ${{ inputs.node-version }}
use-stamp-cache: true
working-directory: ${{ github.workspace }}
- uses: graycoreio/daffodil/.github/actions/setup-nx-remote-cache@develop
with:
cache-mode: read-only
read-only-azure-connection-string: ${{ secrets.AZURE_NX_CACHE_READONLY_CONNECTION_STRING }}
event-name: 'manual-setup'
- name: Authenticate with Verdaccio
run: |
REGISTRY_URL="http://localhost:${{ env.REGISTRY_PORT }}"
TOKEN=$(curl -s -X PUT ${REGISTRY_URL}/-/user/org.couchdb.user:ci \
-H "Content-Type: application/json" \
-d '{"name":"ci","password":"ci-password","type":"user"}' | jq -r '.token')
if [ -z "$TOKEN" ] || [ "$TOKEN" = "null" ]; then echo "ERROR: Failed to obtain Verdaccio token" && exit 1; fi
echo "@daffodil:registry=${REGISTRY_URL}" >> "$HOME/.npmrc"
echo "//localhost:${{ env.REGISTRY_PORT }}/:_authToken=${TOKEN}" >> "$HOME/.npmrc"
- name: Set test version for commerce package
run: |
sed -i "s/version = '.*'/version = '${{ env.TEST_VERSION }}'/" \
tools/schematics/ng-add/generators/version.ts
- name: Build @daffodil/* packages
run: npx nx run @daffodil/commerce:build
- name: Set test version in root
run: npm version "${{ env.TEST_VERSION }}" --no-git-tag-version
- uses: graycoreio/github-actions/set-versions-from-root@main
- name: Compute @daffodil/commerce dependencies
id: daff-deps
run: |
PACKAGES=$(npx nx graph --focus=@daffodil/commerce --file=stdout | \
jq -r '.graph.dependencies["@daffodil/commerce"] | map(.target) | join(",")')
echo "packages=${PACKAGES}" >> "$GITHUB_OUTPUT"
- name: Publish @daffodil/* packages to Verdaccio
run: |
npx nx run-many \
-t publish \
--projects="${{ steps.daff-deps.outputs.packages }},@daffodil/commerce"
- name: Compute test directory
id: test-dir
run: |
DIR_NAME="test-daff-cli-${{ matrix.driver }}"
echo "name=${DIR_NAME}" >> "$GITHUB_OUTPUT"
echo "path=${{ env.WORK_DIR }}/${DIR_NAME}" >> "$GITHUB_OUTPUT"
- name: Restore cached base app
id: cache-base-app
uses: actions/cache/restore@v4
with:
path: ${{ steps.test-dir.outputs.path }}
key: commerce-base-app-scss-standalone-${{ steps.angular-version.outputs.version }}
- name: Scaffold test app
if: steps.cache-base-app.outputs.cache-hit != 'true'
env:
DIR_NAME: "${{ steps.test-dir.outputs.name }}"
run: |
rm -rf "${{ steps.test-dir.outputs.path }}"
mkdir -p "${{ env.WORK_DIR }}"
cd "${{ env.WORK_DIR }}"
npx -y @angular/cli@${{ steps.angular-version.outputs.version }} new "$DIR_NAME" \
--style=scss \
--skip-tests \
--defaults
- name: Cache base app
if: steps.cache-base-app.outputs.cache-hit != 'true'
uses: actions/cache/save@v4
with:
path: ${{ steps.test-dir.outputs.path }}
key: commerce-base-app-scss-standalone-${{ steps.angular-version.outputs.version }}
- name: Add schematic
working-directory: ${{ steps.test-dir.outputs.path }}
run: |
CI=true npx ng add @daffodil/commerce@${{ env.TEST_VERSION }} \
--driver=${{ matrix.driver }} \
--is-new-project \
--defaults \
--skip-confirmation
- name: Verify daff.json was generated
working-directory: ${{ steps.test-dir.outputs.path }}
run: |
if [ ! -f daff.json ]; then
echo "ERROR: daff.json was not generated by the schematic" && exit 1
fi
PROJECT_NAME="${{ steps.test-dir.outputs.name }}"
VERSION=$(jq -r ".projects[\"${PROJECT_NAME}\"].drivers.magento" daff.json)
if [ "$VERSION" = "null" ] || [ -z "$VERSION" ]; then
echo "ERROR: daff.json is missing projects.${PROJECT_NAME}.drivers.magento" && cat daff.json && exit 1
fi
echo "daff.json generated with magento version ${VERSION}"
- name: Verify schematic wrote the production condition to angular.json
working-directory: ${{ steps.test-dir.outputs.path }}
run: |
PROJECT_NAME="${{ steps.test-dir.outputs.name }}"
VERSION=$(jq -r ".projects[\"${PROJECT_NAME}\"].drivers.magento" daff.json)
CONDITIONS=$(jq -c ".projects[\"${PROJECT_NAME}\"].architect.build.configurations.production.conditions" angular.json)
EXPECTED_ENTRY="magento-${VERSION}"
if ! echo "$CONDITIONS" | jq -e --arg c "$EXPECTED_ENTRY" 'index($c)' > /dev/null; then
echo "ERROR: expected condition ${EXPECTED_ENTRY} in ${CONDITIONS}" && exit 1
fi
echo "angular.json contains ${EXPECTED_ENTRY}"
- name: Verify daff sync re-applies a revised daff.json
working-directory: ${{ steps.test-dir.outputs.path }}
run: |
PROJECT_NAME="${{ steps.test-dir.outputs.name }}"
NEW_VERSION="2.4.99"
jq ".projects[\"${PROJECT_NAME}\"].drivers.magento = \"${NEW_VERSION}\"" \
daff.json > daff.json.tmp && mv daff.json.tmp daff.json
npx daff sync
CONDITIONS=$(jq -c ".projects[\"${PROJECT_NAME}\"].architect.build.configurations.production.conditions" angular.json)
EXPECTED="[\"magento-${NEW_VERSION}\"]"
if [ "$CONDITIONS" != "$EXPECTED" ]; then
echo "ERROR: expected conditions ${EXPECTED}, got ${CONDITIONS}" && exit 1
fi
echo "daff sync wrote ${CONDITIONS} to angular.json after revision"