Include package.json/git info in Rollup bundles

This commit is contained in:
Samuel Elliott 2022-06-30 17:51:27 +01:00
parent 82e9b82e23
commit 4c6353c523
No known key found for this signature in database
GPG Key ID: 8420C7CDE43DC4D6
3 changed files with 80 additions and 7 deletions

View File

@ -11,7 +11,7 @@ build:
stage: build
script:
- npx tsc
- npx rollup --config
- NODE_ENV=production npx rollup --config
- node bin/nxapi.js util validate-discord-titles

View File

@ -1,13 +1,57 @@
import path from 'path';
import * as path from 'path';
import { fileURLToPath } from 'url';
import * as fs from 'fs';
import * as child_process from 'child_process';
import typescript from '@rollup/plugin-typescript';
import commonjs from '@rollup/plugin-commonjs';
import alias from '@rollup/plugin-alias';
import replace from '@rollup/plugin-replace';
import nodeResolve from '@rollup/plugin-node-resolve';
import nodePolyfill from 'rollup-plugin-polyfill-node';
import html from '@rollup/plugin-html';
import json from '@rollup/plugin-json';
const dir = path.dirname(fileURLToPath(import.meta.url));
const pkg = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf-8'));
const git = (() => {
try {
fs.statSync(path.join(dir, '.git'));
} catch (err) {
return null;
}
const options = {cwd: dir};
const revision = child_process.execSync('git rev-parse HEAD', options).toString().trim();
const branch = child_process.execSync('git rev-parse --abbrev-ref HEAD', options).toString().trim();
const changed_files = child_process.execSync('git diff --name-only HEAD', options).toString().trim();
return {
revision,
branch: branch && branch !== 'HEAD' ? branch : null,
changed_files: changed_files.length ? changed_files.split('\n') : [],
};
})();;
// If CI_COMMIT_TAG is set this is a tagged version for release
export const product = 'nxapi ' + pkg.version +
(!process.env.CI_COMMIT_TAG && git ?
'-' + git.revision.substr(0, 7) + (git.branch ? ' (' + git.branch + ')' : '') : '');
/**
* @type {import('@rollup/plugin-replace').RollupReplaceOptions}
*/
const replace_options = {
include: ['src/util/product.ts'],
values: {
'globalThis.__NXAPI_BUNDLE_PKG__': JSON.stringify(pkg),
'globalThis.__NXAPI_BUNDLE_GIT__': JSON.stringify(git),
'globalThis.__NXAPI_BUNDLE_PRODUCT__': JSON.stringify(product),
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV ?? 'development'),
},
};
/**
* @type {import('rollup').RollupOptions['watch']}
*/
@ -24,8 +68,10 @@ const cli = {
file: 'dist/bundle/cli-bundle.js',
format: 'es',
inlineDynamicImports: true,
sourcemap: true,
},
plugins: [
replace(replace_options),
typescript({
noEmit: true,
declaration: false,
@ -63,6 +109,7 @@ const app = {
sourcemap: true,
},
plugins: [
replace(replace_options),
typescript({
noEmit: true,
declaration: false,
@ -98,6 +145,7 @@ const app_preload = {
sourcemap: true,
},
plugins: [
replace(replace_options),
typescript({
noEmit: true,
declaration: false,
@ -128,6 +176,7 @@ const app_preload_webservice = {
format: 'cjs',
},
plugins: [
replace(replace_options),
typescript({
noEmit: true,
declaration: false,
@ -162,6 +211,7 @@ const app_browser = {
html({
title: 'nxapi',
}),
replace(replace_options),
typescript({
noEmit: true,
declaration: false,

View File

@ -7,14 +7,35 @@ import createDebug from 'debug';
const debug = createDebug('nxapi:util:product');
//
// Embedded package/version info injected during Rollup build
//
/** @internal */
declare global {
var __NXAPI_BUNDLE_PKG__: any | undefined;
var __NXAPI_BUNDLE_GIT__: {
revision: string;
branch: string | null;
changed_files: string[];
} | null | undefined;
var __NXAPI_BUNDLE_PRODUCT__: any | undefined;
}
const embedded_pkg = globalThis.__NXAPI_BUNDLE_PKG__;
const embedded_git = globalThis.__NXAPI_BUNDLE_GIT__;
const embedded_product = globalThis.__NXAPI_BUNDLE_PRODUCT__;
//
// Package/version info
//
export const dir = path.resolve(fileURLToPath(import.meta.url), '..', '..', '..');
export const pkg = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf-8'));
export const version = pkg.version;
export const git = (() => {
export const pkg = embedded_pkg ?? JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf-8'));
export const version: string = pkg.version;
export const git = typeof embedded_git !== 'undefined' ? embedded_git : (() => {
try {
fs.statSync(path.join(dir, '.git'));
} catch (err) {
@ -32,7 +53,9 @@ export const git = (() => {
changed_files: changed_files.length ? changed_files.split('\n') : [],
};
})();
export const dev = !!git || process.env.NODE_ENV === 'development';
export const product = 'nxapi ' + version +
export const dev = process.env.NODE_ENV !== 'production' &&
(!!git || process.env.NODE_ENV === 'development');
export const product = embedded_product ?? 'nxapi ' + version +
(git ? '-' + git.revision.substr(0, 7) + (git.branch ? ' (' + git.branch + ')' : dev ? '-dev' : '') : '');