瀏覽代碼

feat: Track current version (#2731)

vanilla_orig
Lipis 4 年之前
父節點
當前提交
001880ba88
沒有連結到貢獻者的電子郵件帳戶。
共有 4 個檔案被更改,包括 45 行新增14 行删除
  1. 30
    13
      scripts/build-version.js
  2. 1
    0
      src/constants.ts
  3. 8
    1
      src/excalidraw-app/index.tsx
  4. 6
    0
      src/utils.ts

+ 30
- 13
scripts/build-version.js 查看文件

@@ -5,23 +5,40 @@ const path = require("path");
5 5
 const versionFile = path.join("build", "version.json");
6 6
 const indexFile = path.join("build", "index.html");
7 7
 
8
-const zero = (digit) => `0${digit}`.slice(-2);
9
-
10
-const versionDate = (date) => {
11
-  const date_ = `${date.getFullYear()}-${zero(date.getMonth() + 1)}-${zero(
12
-    date.getDate(),
13
-  )}`;
14
-  const time = `${zero(date.getHours())}-${zero(date.getMinutes())}-${zero(
15
-    date.getSeconds(),
16
-  )}`;
17
-  return `${date_}-${time}`;
8
+const versionDate = (date) => date.toISOString().replace(".000", "");
9
+
10
+const commitHash = () => {
11
+  try {
12
+    return require("child_process")
13
+      .execSync("git rev-parse --short HEAD")
14
+      .toString()
15
+      .trim();
16
+  } catch {
17
+    return "none";
18
+  }
18 19
 };
19 20
 
20
-const now = new Date();
21
+const commitDate = (hash) => {
22
+  try {
23
+    const unix = require("child_process")
24
+      .execSync(`git show -s --format=%ct ${hash}`)
25
+      .toString()
26
+      .trim();
27
+    const date = new Date(parseInt(unix) * 1000);
28
+    return versionDate(date);
29
+  } catch {
30
+    return versionDate(new Date());
31
+  }
32
+};
33
+
34
+const getFullVersion = () => {
35
+  const hash = commitHash();
36
+  return `${commitDate(hash)}-${hash}`;
37
+};
21 38
 
22 39
 const data = JSON.stringify(
23 40
   {
24
-    version: versionDate(now),
41
+    version: getFullVersion(),
25 42
   },
26 43
   undefined,
27 44
   2,
@@ -34,7 +51,7 @@ fs.readFile(indexFile, "utf8", (error, data) => {
34 51
   if (error) {
35 52
     return console.error(error);
36 53
   }
37
-  const result = data.replace(/{version}/g, versionDate(now));
54
+  const result = data.replace(/{version}/g, getFullVersion());
38 55
 
39 56
   fs.writeFile(indexFile, result, "utf8", (error) => {
40 57
     if (error) {

+ 1
- 0
src/constants.ts 查看文件

@@ -70,6 +70,7 @@ export const DEFAULT_FONT_SIZE = 20;
70 70
 export const DEFAULT_FONT_FAMILY: FontFamily = 1;
71 71
 export const DEFAULT_TEXT_ALIGN = "left";
72 72
 export const DEFAULT_VERTICAL_ALIGN = "top";
73
+export const DEFAULT_VERSION = "{version}";
73 74
 
74 75
 export const CANVAS_ONLY_ACTIONS = ["selectAll"];
75 76
 

+ 8
- 1
src/excalidraw-app/index.tsx 查看文件

@@ -6,6 +6,7 @@ import React, {
6 6
   useRef,
7 7
   useState,
8 8
 } from "react";
9
+import { trackEvent } from "../analytics";
9 10
 import { getDefaultAppState } from "../appState";
10 11
 import { ExcalidrawImperativeAPI } from "../components/App";
11 12
 import { ErrorDialog } from "../components/ErrorDialog";
@@ -22,7 +23,12 @@ import Excalidraw, {
22 23
   languages,
23 24
 } from "../packages/excalidraw/index";
24 25
 import { AppState, ExcalidrawAPIRefValue } from "../types";
25
-import { debounce, ResolvablePromise, resolvablePromise } from "../utils";
26
+import {
27
+  debounce,
28
+  getVersion,
29
+  ResolvablePromise,
30
+  resolvablePromise,
31
+} from "../utils";
26 32
 import { SAVE_TO_LOCAL_STORAGE_TIMEOUT } from "./app_constants";
27 33
 import CollabWrapper, { CollabAPI } from "./collab/CollabWrapper";
28 34
 import { LanguageList } from "./components/LanguageList";
@@ -223,6 +229,7 @@ function ExcalidrawWrapper(props: { collab: CollabAPI }) {
223 229
   const { collab } = props;
224 230
 
225 231
   useEffect(() => {
232
+    trackEvent("load", "version", getVersion());
226 233
     excalidrawRef.current!.readyPromise.then((excalidrawApi) => {
227 234
       initializeScene({
228 235
         resetScene: excalidrawApi.resetScene,

+ 6
- 0
src/utils.ts 查看文件

@@ -1,6 +1,7 @@
1 1
 import colors from "./colors";
2 2
 import {
3 3
   CURSOR_TYPE,
4
+  DEFAULT_VERSION,
4 5
   FONT_FAMILY,
5 6
   WINDOWS_EMOJI_FALLBACK_FONT,
6 7
 } from "./constants";
@@ -361,3 +362,8 @@ export const nFormatter = (num: number, digits: number): string => {
361 362
     (num / si[index].value).toFixed(digits).replace(rx, "$1") + si[index].symbol
362 363
   );
363 364
 };
365
+
366
+export const getVersion = () => {
367
+  const version = document.querySelector('meta[name="version"]');
368
+  return version ? (version as any).content : DEFAULT_VERSION;
369
+};

Loading…
取消
儲存