web: Check JS files. Add types.
This commit is contained in:
committed by
Teffen Ellis
parent
40f598f3f1
commit
92629578dd
@ -1,17 +1,33 @@
|
|||||||
import { spawnSync } from "child_process";
|
/**
|
||||||
import fs from "fs";
|
* @file Lit Localize build script.
|
||||||
import path from "path";
|
*
|
||||||
import process from "process";
|
* @remarks
|
||||||
|
* Determines if all the Xliff translation source files are present and
|
||||||
|
* if the Typescript source files generated from those sources are up-to-date.
|
||||||
|
*
|
||||||
|
* If they are not, it runs the locale building script, intercepting the
|
||||||
|
* long spew of "this string is not translated" and replacing it with a
|
||||||
|
* summary of how many strings are missing with respect to the source locale.
|
||||||
|
*
|
||||||
|
* @import { ConfigFile } from "@lit/localize-tools/lib/types/config"
|
||||||
|
*/
|
||||||
|
import { PackageRoot } from "@goauthentik/web/paths";
|
||||||
|
import { spawnSync } from "node:child_process";
|
||||||
|
import { readFileSync, statSync } from "node:fs";
|
||||||
|
import path from "node:path";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determines if all the Xliff translation source files are present and if the Typescript source
|
* @type {ConfigFile}
|
||||||
* files generated from those sources are up-to-date. If they are not, it runs the locale building
|
|
||||||
* script, intercepting the long spew of "this string is not translated" and replacing it with a
|
|
||||||
* summary of how many strings are missing with respect to the source locale.
|
|
||||||
*/
|
*/
|
||||||
|
const localizeRules = JSON.parse(
|
||||||
|
readFileSync(path.join(PackageRoot, "lit-localize.json"), "utf-8"),
|
||||||
|
);
|
||||||
|
|
||||||
const localizeRules = JSON.parse(fs.readFileSync("./lit-localize.json", "utf-8"));
|
/**
|
||||||
|
*
|
||||||
|
* @param {string} loc
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
function generatedFileIsUpToDateWithXliffSource(loc) {
|
function generatedFileIsUpToDateWithXliffSource(loc) {
|
||||||
const xliff = path.join("./xliff", `${loc}.xlf`);
|
const xliff = path.join("./xliff", `${loc}.xlf`);
|
||||||
const gened = path.join("./src/locales", `${loc}.ts`);
|
const gened = path.join("./src/locales", `${loc}.ts`);
|
||||||
@ -22,7 +38,7 @@ function generatedFileIsUpToDateWithXliffSource(loc) {
|
|||||||
// generates a unique error message and halts the build.
|
// generates a unique error message and halts the build.
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var xlfStat = fs.statSync(xliff);
|
var xlfStat = statSync(xliff);
|
||||||
} catch (_error) {
|
} catch (_error) {
|
||||||
console.error(`lit-localize expected '${loc}.xlf', but XLF file is not present`);
|
console.error(`lit-localize expected '${loc}.xlf', but XLF file is not present`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
@ -30,7 +46,7 @@ function generatedFileIsUpToDateWithXliffSource(loc) {
|
|||||||
|
|
||||||
// If the generated file doesn't exist, of course it's not up to date.
|
// If the generated file doesn't exist, of course it's not up to date.
|
||||||
try {
|
try {
|
||||||
var genedStat = fs.statSync(gened);
|
var genedStat = statSync(gened);
|
||||||
} catch (_error) {
|
} catch (_error) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -35,6 +35,11 @@ const __dirname = fileURLToPath(new URL(".", import.meta.url));
|
|||||||
const projectRoot = path.join(__dirname, "..");
|
const projectRoot = path.join(__dirname, "..");
|
||||||
process.chdir(projectRoot);
|
process.chdir(projectRoot);
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {string[]} flags
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
const hasFlag = (flags) => process.argv.length > 1 && flags.includes(process.argv[2]);
|
const hasFlag = (flags) => process.argv.length > 1 && flags.includes(process.argv[2]);
|
||||||
|
|
||||||
const [configFile, files] = hasFlag(["-n", "--nightmare"])
|
const [configFile, files] = hasFlag(["-n", "--nightmare"])
|
||||||
|
|||||||
@ -1,22 +1,36 @@
|
|||||||
import { readFileSync } from "fs";
|
/**
|
||||||
import path from "path";
|
* @file Pseudo-localization script.
|
||||||
|
*
|
||||||
|
* @import { ConfigFile } from "@lit/localize-tools/lib/types/config.js"
|
||||||
|
* @import { Config } from '@lit/localize-tools/lib/types/config.js';
|
||||||
|
* @import { ProgramMessage } from "@lit/localize-tools/src/messages.js"
|
||||||
|
* @import { Locale } from "@lit/localize-tools/src/types/locale.js"
|
||||||
|
*/
|
||||||
|
import { PackageRoot } from "@goauthentik/web/paths";
|
||||||
|
import { readFileSync } from "node:fs";
|
||||||
|
import path from "node:path";
|
||||||
import pseudolocale from "pseudolocale";
|
import pseudolocale from "pseudolocale";
|
||||||
import { fileURLToPath } from "url";
|
|
||||||
|
|
||||||
import { makeFormatter } from "@lit/localize-tools/lib/formatters/index.js";
|
import { makeFormatter } from "@lit/localize-tools/lib/formatters/index.js";
|
||||||
import { sortProgramMessages } from "@lit/localize-tools/lib/messages.js";
|
import { sortProgramMessages } from "@lit/localize-tools/lib/messages.js";
|
||||||
import { TransformLitLocalizer } from "@lit/localize-tools/lib/modes/transform.js";
|
import { TransformLitLocalizer } from "@lit/localize-tools/lib/modes/transform.js";
|
||||||
|
|
||||||
const __dirname = fileURLToPath(new URL(".", import.meta.url));
|
const pseudoLocale = /** @type {Locale} */ ("pseudo-LOCALE");
|
||||||
const pseudoLocale = "pseudo-LOCALE";
|
|
||||||
const targetLocales = [pseudoLocale];
|
const targetLocales = [pseudoLocale];
|
||||||
const baseConfig = JSON.parse(readFileSync(path.join(__dirname, "../lit-localize.json"), "utf-8"));
|
|
||||||
|
/**
|
||||||
|
* @type {ConfigFile}
|
||||||
|
*/
|
||||||
|
const baseConfig = JSON.parse(readFileSync(path.join(PackageRoot, "lit-localize.json"), "utf-8"));
|
||||||
|
|
||||||
// Need to make some internal specifications to satisfy the transformer. It doesn't actually matter
|
// Need to make some internal specifications to satisfy the transformer. It doesn't actually matter
|
||||||
// which Localizer we use (transformer or runtime), because all of the functionality we care about
|
// which Localizer we use (transformer or runtime), because all of the functionality we care about
|
||||||
// is in their common parent class, but I had to pick one. Everything else here is just pure
|
// is in their common parent class, but I had to pick one. Everything else here is just pure
|
||||||
// exploitation of the lit/localize-tools internals.
|
// exploitation of the lit/localize-tools internals.
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @satisfies {Config}
|
||||||
|
*/
|
||||||
const config = {
|
const config = {
|
||||||
...baseConfig,
|
...baseConfig,
|
||||||
baseDir: path.join(__dirname, ".."),
|
baseDir: path.join(__dirname, ".."),
|
||||||
@ -28,6 +42,11 @@ const config = {
|
|||||||
resolve: (path) => path,
|
resolve: (path) => path,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {ProgramMessage} message
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
const pseudoMessagify = (message) => ({
|
const pseudoMessagify = (message) => ({
|
||||||
name: message.name,
|
name: message.name,
|
||||||
contents: message.contents.map((content) =>
|
contents: message.contents.map((content) =>
|
||||||
@ -36,7 +55,7 @@ const pseudoMessagify = (message) => ({
|
|||||||
});
|
});
|
||||||
|
|
||||||
const localizer = new TransformLitLocalizer(config);
|
const localizer = new TransformLitLocalizer(config);
|
||||||
const messages = localizer.extractSourceMessages().messages;
|
const { messages } = localizer.extractSourceMessages();
|
||||||
const translations = messages.map(pseudoMessagify);
|
const translations = messages.map(pseudoMessagify);
|
||||||
const sorted = sortProgramMessages([...messages]);
|
const sorted = sortProgramMessages([...messages]);
|
||||||
const formatter = makeFormatter(config);
|
const formatter = makeFormatter(config);
|
||||||
|
|||||||
@ -2,6 +2,9 @@
|
|||||||
{
|
{
|
||||||
"extends": "@goauthentik/tsconfig",
|
"extends": "@goauthentik/tsconfig",
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
|
"checkJs": true,
|
||||||
|
"allowJs": true,
|
||||||
|
"resolveJsonModule": true,
|
||||||
"allowSyntheticDefaultImports": true,
|
"allowSyntheticDefaultImports": true,
|
||||||
"emitDeclarationOnly": true,
|
"emitDeclarationOnly": true,
|
||||||
"experimentalDecorators": true,
|
"experimentalDecorators": true,
|
||||||
|
|||||||
Reference in New Issue
Block a user