diff --git a/packages/app/next.config.js b/packages/app/next.config.js index ca5e167c16..33cd62ac7f 100644 --- a/packages/app/next.config.js +++ b/packages/app/next.config.js @@ -1,10 +1,23 @@ -const withTM = require('next-transpile-modules')(['@toeverything/pathfinder-logger']); +// @ts-check +const withTM = require('next-transpile-modules')([ + '@toeverything/pathfinder-logger', +]); + +const { getGitVersion, getCommitHash } = require('./scripts/gitInfo'); /** @type {import('next').NextConfig} */ const nextConfig = withTM({ productionBrowserSourceMaps: true, reactStrictMode: true, swcMinify: false, + publicRuntimeConfig: { + NODE_ENV: process.env.NODE_ENV, + PROJECT_NAME: process.env.npm_package_name, + BUILD_DATE: new Date().toISOString(), + CI: process.env.CI || null, + VERSION: getGitVersion(), + COMMIT_HASH: getCommitHash(), + }, }); module.exports = nextConfig; diff --git a/packages/app/scripts/gitInfo.js b/packages/app/scripts/gitInfo.js new file mode 100644 index 0000000000..9e650ed49d --- /dev/null +++ b/packages/app/scripts/gitInfo.js @@ -0,0 +1,46 @@ +// @ts-check + +// import { execSync } from 'child_process' +const { execSync } = require('child_process'); + +const hasGit = () => { + try { + execSync('git --version'); + } catch { + return false; + } + return true; +}; + +const getTopLevel = () => execSync('git rev-parse --show-toplevel'); +const isRepository = () => { + try { + getTopLevel(); + } catch { + return false; + } + return true; +}; + +const getGitVersion = () => { + if (!hasGit() || !isRepository()) { + console.error( + "You haven't installed git or it does not exist in your PATH." + ); + return null; + } + const VERSION = execSync('git describe --always --dirty') + .toString() + // remove empty line + .replace(/[\s\r\n]+$/, ''); + + return VERSION; +}; + +const getCommitHash = (rev = 'HEAD') => + execSync(`git rev-parse --short ${rev}`).toString(); + +module.exports = { + getGitVersion, + getCommitHash, +}; diff --git a/packages/app/src/pages/_app.tsx b/packages/app/src/pages/_app.tsx index fd2108edca..afa0fb03e7 100644 --- a/packages/app/src/pages/_app.tsx +++ b/packages/app/src/pages/_app.tsx @@ -9,6 +9,7 @@ import { Logger } from '@toeverything/pathfinder-logger'; import '@fontsource/space-mono'; import '@fontsource/poppins'; +import '../utils/print-build-info'; const ThemeProvider = dynamic(() => import('@/styles/themeProvider'), { ssr: false, diff --git a/packages/app/src/utils/print-build-info.ts b/packages/app/src/utils/print-build-info.ts new file mode 100644 index 0000000000..5acb994c8b --- /dev/null +++ b/packages/app/src/utils/print-build-info.ts @@ -0,0 +1,43 @@ +import getConfig from 'next/config'; + +type Config = { + BUILD_DATE: string; + NODE_ENV: string; + PROJECT_NAME: string; + VERSION: string; + CI?: string; + COMMIT_HASH: string; +}; + +const nextConfig = getConfig(); +const publicRuntimeConfig: Config = nextConfig.publicRuntimeConfig; + +const printBuildInfo = () => { + if (process.env.NODE_ENV === 'development') { + return; + } + console.group('Build info'); + console.log('Project:', publicRuntimeConfig.PROJECT_NAME); + console.log( + 'Build date:', + publicRuntimeConfig.BUILD_DATE + ? new Date(publicRuntimeConfig.BUILD_DATE).toLocaleString() + : 'Unknown' + ); + console.log( + 'Environment:', + `${publicRuntimeConfig.NODE_ENV}${publicRuntimeConfig.CI ? '(ci)' : ''}` + ); + console.log('Version:', publicRuntimeConfig.VERSION); + console.log( + 'AFFiNE is an open source project, you can view its source code on GitHub!' + ); + console.log( + `https://github.com/toeverything/AFFiNE/tree/${publicRuntimeConfig.COMMIT_HASH}` + ); + console.groupEnd(); +}; + +printBuildInfo(); + +export {};