import fs from 'fs' ; import path from 'path' ; import fse from 'fs-extra' ; import inquirer from 'inquirer' ; import glob from 'glob' ; import Logger from './logger' ; import { I18N_CONFIGURATION_FILE_NAME } from '../config/const' ; import questions from '../config/questionList' ; import { formatJSON } from './helper' ; import { iI18nConf } from '../types' ; const i18nDefaultConfiguration = require ( '../config/i18n.default.config' ) ; param configuration const doGenerateConfiguration = ( configuration ) => { const configurationStr = formatJSON ( configuration ) ; fs . writeFileSync ( I18N_CONFIGURATION_FILE_NAME , configurationStr , 'utf8' ) ; Logger . success ( 'i18n.config.json is generated successfully' ) ; } ; param useDefaultConfig const generateConfiguration = ( useDefaultConfig ) : void => { if ( useDefaultConfig ) { doGenerateConfiguration ( i18nDefaultConfiguration ) ; } else { inquirer . prompt ( questions ) . then ( ( answers ) => { doGenerateConfiguration ( answers ) ; } ) ; } } ; param i18nConf returns const getMatchedFiles = ( i18nConf ) : string [ ] => { let files = [ ] ; const filePath = i18nConf . parsedPath as string ; const stat = fs . statSync ( filePath ) ; if ( stat . isFile ( ) ) { files . push ( filePath ) ; } else if ( stat . isDirectory ( ) ) { const { fileExt } = i18nConf ; const ext = fileExt . includes ( ',' ) ? `{${ fileExt }}` : fileExt ; const pattern = `${ filePath }/**/*.${ ext }` ; const ignorePath = i18nConf . parsedExclude ?. map ( ( item ) => { return item . includes ( '.' ) ? `${ filePath }/**/${ item }` : `${ filePath }/**/${ item }/**` ; } ) ; const option = { ignore : ignorePath , } ; files = glob . sync ( pattern , option ) ; } return files ; } ; returns const isI18nConfExited = ( ) : boolean => { const configFileName = path . resolve ( I18N_CONFIGURATION_FILE_NAME ) ; return fs . existsSync ( configFileName ) ; } ; returns const parseI18nConf = ( filePath ? , language ? , needPrint = true ) : iI18nConf | null => { const configFileName = path . resolve ( I18N_CONFIGURATION_FILE_NAME ) ; if ( isI18nConfExited ( ) ) { const i18nConf = fse . readJSONSync ( configFileName ) ; const importArray = i18nConf . importStr . split ( ' ' ) ; const specifiedPath = filePath || i18nConf . srcPath ; const specifiedLanguage = language || i18nConf . languages ; i18nConf . parsedExclude = i18nConf . exclude . split ( ',' ) ; i18nConf . parsedLanguages = specifiedLanguage . split ( ',' ) ; i18nConf . parsedPath = specifiedPath ; i18nConf . parsedImportKey = importArray [ importArray . length - 1 ] . replace ( '\n' , '' ) ; i18nConf . parsedExcludeWrapperFuncName = i18nConf . excludeWrapperFuncName . split ( ',' ) ; return i18nConf ; } if ( needPrint ) { Logger . error ( 'Missing configuration file, please Run "i18n-helper init" to generate it' ) ; } return null ; } ; export { generateConfiguration , getMatchedFiles , isI18nConfExited , parseI18nConf , } ;