import fs from 'fs' ; import { transformFileSync } from '@babel/core' ; import prettier from 'prettier' ; import { t } from '../i18n' ; import i18nPlugin from '../plugin/babelPlugin' ; import Logger from '../util/logger' ; import { iTransInfo , iWordInfo , iI18nConf , iCmd , iWrapResult } from '../types' ; const originalScanWordInfoList = [ ] ; let generateFileCount = 0 ; param transInfo param code param filename param i18nConfig const generateFile = ( transInfo , code , filename , i18nConf ) => { if ( transInfo . needImport ) { code = `${ i18nConf . importStr }${ code }` ; } code = prettier . format ( code , { parser : 'typescript' , singleQuote : true , } ) ; fs . writeFileSync ( filename , code , 'utf8' ) ; } ; param files param i18nConf param cmdConf const wrap = ( files , i18nConf , cmdConf ) : iWrapResult => { const wrapResult = { wrapInfo : { WRAP_FILE : 0 } } ; files . forEach ( ( filename ) => { const transInfo = { needT : false , needTrans : false , needImport : true , wordInfoArray : [ ] , wrapCount : 0 , } ; const plugin = i18nPlugin ( transInfo , i18nConf ) ; const transResult = transformFileSync ( filename , { plugins : [ [ '@babel/plugin-syntax-typescript' , { isTSX : true } ] , plugin , ] , retainLines : true , } ) ; if ( transResult ) { const code = transResult . code as string ; const needTranslate = transInfo . needT || transInfo . needTrans ; if ( transInfo . wordInfoArray . length > 0 ) { originalScanWordInfoList . push ( transInfo . wordInfoArray ) ; wrapResult . originalScanWordInfoLit = originalScanWordInfoList ; } if ( needTranslate ) { if ( cmdConf . wrap ) { wrapResult . wrapInfo [ filename ] = transInfo . wrapCount ; generateFileCount += 1 ; generateFile ( transInfo , code , filename , i18nConf ) ; } } } } ) ; if ( generateFileCount > 0 ) { Logger . success ( t ( '【包裹】词条包裹已完成！' ) ) ; } else if ( cmdConf . wrap ) { Logger . warning ( t ( '【包裹】本次无词条被包裹！' ) ) ; } return wrapResult ; } ; export default wrap ;