All files / src/apps/gbs-music-web/lang musicWebLocales.js

98.65% Statements 73/74
19.05% Branches 4/21
100% Functions 16/16
98.53% Lines 67/68

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159  1x 1x 1x   1x 1x 1x 1x 1x                     1x   5x   1x 1x     1x 1x   1x   1x 2x 2x 2x     1x 2x 2x 4x       1x       1x   1x 2x         2x 2x 2x 2x 2x 2x 2x           1x     1x         1x 1x 1x 1x   2x 2x   1x               1x           1x 1x 1x 1x 1x 2x     1x           1x 1x   1x   1x 2x 2x 4x 2x 1x     2x 2x             1x 1x 1x         1x               1x                
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require("fs");
const path = require("path");
const glob = require("glob");
 
const repoRoot = path.resolve(__dirname, "../../../..");
const defaultManifestPath = path.join(__dirname, "musicWebL10NManifest.json");
const defaultLocaleDir = path.join(repoRoot, "src", "lang");
const defaultOutputDir = path.join(repoRoot, "out", "music-web-locales");
const defaultScanGlobs = [
  "src/apps/gbs-music-web/**/*.{ts,tsx,js,jsx}",
  "src/components/music/**/*.{ts,tsx,js,jsx}",
  "src/shared/lib/uge/**/*.{ts,tsx,js,jsx}",
  "src/store/features/tracker/**/*.{ts,tsx,js,jsx}",
  "src/store/features/trackerDocument/**/*.{ts,tsx,js,jsx}",
  "src/store/features/clipboard/clipboardState/**/*.{ts,tsx,js,jsx}",
  "src/store/features/editor/editorState/**/*.{ts,tsx,js,jsx}",
  "src/store/features/music/musicState/**/*.{ts,tsx,js,jsx}",
  "src/store/features/navigation/navigationState/**/*.{ts,tsx,js,jsx}",
];
const literalL10NKeyPattern = /l10n\(\s*["']([A-Z0-9_-]+)["']/g;
 
const readJson = (filename) => JSON.parse(fs.readFileSync(filename, "utf8"));
 
const removeGeneratedMusicWebLocales = (outputDir = defaultOutputDir) => {
  fs.rmSync(outputDir, { recursive: true, force: true });
};
 
const listLocaleFiles = (localeDir = defaultLocaleDir) =>
  glob
    .sync(path.join(localeDir, "*.json"))
    .sort((a, b) => a.localeCompare(b, undefined, { numeric: true }));
 
const readManifestKeys = (manifestPath = defaultManifestPath) => {
  const manifest = readJson(manifestPath);
  const keys = Array.isArray(manifest.keys) ? manifest.keys : [];
  return [...new Set(keys)].sort((a, b) => a.localeCompare(b));
};
 
const filterLocaleData = (localeData, allowedKeys) => {
  const allowedKeySet = new Set(allowedKeys);
  return Object.fromEntries(
    Object.entries(localeData).filter(([key]) => allowedKeySet.has(key)),
  );
};
 
const extractStaticMusicWebL10NKeys = ({
  rootDir = repoRoot,
  scanGlobs = defaultScanGlobs,
} = {}) => {
  const keys = new Set();
 
  for (const pattern of scanGlobs) {
    const files = glob.sync(pattern, {
      cwd: rootDir,
      nodir: true,
    });
 
    for (const relativeFilename of files) {
      const filename = path.join(rootDir, relativeFilename);
      const source = fs.readFileSync(filename, "utf8");
      for (const match of source.matchAll(literalL10NKeyPattern)) {
        const key = match[1];
        if (key) {
          keys.add(key);
        }
      }
    }
  }
 
  return [...keys].sort((a, b) => a.localeCompare(b));
};
 
const auditMusicWebL10NManifest = ({
  rootDir = repoRoot,
  manifestPath = defaultManifestPath,
  scanGlobs = defaultScanGlobs,
} = {}) => {
  const manifestKeys = readManifestKeys(manifestPath);
  const scannedKeys = extractStaticMusicWebL10NKeys({ rootDir, scanGlobs });
  const manifestKeySet = new Set(manifestKeys);
  const scannedKeySet = new Set(scannedKeys);
 
  const missingKeys = scannedKeys.filter((key) => !manifestKeySet.has(key));
  const unusedKeys = manifestKeys.filter((key) => !scannedKeySet.has(key));
 
  return {
    manifestKeys,
    scannedKeys,
    missingKeys,
    unusedKeys,
  };
};
 
const generateMusicWebLocales = ({
  manifestPath = defaultManifestPath,
  localeDir = defaultLocaleDir,
  outputDir = defaultOutputDir,
  logger = console,
} = {}) => {
  const manifestKeys = readManifestKeys(manifestPath);
  const sourceLocaleFiles = listLocaleFiles(localeDir);
  const englishFile = path.join(localeDir, "en.json");
  const englishData = readJson(englishFile);
  const missingEnglishKeys = manifestKeys.filter(
    (key) => !(key in englishData),
  );
 
  Iif (missingEnglishKeys.length > 0) {
    throw new Error(
      `music web locale manifest references missing English keys: ${missingEnglishKeys.join(", ")}`,
    );
  }
 
  removeGeneratedMusicWebLocales(outputDir);
  fs.mkdirSync(outputDir, { recursive: true });
 
  const missingByLocale = {};
 
  for (const localeFile of sourceLocaleFiles) {
    const localeName = path.basename(localeFile);
    const localeData = readJson(localeFile);
    const missingKeys = manifestKeys.filter((key) => !(key in localeData));
    if (missingKeys.length > 0) {
      missingByLocale[localeName] = missingKeys;
    }
 
    const filteredLocaleData = filterLocaleData(localeData, manifestKeys);
    fs.writeFileSync(
      path.join(outputDir, localeName),
      `${JSON.stringify(filteredLocaleData, null, 2)}\n`,
      "utf8",
    );
  }
 
  const missingLocales = Object.keys(missingByLocale);
  if (missingLocales.length > 0) {
    logger.warn(
      `music web locale generation: ${missingLocales.length} locale file(s) are missing manifest keys and will rely on English fallback`,
    );
  }
 
  return {
    manifestKeys,
    sourceLocaleFiles,
    outputDir,
    missingByLocale,
  };
};
 
module.exports = {
  auditMusicWebL10NManifest,
  defaultManifestPath,
  defaultOutputDir,
  generateMusicWebLocales,
  listLocaleFiles,
  removeGeneratedMusicWebLocales,
};