WXL
5 天以前 871522ed7e06fd9c62a87c178d7f5c88d7853a20
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
import path from 'path';
import fs from 'fs';
 
import { ReportOptions, I18NReport } from '../types';
import { parseVueFiles } from './vue-files';
import { parseLanguageFiles, LanguageFileUpdater } from './language-files';
import { extractI18NReport, VueI18NExtractReportTypes, writeReportToFile } from './report';
 
export function createI18NReport (vueFiles: string, languageFiles: string, command: ReportOptions): I18NReport {
  const resolvedVueFiles = path.resolve(process.cwd(), vueFiles);
  const resolvedLanguageFiles = path.resolve(process.cwd(), languageFiles);
 
  const parsedVueFiles = parseVueFiles(resolvedVueFiles);
  const parsedLanguageFiles = parseLanguageFiles(resolvedLanguageFiles);
 
  const reportType = command.dynamic ? VueI18NExtractReportTypes.All : (VueI18NExtractReportTypes.Missing + VueI18NExtractReportTypes.Unused);
 
  return extractI18NReport(parsedVueFiles, parsedLanguageFiles, reportType);
}
 
export function reportFromConfigCommand(): Promise<void> | void {
  try {
    const configFile = eval(fs.readFileSync(path.resolve(process.cwd(), 'vue-i18n-extract.config.js'), 'utf8'));
    return reportCommand({
      vueFiles: configFile.vueFilesPath,
      languageFiles: configFile.languageFilesPath,
      ...(configFile.options.output && {output: configFile.options.output }),
      ...(configFile.options.add && { add: Boolean(configFile.options.add) }),
      ...(configFile.options.dynamic && {dynamic: [false, 'ignore', 'report'].findIndex(e => e === configFile.options.dynamic) }),
    });
  } catch (err) {
    console.error(err);
  }
}
 
export async function reportCommand (command: ReportOptions): Promise<void> {
  const { vueFiles, languageFiles, output, add, remove, dynamic, ci } = command;
  console.log(vueFiles);
  const report = createI18NReport(vueFiles, languageFiles, command);
  const updater = new LanguageFileUpdater(languageFiles);
 
  if (report.missingKeys) console.info('missing keys: '), console.table(report.missingKeys);
  if (report.unusedKeys) console.info('unused keys: '), console.table(report.unusedKeys);
  if (report.dynamicKeys && dynamic && dynamic > 1) console.info('dynamic detected keys: '), console.table(report.dynamicKeys);
 
  if (output) {
    await writeReportToFile(report, path.resolve(process.cwd(), output));
    console.log(`The report has been has been saved to ${output}`);
  }
 
  if (add && report.missingKeys && report.missingKeys.length > 0) {
    updater.addMissingKeys(report.missingKeys);
    console.log('The missing keys have been added');
  }
 
  if (remove && report.unusedKeys && report.unusedKeys.length > 0) {
    updater.removeUnusedKeys(report.unusedKeys);
    console.log('The unused keys have been removed');
  }
 
  if (updater.hasChanges) {
    updater.writeChanges();
    console.log('Language files have been updated');
  }
 
  if (ci && Object.prototype.hasOwnProperty.call(report, 'missingKeys') && report.missingKeys !== undefined) {
    const exitCode = report.missingKeys.length > 0 ? 1 : 0;
    console.log(`[vue-i18n-extract] ${report.missingKeys.length} missing keys found.`);
    process.exit(exitCode);
  }
}
 
export * from './vue-files';
export * from './language-files';
export * from './report';