import { promises as fs , readdirSync } from 'fs' ; import { difference , keys , uniq } from 'lodash-unified' ; import { resolve , relative } from 'path' ; import { EXTENSION_SOURCE , ROOT_PATH , walk } from '../utils' ; import { getUsedKeys } from './ast' ; export const LOCALE_PATH = resolve ( EXTENSION_SOURCE , '../shared-ui/locales' ) ; export const LOCALE_NAMES = readdirSync ( LOCALE_PATH ) . filter ( ( name ) => name . endsWith ( '.json' ) ) . map ( ( x ) => x . slice ( 0 , - 5 ) ) ; export function getMessagePath ( name ) { return resolve ( LOCALE_PATH , name + '.json' ) ; } export function getLocaleRelativePath ( ... paths ) { return relative ( ROOT_PATH , resolve ( LOCALE_PATH , ... paths ) ) ; } export async function readMessages ( name ) { return JSON . parse ( await fs . readFile ( getMessagePath ( name ) , 'utf-8' ) ) ; } export async function writeMessages ( name , messages ) { await fs . writeFile ( getMessagePath ( name ) , JSON . stringify ( messages , null , 4 ) + '\n' , 'utf-8' ) ; } export async function findAllUsedKeys ( ) { const usedKeys = [ ] ; for await ( const file of walk ( EXTENSION_SOURCE , /\.(tsx?)$/ ) ) { usedKeys . push ( ... getUsedKeys ( await fs . readFile ( file , 'utf-8' ) ) ) ; } return uniq ( usedKeys ) ; } export async function findAllUnusedKeys ( ) { return difference ( keys ( await readMessages ( 'en-US' ) ) , await findAllUsedKeys ( ) ) ; }