-
Notifications
You must be signed in to change notification settings - Fork 27
/
extractLiteralsFromFiles.ts
140 lines (118 loc) · 3.75 KB
/
extractLiteralsFromFiles.ts
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import * as fs from 'fs';
import * as path from 'path';
import * as ts from 'typescript';
// Function to extract string literals from binary expressions
function extractStringFromBinaryExpression(
node: ts.BinaryExpression,
): string | null {
const left = node.left;
const right = node.right;
let leftString: string | null = null;
let rightString: string | null = null;
if (ts.isStringLiteral(left)) {
leftString = left.text;
} else if (ts.isBinaryExpression(left)) {
leftString = extractStringFromBinaryExpression(left);
}
if (ts.isStringLiteral(right)) {
rightString = right.text;
} else if (ts.isBinaryExpression(right)) {
rightString = extractStringFromBinaryExpression(right);
}
if (leftString !== null && rightString !== null) {
return leftString + rightString;
} else if (leftString !== null) {
return leftString;
} else if (rightString !== null) {
return rightString;
}
return null;
}
// Function to extract string literals from the first argument of the translate function
function extractStringLiteralFromTranslate(
node: ts.Node,
literals: Map<string, Set<string>>,
filePath: string,
) {
if (
ts.isCallExpression(node) &&
ts.isIdentifier(node.expression) &&
node.expression.text === 'translate' &&
node.arguments.length > 0
) {
const firstArg = node.arguments[0];
let literal: string | null = null;
if (ts.isStringLiteral(firstArg)) {
literal = firstArg.text;
} else if (ts.isBinaryExpression(firstArg)) {
literal = extractStringFromBinaryExpression(firstArg);
}
if (literal !== null) {
if (!literals.has(literal)) {
literals.set(literal, new Set<string>());
}
literals.get(literal)!.add(filePath);
}
}
ts.forEachChild(node, (childNode) =>
extractStringLiteralFromTranslate(childNode, literals, filePath),
);
}
// Function to process a single file
function processFile(
filePath: string,
literals: Map<string, Set<string>>,
baseDir: string,
) {
const sourceCode = fs.readFileSync(filePath, 'utf8');
const sourceFile = ts.createSourceFile(
filePath,
sourceCode,
ts.ScriptTarget.Latest,
true,
);
const relativeFilePath = path.relative(baseDir, filePath);
extractStringLiteralFromTranslate(sourceFile, literals, relativeFilePath);
}
// Function to recursively find all .ts and .tsx files in the specified directory
function getAllTSFiles(dirPath: string, arrayOfFiles: string[] = []): string[] {
const files = fs.readdirSync(dirPath);
files.forEach((file) => {
const fullPath = path.join(dirPath, file);
if (fs.statSync(fullPath).isDirectory()) {
getAllTSFiles(fullPath, arrayOfFiles);
} else if (fullPath.endsWith('.ts') || fullPath.endsWith('.tsx')) {
arrayOfFiles.push(fullPath);
}
});
return arrayOfFiles;
}
// Main function
function main(dirPath: string, outputFilePath: string) {
const tsFiles = getAllTSFiles(dirPath);
const literals = new Map<string, Set<string>>();
tsFiles.forEach((filePath) => {
processFile(filePath, literals, dirPath);
});
// Convert the Map to an object and sort its keys
const sortedLiterals = Object.fromEntries(
Array.from(literals.entries())
.sort()
.map(([literal, files]) => [
literal,
{ description: Array.from(files).join(', '), message: literal },
]),
);
// Save the extracted literals to a JSON file
fs.writeFileSync(
outputFilePath,
JSON.stringify(sortedLiterals, null, 2),
'utf8',
);
}
// Specify the directory to be processed (e.g., current directory)
const directoryPath = path.join(__dirname, 'src');
// Specify the output JSON file path
const outputFilePath = path.join(__dirname, 'template.json');
// Run the main function
main(directoryPath, outputFilePath);