cli: define BUILD_INFO in builds

This commit is contained in:
Patrik Oldsberg
2020-08-31 17:30:47 +02:00
parent 343e87f633
commit 0d40a302ff
3 changed files with 45 additions and 4 deletions
+1 -1
View File
@@ -36,7 +36,7 @@ export async function buildBundle(options: BuildOptions) {
const { statsJsonEnabled } = options;
const paths = resolveBundlingPaths(options);
const config = createConfig(paths, {
const config = await createConfig(paths, {
...options,
checksEnabled: false,
isDev: false,
+43 -2
View File
@@ -14,6 +14,7 @@
* limitations under the License.
*/
import fs from 'fs-extra';
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import ModuleScopePlugin from 'react-dev-utils/ModuleScopePlugin';
@@ -25,6 +26,9 @@ import { Config } from '@backstage/config';
import { BundlingPaths } from './paths';
import { transforms } from './transforms';
import { BundlingOptions, BackendBundlingOptions } from './types';
import { version } from '../../lib/version';
import { paths as cliPaths } from '../../lib/paths';
import { runPlain } from '../run';
export function resolveBaseUrl(config: Config): URL {
const baseUrl = config.getString('app.baseUrl');
@@ -35,10 +39,40 @@ export function resolveBaseUrl(config: Config): URL {
}
}
export function createConfig(
async function readBuildInfo() {
const timestamp = Date.now();
let commit = 'unknown';
try {
commit = await runPlain('git', 'rev-parse', 'HEAD');
} catch (error) {
console.warn(`WARNING: Failed to read git commit, ${error}`);
}
let gitVersion = 'unknown';
try {
gitVersion = await runPlain('git', 'describe', '--always');
} catch (error) {
console.warn(`WARNING: Failed to describe git version, ${error}`);
}
const { version: packageVersion } = await fs.readJson(
cliPaths.resolveTarget('package.json'),
);
return {
cliVersion: version,
gitVersion,
packageVersion,
timestamp,
commit,
};
}
export async function createConfig(
paths: BundlingPaths,
options: BundlingOptions,
): webpack.Configuration {
): Promise<webpack.Configuration> {
const { checksEnabled, isDev } = options;
const { plugins, loaders } = transforms(options);
@@ -81,6 +115,13 @@ export function createConfig(
}),
);
const buildInfo = await readBuildInfo();
plugins.push(
new webpack.DefinePlugin({
'process.env.BUILD_INFO': JSON.stringify(buildInfo),
}),
);
return {
mode: isDev ? 'development' : 'production',
profile: false,
+1 -1
View File
@@ -30,7 +30,7 @@ export async function serveBundle(options: ServeOptions) {
const paths = resolveBundlingPaths(options);
const pkgPath = paths.targetPackageJson;
const pkg = await fs.readJson(pkgPath);
const config = createConfig(paths, {
const config = await createConfig(paths, {
...options,
isDev: true,
baseUrl: url,