-
-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathutils.ts
More file actions
55 lines (47 loc) · 1.45 KB
/
utils.ts
File metadata and controls
55 lines (47 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { execSync } from 'node:child_process'
import * as fs from 'node:fs'
import type { EnJson, EnMetaJson } from './types.d.ts'
export function getCurrentCommitHash() {
return git('rev-parse HEAD')
}
export function getNewEnJson(enJsonPath: string): EnJson {
if (fs.existsSync(enJsonPath)) {
return readJson<EnJson>(enJsonPath)
}
return {} as EnJson
}
export function getOldEnMetaJson(enMetaJsonPath: string): EnMetaJson {
if (fs.existsSync(enMetaJsonPath)) {
return readJson<EnMetaJson>(enMetaJsonPath)
}
return {} as EnMetaJson
}
export function checkTranslationChanges(oldMeta: EnMetaJson, newMeta: EnMetaJson): boolean {
const oldObj = omitMeta(oldMeta)
const newObj = omitMeta(newMeta)
return JSON.stringify(oldObj) !== JSON.stringify(newObj)
}
export function createUpdatedEnMetaJson(commitHash: string, content: EnMetaJson): EnMetaJson {
return {
$meta: {
last_updated_commit: commitHash,
updated_at: new Date().toISOString(),
},
...omitMeta(content),
}
}
function git(command: string) {
try {
return execSync(`git ${command}`, { encoding: 'utf-8' }).trim()
} catch {
console.error(`🚨 Git command failed: git ${command}`)
return null
}
}
function readJson<T>(filePath: string): T {
return JSON.parse(fs.readFileSync(filePath, 'utf-8')) as T
}
function omitMeta<T extends object>(obj: T): Omit<T, '$meta'> {
const { $meta: _, ...rest } = obj as T & { $meta?: unknown }
return rest
}