|
| 1 | +import { flatten, unflatten } from 'flat' |
| 2 | +import glob from 'glob' |
| 3 | +import path from 'path' |
| 4 | +import fs from 'fs' |
| 5 | +import { promisify } from 'util' |
| 6 | +import { Arguments, Argv } from 'yargs' |
| 7 | + |
| 8 | +import { |
| 9 | + resolve, |
| 10 | + getLocaleMessages |
| 11 | +} from '../utils' |
| 12 | + |
| 13 | +import { |
| 14 | + Locale, |
| 15 | + LocaleMessage, |
| 16 | + LocaleMessages |
| 17 | +} from '../../types' |
| 18 | + |
| 19 | +type ListOptions = { |
| 20 | + locale: string |
| 21 | + target?: string |
| 22 | + targetPaths?: string |
| 23 | + filenameMatch?: string |
| 24 | + indent: number |
| 25 | + define: boolean |
| 26 | +} |
| 27 | + |
| 28 | +type ListFileInfo = { |
| 29 | + path: string |
| 30 | + locale: Locale |
| 31 | + message: LocaleMessage |
| 32 | +} |
| 33 | + |
| 34 | +import { debug as Debug } from 'debug' |
| 35 | +const debug = Debug('vue-i18n-locale-message:commands:list') |
| 36 | + |
| 37 | +const writeFile = promisify(fs.writeFile) |
| 38 | + |
| 39 | +class LocaleMessageUndefindError extends Error {} |
| 40 | + |
| 41 | +export const command = 'list' |
| 42 | +export const aliases = 'lt' |
| 43 | +export const describe = 'List undefined fields in locale messages' |
| 44 | + |
| 45 | +export const builder = (args: Argv): Argv<ListOptions> => { |
| 46 | + return args |
| 47 | + .option('locale', { |
| 48 | + type: 'string', |
| 49 | + alias: 'l', |
| 50 | + describe: `the main locale of locale messages`, |
| 51 | + demandOption: true |
| 52 | + }) |
| 53 | + .option('target', { |
| 54 | + type: 'string', |
| 55 | + alias: 't', |
| 56 | + describe: 'target path that locale messages file is stored, default list with the filename of target path as locale' |
| 57 | + }) |
| 58 | + .option('targetPaths', { |
| 59 | + type: 'string', |
| 60 | + alias: 'T', |
| 61 | + describe: 'target directory paths that locale messages files is stored, Can also be specified multi paths with comma delimiter' |
| 62 | + }) |
| 63 | + .option('filenameMatch', { |
| 64 | + type: 'string', |
| 65 | + alias: 'm', |
| 66 | + describe: `option should be accepted a regex filenames, must be specified together --targetPaths if it's directory path of locale messages` |
| 67 | + }) |
| 68 | + .option('define', { |
| 69 | + type: 'boolean', |
| 70 | + alias: 'd', |
| 71 | + default: false, |
| 72 | + describe: `if there are undefined fields in the target locale messages, define them with empty string and save them` |
| 73 | + }) |
| 74 | + .option('indent', { |
| 75 | + type: 'number', |
| 76 | + alias: 'i', |
| 77 | + default: 2, |
| 78 | + describe: `option for indent of locale message, if you need to adjust with --define option` |
| 79 | + }) |
| 80 | + .fail((msg, err) => { |
| 81 | + if (msg) { |
| 82 | + // TODO: should refactor console message |
| 83 | + console.error(msg) |
| 84 | + process.exit(1) |
| 85 | + } else { |
| 86 | + if (err instanceof LocaleMessageUndefindError) { |
| 87 | + // TODO: should refactor console message |
| 88 | + console.warn(err.message) |
| 89 | + process.exit(5) |
| 90 | + } else { |
| 91 | + if (err) throw err |
| 92 | + } |
| 93 | + } |
| 94 | + }) |
| 95 | +} |
| 96 | + |
| 97 | +export const handler = async (args: Arguments<ListOptions>): Promise<unknown> => { |
| 98 | + const { locale, define } = args |
| 99 | + if (!args.target && !args.targetPaths) { |
| 100 | + // TODO: should refactor console message |
| 101 | + console.error('You need to specify either --target or --target-paths') |
| 102 | + return |
| 103 | + } |
| 104 | + |
| 105 | + const localeMessages = getLocaleMessages(args) |
| 106 | + const flattedLocaleMessages = {} as LocaleMessages |
| 107 | + Object.keys(localeMessages).forEach(locale => { |
| 108 | + flattedLocaleMessages[locale] = flatten(localeMessages[locale]) |
| 109 | + }) |
| 110 | + |
| 111 | + const mainLocaleMessage = flattedLocaleMessages[locale] |
| 112 | + if (!mainLocaleMessage) { |
| 113 | + console.error(`Not found main '${locale}' locale message`) |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + let valid = true |
| 118 | + Object.keys(flattedLocaleMessages).forEach(l => { |
| 119 | + const message = flattedLocaleMessages[l] as { [prop: string]: LocaleMessage } |
| 120 | + if (!message) { |
| 121 | + console.log(`Not found '${l}' locale messages`) |
| 122 | + valid = false |
| 123 | + } else { |
| 124 | + Object.keys(mainLocaleMessage).forEach(key => { |
| 125 | + if (!message[key]) { |
| 126 | + console.log(`${l}: '${key}' undefined`) |
| 127 | + valid = false |
| 128 | + if (define) { |
| 129 | + message[key] = '' |
| 130 | + } |
| 131 | + } |
| 132 | + }) |
| 133 | + } |
| 134 | + }) |
| 135 | + |
| 136 | + if (!define && !valid) { |
| 137 | + return Promise.reject(new LocaleMessageUndefindError('there are undefined fields in the target locale messages, you can define with --define option')) |
| 138 | + } |
| 139 | + |
| 140 | + const unflattedLocaleMessages = {} as LocaleMessages |
| 141 | + Object.keys(flattedLocaleMessages).forEach(locale => { |
| 142 | + unflattedLocaleMessages[locale] = unflatten(flattedLocaleMessages[locale]) |
| 143 | + }) |
| 144 | + |
| 145 | + await tweakLocaleMessages(unflattedLocaleMessages, args) |
| 146 | + |
| 147 | + return Promise.resolve() |
| 148 | +} |
| 149 | + |
| 150 | +async function tweakLocaleMessages (messages: LocaleMessages, args: Arguments<ListOptions>) { |
| 151 | + const targets = [] as ListFileInfo[] |
| 152 | + |
| 153 | + if (args.target) { |
| 154 | + const targetPath = resolve(args.target) |
| 155 | + const parsed = path.parse(targetPath) |
| 156 | + const locale = parsed.name |
| 157 | + targets.push({ |
| 158 | + path: targetPath, |
| 159 | + locale, |
| 160 | + message: messages[locale] |
| 161 | + }) |
| 162 | + } else if (args.targetPaths) { |
| 163 | + const filenameMatch = args.filenameMatch |
| 164 | + if (!filenameMatch) { |
| 165 | + // TODO: should refactor console message |
| 166 | + throw new Error('You need to specify together --filename-match') |
| 167 | + } |
| 168 | + const targetPaths = args.targetPaths.split(',').filter(p => p) |
| 169 | + targetPaths.forEach(targetPath => { |
| 170 | + const globedPaths = glob.sync(targetPath).map(p => resolve(p)) |
| 171 | + globedPaths.forEach(fullPath => { |
| 172 | + const parsed = path.parse(fullPath) |
| 173 | + const re = new RegExp(filenameMatch, 'ig') |
| 174 | + const match = re.exec(parsed.base) |
| 175 | + debug('regex match', match, fullPath) |
| 176 | + if (match && match[1]) { |
| 177 | + const locale = match[1] |
| 178 | + if (args.locale !== locale) { |
| 179 | + targets.push({ |
| 180 | + path: fullPath, |
| 181 | + locale, |
| 182 | + message: messages[locale] |
| 183 | + }) |
| 184 | + } |
| 185 | + } else { |
| 186 | + // TODO: should refactor console message |
| 187 | + console.log(`${fullPath} is not matched with ${filenameMatch}`) |
| 188 | + } |
| 189 | + }) |
| 190 | + }) |
| 191 | + } |
| 192 | + |
| 193 | + if (args.define) { |
| 194 | + for (const fileInfo of targets) { |
| 195 | + await writeFile(fileInfo.path, JSON.stringify(fileInfo.message, null, args.indent)) |
| 196 | + } |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +export default { |
| 201 | + command, |
| 202 | + aliases, |
| 203 | + describe, |
| 204 | + builder, |
| 205 | + handler |
| 206 | +} |
0 commit comments