-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathaggregateFiles.ts
More file actions
211 lines (193 loc) · 6.39 KB
/
aggregateFiles.ts
File metadata and controls
211 lines (193 loc) · 6.39 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { logger } from '../../console/logger.js';
import { recordWarning } from '../../state/translateWarnings.js';
import { getRelative, readFile } from '../../fs/findFilepath.js';
import { Settings } from '../../types/index.js';
import type { FileFormat, DataFormat, FileToUpload } from '../../types/data.js';
import { SUPPORTED_FILE_EXTENSIONS } from './supportedFiles.js';
import { parseJson } from '../json/parseJson.js';
import parseYaml from '../yaml/parseYaml.js';
import YAML from 'yaml';
import { determineLibrary } from '../../fs/determineFramework.js';
import { hashStringSync } from '../../utils/hash.js';
import { preprocessContent } from './preprocessContent.js';
export const SUPPORTED_DATA_FORMATS = ['JSX', 'ICU', 'I18NEXT'];
export async function aggregateFiles(
settings: Settings
): Promise<FileToUpload[]> {
// Aggregate all files to translate
const allFiles: FileToUpload[] = [];
if (
!settings.files ||
(Object.keys(settings.files.placeholderPaths).length === 1 &&
settings.files.placeholderPaths.gt)
) {
return allFiles;
}
const { resolvedPaths: filePaths } = settings.files;
const skipValidation = settings.options?.skipFileValidation;
// Process JSON files
if (filePaths.json) {
const { library, additionalModules } = determineLibrary();
// Determine dataFormat for JSONs
let dataFormat: DataFormat;
if (library === 'next-intl') {
dataFormat = 'ICU';
} else if (library === 'i18next') {
if (additionalModules.includes('i18next-icu')) {
dataFormat = 'ICU';
} else {
dataFormat = 'I18NEXT';
}
} else {
dataFormat = 'STRING';
}
const jsonFiles = filePaths.json
.map((filePath) => {
const content = readFile(filePath);
const relativePath = getRelative(filePath);
// Pre-validate JSON parseability
if (!skipValidation?.json) {
try {
JSON.parse(content);
} catch (e: any) {
logger.warn(`Skipping ${relativePath}: JSON file is not parsable`);
recordWarning(
'skipped_file',
relativePath,
'JSON file is not parsable'
);
return null;
}
}
const parsedJson = parseJson(
content,
filePath,
settings.options || {},
settings.defaultLocale
);
return {
fileId: hashStringSync(relativePath),
versionId: hashStringSync(parsedJson),
content: parsedJson,
fileName: relativePath,
fileFormat: 'JSON' as const,
dataFormat,
locale: settings.defaultLocale,
} satisfies FileToUpload;
})
.filter((file) => {
if (!file) return false;
if (typeof file.content !== 'string' || !file.content.trim()) {
logger.warn(`Skipping ${file.fileName}: JSON file is empty`);
recordWarning('skipped_file', file.fileName, 'JSON file is empty');
return false;
}
return true;
});
allFiles.push(...jsonFiles.filter((file) => file !== null));
}
// Process YAML files
if (filePaths.yaml) {
const yamlFiles = filePaths.yaml
.map((filePath) => {
const content = readFile(filePath);
const relativePath = getRelative(filePath);
// Pre-validate YAML parseability
if (!skipValidation?.yaml) {
try {
YAML.parse(content);
} catch (e: any) {
logger.warn(`Skipping ${relativePath}: YAML file is not parsable`);
recordWarning(
'skipped_file',
relativePath,
'YAML file is not parsable'
);
return null;
}
}
const { content: parsedYaml, fileFormat } = parseYaml(
content,
filePath,
settings.options || {}
);
return {
content: parsedYaml,
fileName: relativePath,
fileFormat,
fileId: hashStringSync(relativePath),
versionId: hashStringSync(parsedYaml),
locale: settings.defaultLocale,
} satisfies FileToUpload;
})
.filter((file) => {
if (!file || typeof file.content !== 'string' || !file.content.trim()) {
logger.warn(
`Skipping ${file?.fileName ?? 'unknown'}: YAML file is empty`
);
recordWarning(
'skipped_file',
file?.fileName ?? 'unknown',
'YAML file is empty'
);
return false;
}
return true;
});
allFiles.push(...yamlFiles.filter((file) => file !== null));
}
for (const fileType of SUPPORTED_FILE_EXTENSIONS) {
if (fileType === 'json' || fileType === 'yaml') continue;
if (filePaths[fileType]) {
const files = filePaths[fileType]
.map((filePath) => {
const content = readFile(filePath);
const relativePath = getRelative(filePath);
const processed = preprocessContent(
content,
relativePath,
fileType,
settings
);
if (typeof processed !== 'string') {
logger.warn(`Skipping ${relativePath}: ${processed.skip}`);
recordWarning('skipped_file', relativePath, processed.skip);
return null;
}
return {
content: processed,
fileName: relativePath,
fileFormat: fileType.toUpperCase() as FileFormat,
fileId: hashStringSync(relativePath),
versionId: hashStringSync(content),
locale: settings.defaultLocale,
} satisfies FileToUpload;
})
.filter((file) => {
if (
!file ||
typeof file.content !== 'string' ||
!file.content.trim()
) {
logger.warn(
`Skipping ${file?.fileName ?? 'unknown'}: File is empty after sanitization`
);
recordWarning(
'skipped_file',
file?.fileName ?? 'unknown',
'File is empty after sanitization'
);
return false;
}
return true;
});
allFiles.push(...files.filter((file) => file !== null));
}
}
if (allFiles.length === 0 && !settings.publish) {
logger.error(
'No files to translate were found. Please check your configuration and try again.'
);
}
return allFiles;
}