author linhuiw
 *  desc import * as vscode from 'vscode' ; import * as fs from 'fs-extra' ; import * as _ from 'lodash' ; import * as prettier from 'prettier' ; import { getLangData } from './getLangData' ; import { getLangPrefix } from './utils' ; import { LANG_PREFIX } from './const' ; export function updateLangFiles ( keyValue , text , validateDuplicate ) { if ( ! keyValue . startsWith ( 'I18N.' ) ) { return ; } const [ , filename , ... restPath ] = keyValue . split ( '.' ) ; const fullKey = restPath . join ( '.' ) ; const targetFilename = `${ getLangPrefix ( ) || LANG_PREFIX }${ filename }.ts` ; if ( ! fs . existsSync ( targetFilename ) ) { fs . outputFileSync ( targetFilename , generateNewLangFile ( fullKey , text ) ) ; addImportToMainLangFile ( filename ) ; vscode . window . showInformationMessage ( `成功新建语言文件 ${ targetFilename }` ) ; } else { const mainContent = getLangData ( targetFilename ) ; const obj = mainContent ; if ( Object . keys ( obj ) . length === 0 ) { vscode . window . showWarningMessage ( `${ filename } 解析失败，该文件包含的文案无法自动补全` ) ; } if ( validateDuplicate && _ . get ( obj , fullKey ) !== undefined ) { vscode . window . showErrorMessage ( `${ targetFilename } 中已存在 key 为 \`${ fullKey }\` 的翻译，请重新命名变量` ) ; throw new Error ( 'duplicate' ) ; } text = text . replace ( /\\n/gm , '\n' ) ; _ . set ( obj , fullKey , text ) ; fs . writeFileSync ( targetFilename , prettierFile ( `export default ${ JSON . stringify ( obj , null , 2 ) }` ) ) ; } } param fileContent function prettierFile ( fileContent ) { try { return prettier . format ( fileContent , { parser : 'typescript' , trailingComma : 'all' , singleQuote : true } ) ; } catch ( e ) { console . error ( `代码格式化报错！${ e . toString ( ) }\n代码为：${ fileContent }` ) ; return fileContent ; } } export function generateNewLangFile ( key , value ) { const obj = _ . set ( { } , key , value ) ; return prettierFile ( `export default ${ JSON . stringify ( obj , null , 2 ) }` ) ; } export function addImportToMainLangFile ( newFilename ) { let mainContent = '' ; const langPrefix = getLangPrefix ( ) || LANG_PREFIX ; if ( fs . existsSync ( `${ langPrefix }index.ts` ) ) { mainContent = fs . readFileSync ( `${ langPrefix }index.ts` , 'utf8' ) ; mainContent = mainContent . replace ( /^(\s*import.*?;)$/m , `$1\nimport ${ newFilename } from './${ newFilename }';` ) ; if ( /\n(}\);)/ . test ( mainContent ) ) { if ( /\,\n(}\);)/ . test ( mainContent ) ) { /** 最后一行包含,号 */ mainContent = mainContent . replace ( /(}\);)/ , `  ${ newFilename },\n$1` ) ; } else { /** 最后一行不包含,号 */ mainContent = mainContent . replace ( /\n(}\);)/ , `,\n  ${ newFilename },\n$1` ) ; } } if ( /\n(};)/ . test ( mainContent ) ) { if ( /\,\n(};)/ . test ( mainContent ) ) { /** 最后一行包含,号 */ mainContent = mainContent . replace ( /(};)/ , `  ${ newFilename },\n$1` ) ; } else { /** 最后一行不包含,号 */ mainContent = mainContent . replace ( /\n(};)/ , `,\n  ${ newFilename },\n$1` ) ; } } } else { mainContent = `import ${ newFilename } from './${ newFilename }';\n\nexport default Object.assign({}, {\n  ${ newFilename },\n});` ; } fs . outputFileSync ( `${ langPrefix }index.ts` , mainContent ) ; }