import path from 'path' ; import { Uri , workspace , window , commands } from 'vscode' ; import fg from 'fast-glob' ; import { Commands } from './commands' ; import { ExtensionModule } from '~/modules' ; import { Config } from '~/core' ; import i18n from '~/i18n' ; import { Log } from '~/utils' ; export class ConfigLocalesGuide { static async prompt ( ) { const okText = i18n . t ( 'prompt.config_locales_button' ) ; const result = await window . showInformationMessage ( i18n . t ( 'prompt.config_locales_info' ) , okText ) ; if ( result !== okText ) return ; this . config ( ) ; } static async config ( ) { const dirs = await this . pickDir ( ) ; Config . updateLocalesPaths ( dirs ) ; this . success ( ) ; } static async pickDir ( ) { const rootPath = workspace . workspaceFolders ?. [ 0 ] ?. uri . path ; if ( ! rootPath ) return [ ] ; const dirs = await window . showOpenDialog ( { defaultUri : Uri . file ( rootPath ) , canSelectFolders : true , } ) ; if ( ! dirs ) return [ ] ; return dirs . map ( ( item ) => { if ( process . platform === 'win32' ) return item . path . slice ( 1 ) ; return item . path ; } ) . map ( pa => path . relative ( rootPath , pa ) . replace ( /\\/g , '/' ) ) ; } static async success ( ) { await window . showInformationMessage ( i18n . t ( 'prompt.config_locales_success' ) ) ; } static async autoSet ( ) { const rootPath = workspace . rootPath ; if ( ! rootPath ) return ; const pattern = [ '**/**/(locales|locale|i18n|lang|langs|language|languages|messages)' ] ; const result = await fg ( pattern , { cwd : rootPath , ignore : [ '**/node_modules' , '**/dist' , '**/test' , '**/tests' , '**/tmp' , '**/build' , '**/.build' , '**/logs' , '**/vendor' , '**/vendors' , '**/target' , ] , onlyDirectories : true , } ) ; if ( result . length ) { Config . updateLocalesPaths ( result ) ; await window . showInformationMessage ( i18n . t ( 'prompt.config_locales_auto_success' , result . join ( ';' ) . toString ( ) ) ) ; } else { Log . warn ( i18n . t ( 'prompt.locales_dir_not_found' ) , false ) ; this . prompt ( ) ; } } } export default < ExtensionModule > function ( ) { return [ commands . registerCommand ( Commands . config_locales_auto , ( ) => ConfigLocalesGuide . autoSet ( ) ) , commands . registerCommand ( Commands . config_locales , ( ) => ConfigLocalesGuide . config ( ) ) , ] ; } ;