Distribute git data through NPM (#2671)

* Distribute git data through NPM

* Make git data parsing backwards compatible with current dockerfile
This commit is contained in:
Lion - dapplion
2021-06-09 23:35:49 +02:00
committed by GitHub
parent 5df11338e8
commit 927ab09961
4 changed files with 47 additions and 12 deletions

3
.gitignore vendored
View File

@@ -48,3 +48,6 @@ packages/lodestar/.tmpdb/
# docker-compose .env file
.env
# Git artifacts
packages/cli/.git-data.json

View File

@@ -12,14 +12,15 @@
"files": [
"lib/**/*.d.ts",
"lib/**/*.js",
"lib/**/*.js.map"
"lib/**/*.js.map",
".git-data.json"
],
"bin": {
"lodestar": "lib/index.js"
},
"scripts": {
"clean": "rm -rf lib && rm -f *.tsbuildinfo",
"build": "concurrently \"yarn build:lib\" \"yarn build:types\"",
"build": "concurrently \"yarn build:lib\" \"yarn build:types\" && yarn write-git-data",
"build:release": "yarn clean && yarn run build && yarn run build:typedocs",
"build:lib": "babel src -x .ts -d lib --source-maps",
"build:lib:watch": "yarn run build:lib --watch",
@@ -27,6 +28,7 @@
"build:types": "tsc -p tsconfig.build.json",
"build:types:watch": "yarn run build:types --watch",
"build:refdocs": "ts-node docsgen docs/cli.md",
"write-git-data": "node ./scripts/getGitData .git-data.json",
"check-types": "tsc",
"lint": "eslint --color --ext .ts src/ test/",
"lint:fix": "yarn run lint --fix",

View File

@@ -0,0 +1,17 @@
/* eslint-disable */
const fs = require("fs");
const {execSync} = require("child_process");
// Persist git data and distribute through NPM so CLI consumers can know exactly
// at what commit was this src build. This is used in the metrics and to log initially
const shell = (cmd) => execSync(cmd).toString().trim();
const version = JSON.parse(fs.readFileSync("package.json")).version;
const branch = shell("git rev-parse --abbrev-ref HEAD");
const commit = shell("git rev-parse --verify HEAD");
const data = {version, branch, commit};
const versionDataFile = process.argv[2];
fs.writeFileSync(versionDataFile, JSON.stringify(data, null, 2));
console.log(data);

View File

@@ -1,7 +1,12 @@
import fs from "fs";
import path from "path";
import {execSync} from "child_process";
import {getLocalVersion} from "./version";
// This file is created in the build step and is distributed through NPM
// MUST be in sync with packages/cli/scripts/getGitData.js, and package.json .files
const LOCAL_GIT_DATA_FILEPATH = path.join(__dirname, "../../.git-data.json");
/* eslint-disable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment */
type GitData = {
/** "0.16.0" */
@@ -27,17 +32,12 @@ type GitData = {
*/
export function readLodestarGitData(): GitData {
try {
const gitDataFilepath = process?.env?.DOCKER_LODESTAR_GIT_DATA_FILEPATH;
if (gitDataFilepath) {
// Lazy load fs module only if necessary
// eslint-disable-next-line
const gitData = JSON.parse(fs.readFileSync(gitDataFilepath, "utf8"));
const {version: semver, branch, commit} = gitData;
return {semver, branch, commit, version: `${semver} ${branch} ${commit.slice(0, 8)}`};
}
const semver = getLocalVersion() ?? undefined;
const gitData = getGitData();
const currentGitData = getGitData();
const persistedGitData = getPersistedGitData();
// If the CLI is run from source, prioritze current git data over .git-data.json file, which might be stale
const gitData = {...persistedGitData, ...currentGitData};
return {
semver: semver || "-",
branch: gitData?.branch || "-",
@@ -63,3 +63,16 @@ function getGitData(): Partial<GitData> {
return {};
}
}
function getPersistedGitData(): Partial<GitData> {
try {
const gitDataFilepath = process?.env?.DOCKER_LODESTAR_GIT_DATA_FILEPATH || LOCAL_GIT_DATA_FILEPATH;
// eslint-disable-next-line
const gitData = JSON.parse(fs.readFileSync(gitDataFilepath, "utf8"));
const {version: semver, branch, commit} = gitData;
return {semver, branch, commit, version: `${semver} ${branch} ${commit.slice(0, 8)}`};
} catch (e) {
return {};
}
}