From c088259e46844326660198fa6ee6afba5bc6be50 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 27 Feb 2022 20:10:10 +0100 Subject: [PATCH] Split dist file creation and version replacing from main build script --- .dockerignore | 1 + .github/workflows/publish-release.yml | 2 +- Dockerfile | 3 +- package.json | 3 +- scripts/build.js | 45 --------------------------- scripts/create-dist-file.js | 36 +++++++++++++++++++++ scripts/replace-version.js | 20 ++++++++++++ 7 files changed, 61 insertions(+), 49 deletions(-) create mode 100644 scripts/create-dist-file.js create mode 100644 scripts/replace-version.js diff --git a/.dockerignore b/.dockerignore index 1d878882..c118f1c6 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,4 +1,5 @@ ./.github +./.stryker-tmp ./build ./coverage ./node_modules diff --git a/.github/workflows/publish-release.yml b/.github/workflows/publish-release.yml index 351d4574..8306b7e7 100644 --- a/.github/workflows/publish-release.yml +++ b/.github/workflows/publish-release.yml @@ -16,7 +16,7 @@ jobs: with: node-version: 16.13 - name: Generate release assets - run: npm ci && npm run build ${GITHUB_REF#refs/tags/v} + run: npm ci && VERSION=${GITHUB_REF#refs/tags/v} npm run build:dist - name: Publish release with assets uses: docker://antonyurchenko/git-release:latest env: diff --git a/Dockerfile b/Dockerfile index 73d86942..ec0963c4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,8 +2,7 @@ FROM node:16.13-alpine as node COPY . /shlink-web-client ARG VERSION="latest" ENV VERSION ${VERSION} -RUN cd /shlink-web-client && \ - npm install && npm run build -- ${VERSION} --no-dist +RUN cd /shlink-web-client && npm ci && npm run build FROM nginx:1.21-alpine LABEL maintainer="Alejandro Celaya " diff --git a/package.json b/package.json index a9c799bf..71a49547 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "lint:js:fix": "npm run lint:js -- --fix", "start": "node scripts/start.js", "serve:build": "serve ./build", - "build": "node scripts/build.js", + "build": "node scripts/build.js && node scripts/replace-version.js", + "build:dist": "npm run build && node scripts/create-dist-file.js", "test": "node scripts/test.js --env=jsdom --colors --verbose", "test:coverage": "npm run test -- --coverage --coverageReporters=text --coverageReporters=text-summary", "test:ci": "npm run test:coverage -- --coverageReporters=clover", diff --git a/scripts/build.js b/scripts/build.js index 4ac2a6f9..ccfe012b 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -18,7 +18,6 @@ const chalk = require('chalk'); const fs = require('fs-extra'); const webpack = require('webpack'); const bfj = require('bfj'); -const AdmZip = require('adm-zip'); const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles'); const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages'); const FileSizeReporter = require('react-dev-utils/FileSizeReporter'); @@ -44,8 +43,6 @@ if (!checkRequiredFiles([ paths.appHtml, paths.appIndexJs ])) { const argvSliceStart = 2; const argv = process.argv.slice(argvSliceStart); const writeStatsJson = argv.includes('--stats'); -const withoutDist = argv.includes('--no-dist'); -const { version, hasVersion } = getVersionFromArgs(argv); // Generate configuration const config = configFactory('production'); @@ -84,7 +81,6 @@ checkBrowsers(paths.appPath, isInteractive) ); } else { console.log(chalk.green('Compiled successfully.\n')); - hasVersion && replaceVersionPlaceholder(version); } console.log('File sizes after gzip:\n'); @@ -103,7 +99,6 @@ checkBrowsers(paths.appPath, isInteractive) process.exit(1); }, ) - .then(() => hasVersion && !withoutDist && zipDist(version)) .catch((err) => { if (err && err.message) { console.log(err.message); @@ -185,43 +180,3 @@ function copyPublicFolder() { filter: (file) => file !== paths.appHtml, }); } - -function zipDist(version) { - const versionFileName = `./dist/shlink-web-client_${version}_dist.zip`; - - console.log(chalk.cyan(`Generating dist file for version ${chalk.bold(version)}...`)); - const zip = new AdmZip(); - - try { - if (fs.existsSync(versionFileName)) { - fs.unlink(versionFileName); - } - - zip.addLocalFolder('./build', `shlink-web-client_${version}_dist`); - zip.writeZip(versionFileName); - console.log(chalk.green('Dist file properly generated')); - } catch (e) { - console.log(chalk.red('An error occurred while generating dist file')); - console.log(e); - } - console.log(); -} - -function getVersionFromArgs(argv) { - const [ version ] = argv; - - return { version, hasVersion: !!version }; -} - -function replaceVersionPlaceholder(version) { - const staticJsFilesPath = './build/static/js'; - const versionPlaceholder = '%_VERSION_%'; - - const isMainFile = (file) => file.startsWith('main.') && file.endsWith('.js'); - const [ mainJsFile ] = fs.readdirSync(staticJsFilesPath).filter(isMainFile); - const filePath = `${staticJsFilesPath}/${mainJsFile}`; - const fileContent = fs.readFileSync(filePath, 'utf-8'); - const replaced = fileContent.replace(versionPlaceholder, version); - - fs.writeFileSync(filePath, replaced, 'utf-8'); -} diff --git a/scripts/create-dist-file.js b/scripts/create-dist-file.js new file mode 100644 index 00000000..b2ac89ce --- /dev/null +++ b/scripts/create-dist-file.js @@ -0,0 +1,36 @@ +/* eslint-disable no-console, @typescript-eslint/prefer-nullish-coalescing, @typescript-eslint/promise-function-async, @typescript-eslint/prefer-optional-chain */ + +// Do this as the first thing so that any code reading it knows the right env. +process.env.BABEL_ENV = 'production'; +process.env.NODE_ENV = 'production'; + +const chalk = require('chalk'); +const AdmZip = require('adm-zip'); +const fs = require('fs-extra'); + +function zipDist(version) { + const versionFileName = `./dist/shlink-web-client_${version}_dist.zip`; + + console.log(chalk.cyan(`Generating dist file for version ${chalk.bold(version)}...`)); + const zip = new AdmZip(); + + try { + if (fs.existsSync(versionFileName)) { + fs.unlink(versionFileName); + } + + zip.addLocalFolder('./build', `shlink-web-client_${version}_dist`); + zip.writeZip(versionFileName); + console.log(chalk.green('Dist file properly generated')); + } catch (e) { + console.log(chalk.red('An error occurred while generating dist file')); + console.log(e); + } + console.log(); +} + +const version = process.env.VERSION; + +if (version) { + zipDist(version); +} diff --git a/scripts/replace-version.js b/scripts/replace-version.js new file mode 100644 index 00000000..e2092e0c --- /dev/null +++ b/scripts/replace-version.js @@ -0,0 +1,20 @@ +const fs = require('fs-extra'); + +function replaceVersionPlaceholder(version) { + const staticJsFilesPath = './build/static/js'; + const versionPlaceholder = '%_VERSION_%'; + + const isMainFile = (file) => file.startsWith('main.') && file.endsWith('.js'); + const [ mainJsFile ] = fs.readdirSync(staticJsFilesPath).filter(isMainFile); + const filePath = `${staticJsFilesPath}/${mainJsFile}`; + const fileContent = fs.readFileSync(filePath, 'utf-8'); + const replaced = fileContent.replace(versionPlaceholder, version); + + fs.writeFileSync(filePath, replaced, 'utf-8'); +} + +const version = process.env.VERSION; + +if (version) { + replaceVersionPlaceholder(version); +}