Merge pull request #523 from toeverything/chore/print-build-info

chore: print build info
This commit is contained in:
Qi 2022-11-03 19:02:48 +08:00 committed by GitHub
commit 0a57e29f2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 104 additions and 1 deletions

View File

@ -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;

View File

@ -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,
};

View File

@ -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,

View File

@ -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 {};