mirror of
https://github.com/scratchfoundation/scratch-l10n.git
synced 2025-07-04 01:51:01 -04:00
style: apply new eslint-config-scratch style, fix lint
This commit is contained in:
parent
fb18199b4c
commit
57638d450c
35 changed files with 1415 additions and 1449 deletions
|
@ -1,86 +1,83 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* @fileoverview
|
||||
* @file
|
||||
* Script to validate extension block input placeholders
|
||||
*/
|
||||
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import async from 'async';
|
||||
import assert from 'assert';
|
||||
import locales from '../src/supported-locales.mjs';
|
||||
import assert from 'assert'
|
||||
import async from 'async'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import locales from '../src/supported-locales.mjs'
|
||||
|
||||
// Globals
|
||||
const JSON_DIR = path.join(process.cwd(), '/editor/extensions');
|
||||
const source = JSON.parse(fs.readFileSync(`${JSON_DIR}/en.json`));
|
||||
const JSON_DIR = path.join(process.cwd(), '/editor/extensions')
|
||||
const source = JSON.parse(fs.readFileSync(`${JSON_DIR}/en.json`))
|
||||
|
||||
// Matches everything inside brackets, and the brackets themselves.
|
||||
// e.g. matches '[MOTOR_ID]', '[POWER]' from 'altera a potência de [MOTOR_ID] para [POWER]'
|
||||
const blockInputRegex = /\[.+?\]/g;
|
||||
const blockInputRegex = /\[.+?\]/g
|
||||
|
||||
let numTotalErrors = 0;
|
||||
let numTotalErrors = 0
|
||||
|
||||
const validateExtensionInputs = (translationData, locale) => {
|
||||
let numLocaleErrors = 0;
|
||||
for (const block of Object.keys(translationData)) {
|
||||
const englishBlockInputs = source[block].match(blockInputRegex);
|
||||
let numLocaleErrors = 0
|
||||
for (const block of Object.keys(translationData)) {
|
||||
const englishBlockInputs = source[block].match(blockInputRegex)
|
||||
|
||||
if (!englishBlockInputs) continue;
|
||||
if (!englishBlockInputs) continue
|
||||
|
||||
// If null (meaning no matches), that means that English block inputs exist but translated ones don't.
|
||||
// Coerce it to an empty array so that the assertion below fails, instead of getting the less-helpful error
|
||||
// that we can't call Array.includes on null.
|
||||
const translatedBlockInputs = translationData[block].match(blockInputRegex) || [];
|
||||
// If null (meaning no matches), that means that English block inputs exist but translated ones don't.
|
||||
// Coerce it to an empty array so that the assertion below fails, instead of getting the less-helpful error
|
||||
// that we can't call Array.includes on null.
|
||||
const translatedBlockInputs = translationData[block].match(blockInputRegex) || []
|
||||
|
||||
for (const input of englishBlockInputs) {
|
||||
// Currently there are enough errors here that it would be tedious to fix an error, rerun this tool
|
||||
// to find the next error, and repeat. So, catch the assertion error and add to the number of total errors.
|
||||
// This allows all errors to be displayed when the command is run, rather than just the first encountered.
|
||||
try {
|
||||
assert(
|
||||
translatedBlockInputs.includes(input),
|
||||
for (const input of englishBlockInputs) {
|
||||
// Currently there are enough errors here that it would be tedious to fix an error, rerun this tool
|
||||
// to find the next error, and repeat. So, catch the assertion error and add to the number of total errors.
|
||||
// This allows all errors to be displayed when the command is run, rather than just the first encountered.
|
||||
try {
|
||||
assert(
|
||||
translatedBlockInputs.includes(input),
|
||||
|
||||
`Block '${block}' in locale '${locale}' does not include input ${input}:\n` +
|
||||
translationData[block]
|
||||
);
|
||||
} catch (err) {
|
||||
numLocaleErrors++;
|
||||
console.error(err.message + '\n'); // eslint-disable-line no-console
|
||||
}
|
||||
}
|
||||
`Block '${block}' in locale '${locale}' does not include input ${input}:\n` + translationData[block],
|
||||
)
|
||||
} catch (err) {
|
||||
numLocaleErrors++
|
||||
console.error(err.message + '\n')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (numLocaleErrors > 0) {
|
||||
numTotalErrors += numLocaleErrors;
|
||||
throw new Error(`${numLocaleErrors} total error(s) for locale '${locale}'`);
|
||||
}
|
||||
};
|
||||
if (numLocaleErrors > 0) {
|
||||
numTotalErrors += numLocaleErrors
|
||||
throw new Error(`${numLocaleErrors} total error(s) for locale '${locale}'`)
|
||||
}
|
||||
}
|
||||
|
||||
const validate = (locale, callback) => {
|
||||
fs.readFile(`${JSON_DIR}/${locale}.json`, function (err, data) {
|
||||
if (err) callback(err);
|
||||
// let this throw an error if invalid json
|
||||
data = JSON.parse(data);
|
||||
fs.readFile(`${JSON_DIR}/${locale}.json`, (err, data) => {
|
||||
if (err) callback(err)
|
||||
// let this throw an error if invalid json
|
||||
data = JSON.parse(data)
|
||||
|
||||
try {
|
||||
validateExtensionInputs(data, locale);
|
||||
} catch (error) {
|
||||
console.error(error.message + '\n'); // eslint-disable-line no-console
|
||||
}
|
||||
|
||||
callback();
|
||||
});
|
||||
};
|
||||
|
||||
async.each(Object.keys(locales), validate, function (err) {
|
||||
if (err) {
|
||||
console.error(err); // eslint-disable-line no-console
|
||||
process.exit(1);
|
||||
try {
|
||||
validateExtensionInputs(data, locale)
|
||||
} catch (error) {
|
||||
console.error(error.message + '\n')
|
||||
}
|
||||
|
||||
if (numTotalErrors > 0) {
|
||||
console.error(`${numTotalErrors} total extension input error(s)`); // eslint-disable-line no-console
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
callback()
|
||||
})
|
||||
}
|
||||
|
||||
async.each(Object.keys(locales), validate, err => {
|
||||
if (err) {
|
||||
console.error(err)
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
if (numTotalErrors > 0) {
|
||||
console.error(`${numTotalErrors} total extension input error(s)`)
|
||||
process.exit(1)
|
||||
}
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue