From 001880ba88854de709c5a22e90f47dde3fef409f Mon Sep 17 00:00:00 2001 From: Lipis Date: Sun, 10 Jan 2021 20:48:12 +0200 Subject: [PATCH] feat: Track current version (#2731) --- scripts/build-version.js | 41 +++++++++++++++++++++++++----------- src/constants.ts | 1 + src/excalidraw-app/index.tsx | 9 +++++++- src/utils.ts | 6 ++++++ 4 files changed, 44 insertions(+), 13 deletions(-) diff --git a/scripts/build-version.js b/scripts/build-version.js index 06b31b43..0d77f7da 100755 --- a/scripts/build-version.js +++ b/scripts/build-version.js @@ -5,23 +5,40 @@ const path = require("path"); const versionFile = path.join("build", "version.json"); const indexFile = path.join("build", "index.html"); -const zero = (digit) => `0${digit}`.slice(-2); +const versionDate = (date) => date.toISOString().replace(".000", ""); -const versionDate = (date) => { - const date_ = `${date.getFullYear()}-${zero(date.getMonth() + 1)}-${zero( - date.getDate(), - )}`; - const time = `${zero(date.getHours())}-${zero(date.getMinutes())}-${zero( - date.getSeconds(), - )}`; - return `${date_}-${time}`; +const commitHash = () => { + try { + return require("child_process") + .execSync("git rev-parse --short HEAD") + .toString() + .trim(); + } catch { + return "none"; + } }; -const now = new Date(); +const commitDate = (hash) => { + try { + const unix = require("child_process") + .execSync(`git show -s --format=%ct ${hash}`) + .toString() + .trim(); + const date = new Date(parseInt(unix) * 1000); + return versionDate(date); + } catch { + return versionDate(new Date()); + } +}; + +const getFullVersion = () => { + const hash = commitHash(); + return `${commitDate(hash)}-${hash}`; +}; const data = JSON.stringify( { - version: versionDate(now), + version: getFullVersion(), }, undefined, 2, @@ -34,7 +51,7 @@ fs.readFile(indexFile, "utf8", (error, data) => { if (error) { return console.error(error); } - const result = data.replace(/{version}/g, versionDate(now)); + const result = data.replace(/{version}/g, getFullVersion()); fs.writeFile(indexFile, result, "utf8", (error) => { if (error) { diff --git a/src/constants.ts b/src/constants.ts index 0a6a793c..4f22b35e 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -70,6 +70,7 @@ export const DEFAULT_FONT_SIZE = 20; export const DEFAULT_FONT_FAMILY: FontFamily = 1; export const DEFAULT_TEXT_ALIGN = "left"; export const DEFAULT_VERTICAL_ALIGN = "top"; +export const DEFAULT_VERSION = "{version}"; export const CANVAS_ONLY_ACTIONS = ["selectAll"]; diff --git a/src/excalidraw-app/index.tsx b/src/excalidraw-app/index.tsx index 4af6a242..d63eb71b 100644 --- a/src/excalidraw-app/index.tsx +++ b/src/excalidraw-app/index.tsx @@ -6,6 +6,7 @@ import React, { useRef, useState, } from "react"; +import { trackEvent } from "../analytics"; import { getDefaultAppState } from "../appState"; import { ExcalidrawImperativeAPI } from "../components/App"; import { ErrorDialog } from "../components/ErrorDialog"; @@ -22,7 +23,12 @@ import Excalidraw, { languages, } from "../packages/excalidraw/index"; import { AppState, ExcalidrawAPIRefValue } from "../types"; -import { debounce, ResolvablePromise, resolvablePromise } from "../utils"; +import { + debounce, + getVersion, + ResolvablePromise, + resolvablePromise, +} from "../utils"; import { SAVE_TO_LOCAL_STORAGE_TIMEOUT } from "./app_constants"; import CollabWrapper, { CollabAPI } from "./collab/CollabWrapper"; import { LanguageList } from "./components/LanguageList"; @@ -223,6 +229,7 @@ function ExcalidrawWrapper(props: { collab: CollabAPI }) { const { collab } = props; useEffect(() => { + trackEvent("load", "version", getVersion()); excalidrawRef.current!.readyPromise.then((excalidrawApi) => { initializeScene({ resetScene: excalidrawApi.resetScene, diff --git a/src/utils.ts b/src/utils.ts index e1c276d4..933765c2 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,7 @@ import colors from "./colors"; import { CURSOR_TYPE, + DEFAULT_VERSION, FONT_FAMILY, WINDOWS_EMOJI_FALLBACK_FONT, } from "./constants"; @@ -361,3 +362,8 @@ export const nFormatter = (num: number, digits: number): string => { (num / si[index].value).toFixed(digits).replace(rx, "$1") + si[index].symbol ); }; + +export const getVersion = () => { + const version = document.querySelector('meta[name="version"]'); + return version ? (version as any).content : DEFAULT_VERSION; +};