author linhuiw
 *  desc import * as fs from 'fs' ; import * as path from 'path' ; import { getKiwiDir , getLangDir , traverse } from './utils' ; const lookingForString = '' ; function findUnUsed ( ) { const srcLangDir = path . resolve ( getKiwiDir ( ) , 'zh-CN' ) ; let files = fs . readdirSync ( srcLangDir ) ; files = files . filter ( file => file . endsWith ( '.ts' ) && file !== 'index.ts' ) ; const unUnsedKeys = [ ] ; files . map ( file => { const srcFile = path . resolve ( srcLangDir , file ) ; const { default : messages } = require ( srcFile ) ; const filename = path . basename ( file , '.ts' ) ; traverse ( messages , ( text , path ) => { const key = `I18N.${ filename }.${ path }` ; const hasKey = recursiveReadFile ( './src' , key ) ; if ( ! hasKey ) { unUnsedKeys . push ( key ) ; } } ) ; } ) ; console . log ( unUnsedKeys , 'unUnsedKeys' ) ; } param fileName function recursiveReadFile ( fileName , text ) { let hasText = false ; if ( ! fs . existsSync ( fileName ) ) return ; if ( isFile ( fileName ) && ! hasText ) { check ( fileName , text , ( ) => { hasText = true ; } ) ; } if ( isDirectory ( fileName ) ) { var files = fs . readdirSync ( fileName ) . filter ( file => { return ! file . startsWith ( '.' ) && ! [ 'node_modules' , 'build' , 'dist' ] . includes ( file ) ; } ) ; files . forEach ( function ( val , key ) { var temp = path . join ( fileName , val ) ; if ( isDirectory ( temp ) && ! hasText ) { hasText = recursiveReadFile ( temp , text ) ; } if ( isFile ( temp ) && ! hasText ) { check ( temp , text , ( ) => { hasText = true ; } ) ; } } ) ; } return hasText ; } param fileName function check ( fileName , text , callback ) { var data = readFile ( fileName ) ; var exc = new RegExp ( text ) ; if ( exc . test ( data ) ) { callback ( ) ; } } param fileName function isDirectory ( fileName ) { if ( fs . existsSync ( fileName ) ) { return fs . statSync ( fileName ) . isDirectory ( ) ; } } param fileName function isFile ( fileName ) { if ( fs . existsSync ( fileName ) ) { return fs . statSync ( fileName ) . isFile ( ) ; } } param fileName function readFile ( fileName ) { if ( fs . existsSync ( fileName ) ) { return fs . readFileSync ( fileName , 'utf-8' ) ; } } export { findUnUsed } ;