|
| 1 | +const { chalk } = require(require.resolve('@vue/cli-shared-utils')) |
| 2 | +const { resolve } = require('path') |
| 3 | +const i18nExtract = require('vue-i18n-extract').default |
| 4 | + |
| 5 | +const EXTRACT_MISSING = 1 |
| 6 | +const EXTRACT_UNUSED = 2 |
| 7 | +const EXTRACT_ALL = 3 |
| 8 | + |
| 9 | +const options = { |
| 10 | + description: 'vue-i18n report', |
| 11 | + usage: 'vue-cli-service i18n:report [options]', |
| 12 | + options: { |
| 13 | + locales: 'target locale files path, required', |
| 14 | + src: 'target source codes path, required', |
| 15 | + type: 'reporting type, default `missing` and `unused` all, optional', |
| 16 | + output: 'create a json file out of report, optional' |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +function resolveReportType (args) { |
| 21 | + let type = EXTRACT_ALL |
| 22 | + if (args.type === 'missing') { |
| 23 | + type = EXTRACT_MISSING |
| 24 | + } else if (args.type === 'unused') { |
| 25 | + type = EXTRACT_UNUSED |
| 26 | + } |
| 27 | + return type |
| 28 | +} |
| 29 | + |
| 30 | +async function service (args = {}, api) { |
| 31 | + if (!args.src) { |
| 32 | + console.log(chalk.red(`not specified 'src' argument.`)) |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + if (!args.locales) { |
| 37 | + console.log(chalk.red(`not specified 'locales' argument.`)) |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + const currentDir = process.cwd() |
| 42 | + const srcFiles = resolve(currentDir, args.src) |
| 43 | + const localeFiles = resolve(currentDir, args.locales) |
| 44 | + const extractType = resolveReportType(args) |
| 45 | + |
| 46 | + const i18nReport = i18nExtract.createI18NReport(srcFiles, localeFiles, extractType) |
| 47 | + i18nExtract.logI18NReport(i18nReport) |
| 48 | + |
| 49 | + if (args.output) { |
| 50 | + await i18nExtract.writeReportToFile(i18nReport, args.output) |
| 51 | + } |
| 52 | + |
| 53 | + return i18nReport |
| 54 | +} |
| 55 | + |
| 56 | +module.exports = { |
| 57 | + service, |
| 58 | + options |
| 59 | +} |
0 commit comments