diff --git a/.dockerignore b/.dockerignore index f2ce3d6..8221834 100644 --- a/.dockerignore +++ b/.dockerignore @@ -10,7 +10,8 @@ coverage/ *.test.ts # Version control -.git/ +# The .git dir is intentionally included in the build context because the build stage needs it to derive a version tag. +# The Dockerfile only copies the build stage output from /app/dist into the final image, so .git is excluded that way. .gitignore # CI/CD diff --git a/package.json b/package.json index 553b9b9..12b34c1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,4 @@ { - "name": "fdp-client-v3", - "version": "2.0.0-dev", "private": true, "type": "module", "scripts": { diff --git a/vite.config.ts b/vite.config.ts index dbc5f89..2898ccd 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,5 +1,7 @@ /// +import { strict as assert } from 'node:assert' +import { spawnSync } from 'node:child_process' import * as fs from 'node:fs' import path from 'node:path' import { fileURLToPath, URL } from 'node:url' @@ -9,8 +11,6 @@ import { defineConfig, HtmlTagDescriptor, ViteDevServer } from 'vite' import vueDevTools from 'vite-plugin-vue-devtools' import svgLoader from 'vite-svg-loader' -import { version } from './package.json' - // https://vite.dev/config/ export default defineConfig({ // plugin order matters @@ -21,7 +21,7 @@ export default defineConfig({ }, }, define: { - __APP_VERSION__: JSON.stringify(version), + __APP_VERSION__: JSON.stringify(gitDescribeVersion()), __APP_BUILT_AT__: JSON.stringify(new Date().toISOString()), }, // vitest options @@ -65,6 +65,25 @@ function clientConfigOverridePlugin() { } } +/** + * Returns a version string obtained from `git describe`, in long format, for example, `'v1.2.3-0-gfc7760f'`. + * Fails intentionally if the `git describe` call fails or if its output is falsy. + */ +function gitDescribeVersion(): string { + const { stdout, stderr, status } = spawnSync( + 'git', + [ + 'describe', + '--tags', // include the lightweight tags created by e.g. github + '--always', // use abbreviated commit hash if there are no tags at all + '--long', // use long format even if the current commit is tagged ('v1.2.3-0-gfc7760f' instead of 'v1.2.3') + ], + { encoding: 'utf8' }, + ) + assert(stdout, `git describe call failed (exit code: ${status}, stderr: ${stderr?.trim()})`) + return stdout.trim() +} + /** * Appends a `` tag for a custom CSS `stylesheet` to the `` section of `index.html`. * This should be loaded after the default CSS files to allow proper overriding.