Spittal / vue-i18n-extract

Manage vue-i18n localization with static analysis
https://pixari.github.io/vue-i18n-extract/#what-is-it
MIT License
310 stars 85 forks source link

Can you provide additional show console options to createI18NReport? #215

Open joojaewoo opened 9 months ago

joojaewoo commented 9 months ago

Can you provide additional options to createI18NReport? I don't want to see the console for the missingKey list. Could you please add the corresponding console exposure option? like this

export async function createI18NReport (options: ReportOptions): Promise<I18NReport> {
  const {
    vueFiles: vueFilesGlob,
    languageFiles: languageFilesGlob,
    output,
    add,
    remove,
    exclude = [],
    ci,
    separator,
    noEmptyTranslation = '',
    missingTranslationString = '',
    // get show console option
    showMissingKeys = true,
    showUnusedKeys = true,
    detect = [DetectionType.Missing, DetectionType.Unused, DetectionType.Dynamic]
  } = options;

  if (!vueFilesGlob) throw new Error('Required configuration vueFiles is missing.');
  if (!languageFilesGlob) throw new Error('Required configuration languageFiles is missing.');

  let issuesToDetect = Array.isArray(detect) ? detect : [detect];
  const invalidDetectOptions = issuesToDetect.filter(item => !Object.values(DetectionType).includes(item));
  if (invalidDetectOptions.length) {
    throw new Error(`Invalid 'detect' value(s): ${invalidDetectOptions}`);
  }

  const dot = typeof separator === 'string' ? new Dot(separator) : Dot;
  const vueFiles = readVueFiles(path.resolve(process.cwd(), vueFilesGlob));
  const languageFiles = readLanguageFiles(path.resolve(process.cwd(), languageFilesGlob));

  const I18NItems = extractI18NItemsFromVueFiles(vueFiles);
  const I18NLanguage = extractI18NLanguageFromLanguageFiles(languageFiles, dot);

  const report = extractI18NReport(I18NItems, I18NLanguage, issuesToDetect);

  report.unusedKeys = report.unusedKeys.filter(key =>
      !exclude.filter(excluded => key.path.startsWith(excluded)).length)
  // use show console option
  if (showMissingKeys && report.missingKeys.length) console.info('\nMissing Keys'), console.table(report.missingKeys);
  if (showUnusedKeys && report.unusedKeys.length) console.info('\nUnused Keys'), console.table(report.unusedKeys);
  if (report.maybeDynamicKeys.length) console.warn('\nSuspected Dynamic Keys Found\nvue-i18n-extract does not compile Vue templates and therefore can not infer the correct key for the following keys.'), console.table(report.maybeDynamicKeys);

  if (output) {
    await writeReportToFile(report, path.resolve(process.cwd(), output));
    console.info(`\nThe report has been has been saved to ${output}`);
  }

  if (remove && report.unusedKeys.length) {
    removeUnusedFromLanguageFiles(languageFiles, report.unusedKeys, dot);
    console.info('\nThe unused keys have been removed from your language files.');
  }

  if (add && report.missingKeys.length) {
    writeMissingToLanguageFiles(languageFiles, report.missingKeys, dot, noEmptyTranslation, missingTranslationString);
    console.info('\nThe missing keys have been added to your language files.');
  }

  if (ci && report.missingKeys.length) {
    throw new Error(`${report.missingKeys.length} missing keys found.`);
  }

  if (ci && report.unusedKeys.length) {
    throw new Error(`${report.unusedKeys.length} unused keys found.`);
  }

  return report;
}
storageddd commented 9 months ago

Actual and for me :)

Next solution is temporary solving issue:

disableConsole();
const result = await VueI18NExtract.createI18NReport({
  vueFiles: this.vueFiles,
  languageFiles: this.languageFiles,
  exclude: systemHints
});
enableConsole();
const originMethods: Record<string, typeof Function> = {};
const emptyFn = () => {};

[].concat
  // @ts-ignore
  .call(Object.keys(console), Object.keys(console.__proto__))
  .filter(prop => typeof console[prop] === 'function')
  .forEach(fn => {
    originMethods[fn] = (console[fn] as typeof Function).bind(console);
  });

export const disableConsole = () => {
  Object.keys(originMethods).forEach(fn => {
    // @ts-ignore
    console[fn] = emptyFn;
  });
};

export const enableConsole = () => {
  Object.keys(originMethods).forEach(fn => {
    // @ts-ignore
    console[fn] = originMethods[fn];
  });
};