Skip to content

Commit 9888414

Browse files
committed
feat(compiler): add ReplexicaOutputProcessor
1 parent 8d3f3ee commit 9888414

1 file changed

Lines changed: 111 additions & 0 deletions

File tree

packages/compiler/src/output.ts

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import { File } from "@babel/types";
2+
import fs from 'fs';
3+
import path from 'path';
4+
import { ReplexicaCompilerData, ReplexicaCompilerPayload } from "./compiler";
5+
import { ReplexicaLocaleData, ReplexicaConfig } from "./types";
6+
7+
const debugDir = '.debug/replexica';
8+
export class ReplexicaOutputProcessor {
9+
public static create(relativeFilePath: string, options: ReplexicaConfig) {
10+
return new ReplexicaOutputProcessor(relativeFilePath, options);
11+
}
12+
13+
private constructor(
14+
private readonly relativeFilePath: string,
15+
private readonly options: ReplexicaConfig,
16+
) {}
17+
18+
public saveData(data: ReplexicaCompilerData) {
19+
const filePath = path.join(process.cwd(), this.options.outDir, '.replexica.json');
20+
const existingData: ReplexicaCompilerPayload = this._loadObject<ReplexicaCompilerPayload>(filePath) || this._createEmptyCompilerPayload();
21+
const newData = {
22+
...existingData,
23+
data: {
24+
...existingData.data,
25+
...data,
26+
},
27+
};
28+
this._saveObject(filePath, newData);
29+
}
30+
31+
32+
public saveSourceLocaleData(data: ReplexicaCompilerData) {
33+
const existingData: ReplexicaLocaleData =
34+
this._loadObject<ReplexicaLocaleData>(path.join(process.cwd(), this.options.i18nDir, `${this.options.sourceLocale}.json`)) ||
35+
this._createEmptyLocaleData();
36+
37+
const newLocaleData: ReplexicaLocaleData = {
38+
...existingData,
39+
};
40+
for (const [fileId, fileData] of Object.entries(data)) {
41+
newLocaleData[fileId] = {};
42+
for (const [scopeId, scopeData] of Object.entries(fileData.data)) {
43+
for (const [chunkId, value] of Object.entries(scopeData.data)) {
44+
newLocaleData[fileId] = {
45+
...newLocaleData[fileId],
46+
[scopeId]: {
47+
...newLocaleData[fileId]?.[scopeId],
48+
[chunkId]: value,
49+
},
50+
};
51+
}
52+
}
53+
if (Object.keys(newLocaleData[fileId]).length === 0) {
54+
delete newLocaleData[fileId];
55+
}
56+
}
57+
const filePath = path.join(process.cwd(), this.options.i18nDir, `${this.options.sourceLocale}.json`);
58+
this._saveObject(filePath, newLocaleData);
59+
}
60+
61+
public saveAst(ast: File) {
62+
const filePath = path.join(process.cwd(), debugDir, this.relativeFilePath + '.json');
63+
this._saveObject(filePath, ast);
64+
}
65+
66+
public saveOutput(output: string) {
67+
const filePath = path.join(process.cwd(), debugDir, this.relativeFilePath + '.txt');
68+
this._saveText(filePath, output);
69+
}
70+
71+
// Private
72+
73+
private _createEmptyCompilerPayload(): ReplexicaCompilerPayload {
74+
return {
75+
settings: {
76+
locale: {
77+
source: this.options.sourceLocale,
78+
},
79+
},
80+
data: {},
81+
};
82+
}
83+
84+
private _createEmptyLocaleData(): ReplexicaLocaleData {
85+
return {};
86+
}
87+
88+
private _loadObject<T>(filePath: string): T | null {
89+
const content = this._loadText(filePath);
90+
if (!content) { return null; }
91+
return JSON.parse(content);
92+
}
93+
94+
private _saveObject<T>(filePath: string, data: T) {
95+
const content = JSON.stringify(data, null, 2);
96+
this._saveText(filePath, content);
97+
}
98+
99+
private _loadText(filePath: string): string | null {
100+
if (!fs.existsSync(filePath)) { return null; }
101+
return fs.readFileSync(filePath, 'utf-8');
102+
}
103+
104+
private _saveText(filePath: string, content: string) {
105+
const filePathDir = path.dirname(filePath);
106+
if (!fs.existsSync(filePathDir)) {
107+
fs.mkdirSync(filePathDir, { recursive: true });
108+
}
109+
fs.writeFileSync(filePath, content, 'utf-8');
110+
}
111+
}

0 commit comments

Comments
 (0)