瀏覽代碼

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
 const versionFile = path.join("build", "version.json");
5
 const versionFile = path.join("build", "version.json");
6
 const indexFile = path.join("build", "index.html");
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
 const data = JSON.stringify(
39
 const data = JSON.stringify(
23
   {
40
   {
24
-    version: versionDate(now),
41
+    version: getFullVersion(),
25
   },
42
   },
26
   undefined,
43
   undefined,
27
   2,
44
   2,
34
   if (error) {
51
   if (error) {
35
     return console.error(error);
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
   fs.writeFile(indexFile, result, "utf8", (error) => {
56
   fs.writeFile(indexFile, result, "utf8", (error) => {
40
     if (error) {
57
     if (error) {

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

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

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

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

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

1
 import colors from "./colors";
1
 import colors from "./colors";
2
 import {
2
 import {
3
   CURSOR_TYPE,
3
   CURSOR_TYPE,
4
+  DEFAULT_VERSION,
4
   FONT_FAMILY,
5
   FONT_FAMILY,
5
   WINDOWS_EMOJI_FALLBACK_FONT,
6
   WINDOWS_EMOJI_FALLBACK_FONT,
6
 } from "./constants";
7
 } from "./constants";
361
     (num / si[index].value).toFixed(digits).replace(rx, "$1") + si[index].symbol
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…
取消
儲存