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
const { chalk } = require(require.resolve('@vue/cli-shared-utils'))
const { resolve } = require('path')
const i18nExtract = require('vue-i18n-extract').default
 
const EXTRACT_MISSING = 1
const EXTRACT_UNUSED = 2
const EXTRACT_ALL = 3
 
const options = {
  description: 'vue-i18n report',
  usage: 'vue-cli-service i18n:report [options]',
  options: {
    locales: 'target locale files path, required',
    src: 'target source codes path, required',
    type: 'reporting type, default `missing` and `unused` all, optional',
    output: 'create a json file out of report, optional'
  }
}
 
function resolveReportType(args) {
  let type = EXTRACT_ALL
  if (args.type === 'missing') {
    type = EXTRACT_MISSING
  } else if (args.type === 'unused') {
    type = EXTRACT_UNUSED
  }
  return type
}
 
async function service(args = {}, api) {
  if (!args.src) {
    console.log(chalk.red(`not specified 'src' argument.`))
    return
  }
 
  if (!args.locales) {
    console.log(chalk.red(`not specified 'locales' argument.`))
    return
  }
 
  const currentDir = process.cwd()
  const srcFiles = resolve(currentDir, args.src)
  const localeFiles = resolve(currentDir, args.locales)
  const extractType = resolveReportType(args)
 
  const i18nReport = i18nExtract.createI18NReport(
    srcFiles,
    localeFiles,
    extractType
  )
  i18nExtract.logI18NReport(i18nReport)
 
  if (args.output) {
    await i18nExtract.writeReportToFile(i18nReport, args.output)
  }
 
  return i18nReport
}
 
module.exports = {
  service,
  options
}