|
1 | 1 | import { execSync } from 'node:child_process' |
2 | | -import { relative } from 'node:path' |
| 2 | +import * as path from 'node:path' |
| 3 | +import * as fs from 'node:fs' |
| 4 | +import type { EnJson, EnMetaJson } from './types.d.ts' |
3 | 5 |
|
4 | | -export function git(command: string): string { |
5 | | - return execSync(command, { encoding: 'utf-8' }).trim() |
| 6 | +export function git(command: string) { |
| 7 | + try { |
| 8 | + return execSync(`git ${command}`, { encoding: 'utf-8' }).trim() |
| 9 | + } catch { |
| 10 | + console.error(`🚨 Git command failed: git ${command}`) |
| 11 | + return null |
| 12 | + } |
6 | 13 | } |
7 | 14 |
|
8 | | -export function getCurrentCommitHash(): string { |
9 | | - return git('git rev-parse HEAD') |
| 15 | +function readJson<T>(filePath: string): T { |
| 16 | + return JSON.parse(fs.readFileSync(filePath, 'utf-8')) as T |
10 | 17 | } |
11 | 18 |
|
12 | | -export function getPreviousEnJson(fileName: string): string { |
13 | | - const relativePath = relative(process.cwd(), fileName) |
14 | | - return git(`git show HEAD^:${relativePath}`) |
| 19 | +export function getCurrentCommitHash() { |
| 20 | + return git('rev-parse HEAD') |
| 21 | +} |
| 22 | + |
| 23 | +export function getPreviousCommitHash(parsedOldEnMetaJson: EnMetaJson) { |
| 24 | + const lastCommit = parsedOldEnMetaJson.$meta?.last_updated_commit |
| 25 | + if (lastCommit) { |
| 26 | + return lastCommit |
| 27 | + } |
| 28 | + return git('rev-parse HEAD~1') |
| 29 | +} |
| 30 | + |
| 31 | +export function getNewEnJson(enJsonPath: string): EnJson { |
| 32 | + if (fs.existsSync(enJsonPath)) { |
| 33 | + return readJson<EnMetaJson>(enJsonPath) |
| 34 | + } |
| 35 | + return {} as EnMetaJson |
| 36 | +} |
| 37 | + |
| 38 | +export function getOldEnJson(oldEnMeta: EnMetaJson, enJsonPath: string): EnJson { |
| 39 | + const previousHash = getPreviousCommitHash(oldEnMeta) |
| 40 | + if (previousHash) { |
| 41 | + const relativePath = path.relative(process.cwd(), enJsonPath) |
| 42 | + const previousContent = git(`show "${previousHash}:${relativePath}"`) |
| 43 | + if (previousContent) { |
| 44 | + return JSON.parse(previousContent) as EnMetaJson |
| 45 | + } |
| 46 | + } |
| 47 | + return {} as EnMetaJson |
| 48 | +} |
| 49 | + |
| 50 | +export function getOldEnMetaJson(enMetaJsonPath: string): EnMetaJson { |
| 51 | + if (fs.existsSync(enMetaJsonPath)) { |
| 52 | + return readJson<EnMetaJson>(enMetaJsonPath) |
| 53 | + } |
| 54 | + return {} as EnMetaJson |
| 55 | +} |
| 56 | + |
| 57 | +function omitMeta<T extends object>(obj: T): Omit<T, '$meta'> { |
| 58 | + const { $meta: _, ...rest } = obj as T & { $meta?: unknown } |
| 59 | + return rest |
| 60 | +} |
| 61 | + |
| 62 | +export function checkTranslationChanges(oldMeta: EnMetaJson, newMeta: EnMetaJson): boolean { |
| 63 | + const oldObj = omitMeta(oldMeta) |
| 64 | + const newObj = omitMeta(newMeta) |
| 65 | + return JSON.stringify(oldObj) !== JSON.stringify(newObj) |
| 66 | +} |
| 67 | + |
| 68 | +export function createUpdatedEnMetaJson( |
| 69 | + commitHash: string | null, |
| 70 | + content: EnMetaJson, |
| 71 | +): EnMetaJson { |
| 72 | + return { |
| 73 | + $meta: { |
| 74 | + last_updated_commit: commitHash, |
| 75 | + updated_at: new Date().toISOString(), |
| 76 | + }, |
| 77 | + ...content, |
| 78 | + } as EnMetaJson |
15 | 79 | } |
0 commit comments