[READ-ONLY] a fast, modern browser for the npm registry
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

chore: prevent adding missing entries to wrong locale files (#1157)

authored by

Joaquín Sánchez and committed by
GitHub
3b077e16 921d3f55

+22 -4
+1 -1
lunaria/prepare-json-files.ts
··· 88 88 89 89 await fs.writeFile( 90 90 path.resolve(`${destFolder}/${locale.code}.json`), 91 - JSON.stringify(source, null, 2), 91 + `${JSON.stringify(source, null, 2)}\n`, 92 92 'utf-8', 93 93 ) 94 94 }
+21 -3
scripts/compare-translations.ts
··· 209 209 } 210 210 211 211 const processLocale = async ( 212 + singleLocale: boolean, 212 213 localeFile: string, 213 214 referenceContent: NestedObject, 214 215 fix = false, 215 216 ): Promise<SyncStats> => { 216 217 const filePath = join(LOCALES_DIRECTORY, localeFile) 217 218 const localeInfo = checkJsonName(filePath) 219 + 220 + // prevent updating wrong locale file: 221 + // - language locale files at countries allowed: e.g. es.json 222 + // - country locale file forbidden: e.g. es-ES.json 223 + // - target locale file forbidden: e.g. es-419.json 224 + if (fix && localeInfo.mergeLocale && singleLocale) { 225 + console.error( 226 + `${COLORS.red}Error: Locale "${localeInfo.locale}" cannot be fixed, fix the ${localeInfo.lang} locale instead!${COLORS.reset}`, 227 + ) 228 + process.exit(1) 229 + } 230 + 218 231 const targetContent = await loadJson(localeInfo) 219 232 220 233 const stats: SyncStats = { ··· 226 239 const newContent = syncLocaleData(referenceContent, targetContent, stats, fix) 227 240 228 241 // Write if there are removals (always) or we are in fix mode 229 - if (stats.extra.length > 0 || fix) { 242 + if (!localeInfo.mergeLocale && (stats.extra.length > 0 || fix)) { 230 243 writeFileSync(filePath, JSON.stringify(newContent, null, 2) + '\n', 'utf-8') 231 244 } 232 245 ··· 246 259 process.exit(1) 247 260 } 248 261 249 - const { missing, extra, referenceKeys } = await processLocale(localeFile, referenceContent, fix) 262 + const { missing, extra, referenceKeys } = await processLocale( 263 + true, 264 + localeFile, 265 + referenceContent, 266 + fix, 267 + ) 250 268 251 269 console.log( 252 270 `${COLORS.cyan}=== Missing keys for ${localeFile}${fix ? ' (with --fix)' : ''} ===${COLORS.reset}`, ··· 286 304 let totalAdded = 0 287 305 288 306 for (const localeFile of localeFiles) { 289 - const stats = await processLocale(localeFile, referenceContent, fix) 307 + const stats = await processLocale(false, localeFile, referenceContent, fix) 290 308 results.push({ 291 309 file: localeFile, 292 310 ...stats,