diff --git a/.eslintrc.json b/.eslintrc.json index efff6bd..7950b8d 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -298,12 +298,16 @@ "sort-keys": "off", "sort-vars": "off", "space-before-blocks": "error", - "space-before-function-paren": "off", + "space-before-function-paren": ["error", { + "anonymous": "always", + "named": "never", + "asyncArrow": "always" + }], "space-in-parens": [ "error", "never" ], - "space-infix-ops": "off", + "space-infix-ops": ["error", { "int32Hint": false }], "space-unary-ops": "error", "spaced-comment": "off", "strict": [ diff --git a/Makefile b/Makefile index cf4b247..f2b9c4d 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,10 @@ # -*- Makefile -*- -PORT = 3000 +ESLINT?=eslint + +ifeq (, $(shell which $(firstword ${ESLINT}))) + ESLINT?=npx eslint +endif help: @echo make lang: build translation files @@ -11,11 +15,7 @@ lang: cd l10n; make eslint: - -npx eslint server/trivabble-server.js - -npx eslint public/alert.js - -npx eslint public/config.js - -npx eslint public/l10n.js - -npx eslint public/trivabble.js + -${ESLINT} **/*.js start-dev-server: cd server && make start-dev-server diff --git a/l10n/makejs.js b/l10n/makejs.js index bef4edd..e278d27 100755 --- a/l10n/makejs.js +++ b/l10n/makejs.js @@ -1,104 +1,117 @@ #!/usr/bin/env node +/*eslint strict: [2, "global"]*/ +/*eslint no-sync: ["error", { allowAtRootLevel: true }]*/ -var ROOT = "../public/l10n/"; +"use strict"; -/* Builds translation files.*/ +const ROOT = "../public/l10n/"; -var fs = require('fs'); +/* Builds translation files. */ -var langs = fs.readdirSync("po"); -var po, i, len; +const fs = require("fs"); + +let po; +let i; +let len; function skipLine() { - while (i < len && po[i] !== '\n') { - ++i; - } - ++i; + while (i < len && po[i] !== "\n") { + ++i; + } + ++i; } function skipSpaces() { - while (i < len && !po[i].trim()) { - ++i; - } + while (i < len && !po[i].trim()) { + ++i; + } } function parseString() { - skipSpaces(); - if (po[i] !== '"') { - return ""; - } - ++i; - var deb = i, end; - while (i < len) { - if (po[i] === "\\") { - ++i; - } else if (po[i] === '"') { - var str1 = po.substring(deb, i++); - var end = i; - skipSpaces(); - var ndeb = i; - var str2 = parseString(); - if (i === ndeb) { // we did not parse anything - i = end; - return str1; - } + skipSpaces(); + if (po[i] !== '"') { + return ""; + } + ++i; + const deb = i; - return str1 + str2; - } - ++i; - } - throw new Error("not ended string at character " + deb); -} - -for (var l in langs) { - var lang = langs[l]; - var jsFile = fs.openSync(ROOT + "js/" + lang + ".js", "w"); - fs.writeSync(jsFile, "(function(){var "); - var poFiles = fs.readdirSync("po/" + lang); - for (var p in poFiles) { - var poFile = poFiles[p]; - - var translationFunction = fs.readFileSync("pot/" + poFile + 't', {encoding:'utf-8'}) - .match(/\#TranslationFunction[\s]+([\S]+)/)[1]; - - fs.writeSync(jsFile, "_=" + translationFunction + ".l10n;"); - - po = fs.readFileSync("po/" + lang + '/' + poFile, {encoding:'utf-8'}); - - i = 0; len = po.length; - while (i < len) { - skipSpaces(); - - if (po[i] === '#') { - skipLine(); - continue; - } - - if (po.substr(i, 5) === "msgid") { - if (po[i+5].trim() && po[i+5] !== '"') { - skipLine(); // don't understand this line - continue; - } - i+=5; + while (i < len) { + if (po[i] === "\\") { + ++i; + } else if (po[i] === '"') { + const str1 = po.substring(deb, i++); + const end = i; skipSpaces(); - msgid = parseString(); - } else if (po.substr(i, 6) === "msgstr") { - if (po[i+6].trim() && po[i+6] !== '"') { - skipLine(); // don't understand this line - continue; - } - i+=6; - msgstr = parseString(); - fs.writeSync(jsFile, '_("' + lang + '","' + msgid.replace(/\n/g,"") + '","' + msgstr.replace(/\n/g,"") + '");'); - } - skipLine(); - } - } + const ndeb = i; + const str2 = parseString(); - fs.writeSync(jsFile, "if(" + translationFunction + ".applyL10n){" + translationFunction + ".applyL10n();}})();"); - fs.close(jsFile, function (e) { - if (e) { - console.error(e); - } - }); + if (i === ndeb) { // we did not parse anything + i = end; + return str1; + } + + return str1 + str2; + } + ++i; + } + throw new Error("not ended string at character " + deb); +} + +let msgid; +let msgstr; + +for (const lang of fs.readdirSync("po")) { + const jsFile = fs.openSync(ROOT + "js/" + lang + ".js", "w"); + fs.writeSync(jsFile, "(function(){var "); + let translationFunction = "translationFunction"; + + for (const poFile of fs.readdirSync("po/" + lang)) { + translationFunction = fs.readFileSync("pot/" + poFile + "t", {encoding: "utf-8"}) + .match(/#TranslationFunction[\s]+(?[\S]+)/u).groups.functionName; + + fs.writeSync(jsFile, "_=" + translationFunction + ".l10n;"); + + po = fs.readFileSync("po/" + lang + "/" + poFile, {encoding: "utf-8"}); + + i = 0; + len = po.length; + while (i < len) { + skipSpaces(); + + if (po.substr(i, 5) === "msgid") { + if (po[i + 5].trim() && po[i + 5] !== '"') { + skipLine(); // don't understand this line + } else { + i += 5; + skipSpaces(); + msgid = parseString(); + } + } else if (po.substr(i, 6) === "msgstr") { + if (po[i + 6].trim() && po[i + 6] !== '"') { + skipLine(); // don't understand this line + } else { + i += 6; + msgstr = parseString(); + fs.writeSync( + jsFile, + '_("' + lang + '","' + msgid.replace(/\n/gu, "") + '","' + msgstr.replace(/\n/gu, "") + '");' + ); + } + } + + // if po[i] === "#", ignore + skipLine(); + } + } + + fs.writeSync( + jsFile, + "if(" + translationFunction + ".applyL10n){" + translationFunction + ".applyL10n();}})();" + ); + + fs.close(jsFile, function (e) { + if (e) { + console.error(e); + } + }); } diff --git a/public/alert.js b/public/alert.js index d31cdc2..8a2b7c4 100644 --- a/public/alert.js +++ b/public/alert.js @@ -14,7 +14,9 @@ let alertInput; let divAlertContent; - const _ = (window.libD && libD.l10n) ? libD.l10n() : function (s) {return s;}; + const _ = (window.libD && libD.l10n) ? libD.l10n() : function (s) { + return s; + }; function promptOK() { divAlert.style.display = "none"; diff --git a/public/index.html b/public/index.html index 7071251..5b993e4 100644 --- a/public/index.html +++ b/public/index.html @@ -38,7 +38,7 @@ -
+