From 945a233efc890da70801c622adb89770766840bf Mon Sep 17 00:00:00 2001 From: afonso Date: Thu, 22 Feb 2024 16:44:57 +0000 Subject: [PATCH] CDE-40 chore: Add build demo step --- Dockerfile | 7 +++++-- build_demo.js | 38 ++++++++++++++++++++++++++++++++++++++ package.json | 3 +++ 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 build_demo.js diff --git a/Dockerfile b/Dockerfile index 2d3a676..0f41f2a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,12 @@ ARG PARENT=ubuntu/nginx ARG NODE_PARENT=node:18.12.1 -ARG VITE_API_KEY + + FROM ${NODE_PARENT} as build -ENV APP_DIR=/app +ARG VITE_API_KEY ENV VITE_API_KEY=$VITE_API_KEY +ENV APP_DIR=/app WORKDIR ${APP_DIR} @@ -15,6 +17,7 @@ RUN npm install # user node ADD --chown=node:node . ${APP_DIR} +RUN npm run build:demo RUN npm run build FROM ${PARENT} diff --git a/build_demo.js b/build_demo.js new file mode 100644 index 0000000..ae39980 --- /dev/null +++ b/build_demo.js @@ -0,0 +1,38 @@ +/* eslint-env node */ + +import dotenv from 'dotenv'; +import replace from 'replace-in-file'; + +dotenv.config(); // Load environment variables + +const getReplacements = () => { + // Filter only variables starting with VITE_ + const viteEnvVars = Object.keys(process.env).filter(key => key.startsWith('VITE_')); + + // Create replacement options for each VITE_ variable + return viteEnvVars.map(key => ({ + from: new RegExp(`import.meta.env.${key}`, 'g'), + to: JSON.stringify(process.env[key]), + })); +}; + +const options = { + files: 'demo/*.js', // Target all .js files in dist directory + from: [], + to: [], +}; + +// Generate and apply replacements +const replacements = getReplacements(); +replacements.forEach(replacement => { + options.from.push(replacement.from); + options.to.push(replacement.to); +}); + +replace(options) + .then(results => { + console.log(results.filter(r => r.hasChanged).length, 'files changed.') + }) + .catch(error => { + console.error('Error occurred:', error); + }); diff --git a/package.json b/package.json index 9bf0f5d..111d8b5 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "scripts": { "dev": "vite", "build": "tsc --p ./tsconfig-build.json && vite build && cp -f dist/cde-mapper.js demo", + "build:demo": "node build_demo.js", "lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0", "preview": "vite preview", "prepublishOnly": "npm run build" @@ -32,10 +33,12 @@ "@typescript-eslint/eslint-plugin": "^6.14.0", "@typescript-eslint/parser": "^6.14.0", "@vitejs/plugin-react": "^4.2.1", + "dotenv": "^16.4.5", "eslint": "^8.55.0", "eslint-plugin-react-hooks": "^4.6.0", "eslint-plugin-react-refresh": "^0.4.5", "react-dropzone": "^14.2.3", + "replace-in-file": "^7.1.0", "typescript": "^5.2.2", "vite": "^5.0.8", "vite-plugin-dts": "^3.6.4"