Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
joojaewoo opened this issue Jan 11, 2024 · 1 comment
Open

Comments

@joojaewoo
Copy link

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;
}
@joojaewoo joojaewoo changed the title Can you provide additional options to createI18NReport? Can you provide additional show console options to createI18NReport? Jan 11, 2024
@storageddd
Copy link

storageddd commented Jan 22, 2024

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];
  });
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants