qwik-speak
Version:
Internationalization (i18n) library to translate texts, dates and numbers in Qwik apps
74 lines (73 loc) • 2.84 kB
JavaScript
import { qwikSpeakExtract as u } from "./index.js";
/**
* @license
* Qwik Speak
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/robisim74/qwik-speak/blob/main/LICENSE
*/
function l(o) {
const s = o.split("=");
if (s.length === 2 && s[0].startsWith("--")) {
const e = s[0].slice(2), i = /,/.test(s[1]) ? s[1].split(",") : s[1];
return { key: e, value: i };
}
return { key: "error", value: `- wrong option: "${s[0]}"` };
}
const t = (o, s) => !!(s === o || s === "array" && Array.isArray(o) || s === "string" && typeof o == "string" || s === "boolean" && typeof o == "boolean"), n = (o, s) => `- option "${o}": wrong value ${JSON.stringify(s)}`, p = (o) => `- missing option: "${o}"`, c = process.argv.slice(2), r = {}, a = [];
for (const o of c) {
const { key: s, value: e } = l(o);
switch (s) {
case "basePath":
t(e, "string") ? r.basePath = e : a.push(n(s, e));
break;
case "sourceFilesPaths":
t(e, "array") ? r.sourceFilesPaths = e : t(e, "string") ? r.sourceFilesPaths = [e] : a.push(n(s, e));
break;
case "excludedPaths":
t(e, "array") ? r.excludedPaths = e : t(e, "string") ? r.excludedPaths = [e] : a.push(n(s, e));
break;
case "assetsPath":
t(e, "string") ? r.assetsPath = e : a.push(n(s, e));
break;
case "format":
t(e, "json") ? r.format = e : a.push(n(s, e));
break;
case "filename":
t(e, "string") ? r.filename = e : a.push(n(s, e));
break;
case "supportedLangs":
t(e, "array") ? r.supportedLangs = e : t(e, "string") ? r.supportedLangs = [e] : a.push(n(s, e));
break;
case "keySeparator":
t(e, "string") ? r.keySeparator = e : a.push(n(s, e));
break;
case "keyValueSeparator":
t(e, "string") ? r.keyValueSeparator = e : a.push(n(s, e));
break;
case "autoKeys":
t(e, "boolean") ? r.autoKeys = e : t(e, "string") && (e === "true" || e === "false") ? r.autoKeys = e === "true" : a.push(n(s, e));
break;
case "unusedKeys":
t(e, "boolean") ? r.unusedKeys = e : t(e, "string") && (e === "true" || e === "false") ? r.unusedKeys = e === "true" : a.push(n(s, e));
break;
case "runtimeAssets":
t(e, "array") ? r.runtimeAssets = e : t(e, "string") ? r.runtimeAssets = [e] : a.push(n(s, e));
break;
case "error":
a.push(e);
break;
default:
a.push(`- unknown option: "${s}"`);
}
}
r.supportedLangs || a.push(p("supportedLangs"));
if (a.length > 0) {
console.log("\x1B[31m%s\x1B[0m", "Qwik Speak Extract errors:");
for (const o of a)
console.log(o);
process.exit(1);
}
console.log("\x1B[36m%s\x1B[0m", "Qwik Speak Extract");
console.log("\x1B[32m%s\x1B[0m", "extracting translation...");
u(r);