import * as fs from 'fs' ; import { join } from 'path' ; import { availableLocales } from '../../cldr' ; import { LanguageResolver , Locale } from '@phensley/cldr-core' ; /**
 * Write data to a file.
 */ export const writeData = ( path , data ) => { console . warn ( `writing '${ path }'` ) ; fs . writeFileSync ( path , data , { encoding : 'binary' } ) ; } ; /**
 * Write JSON-encoded data to a file.
 */ export const writeJSON = ( path , obj ) => { const data = JSON . stringify ( obj , undefined , 2 ) ; fs . writeFileSync ( path , data , { encoding : 'utf-8' } ) ; } ; export interface ProjectInfo { version : string ; cldrVersion : string ; } export const getProjectInfo = ( ) : ProjectInfo => { const path = join ( __dirname , '..' , '..' , '..' , 'package.json' ) ; const raw = fs . readFileSync ( path , { encoding : 'utf-8' } ) ; const pkg = JSON . parse ( raw ) ; return { version : pkg . version , cldrVersion : pkg . cldrversion } ; } ; /**
 * Convert a string into a locale object.
 */ const getLocale = ( id ) : Locale => ( { id , tag : LanguageResolver . resolve ( id ) } ) ; export type LocaleMap = { [ x ] : Locale [ ] ; } ; /**
 * Build a mapping of language -> Locale[].
 */ export const buildLocaleMap = ( ) : LocaleMap => availableLocales ( ) . map ( getLocale ) . reduce ( ( o , c ) => { const lang = c . tag . language ( ) ; const values = o [ lang ] || [ ] ; values . push ( c ) ; o [ lang ] = values ; return o ; } , { } ) ; /**
 * Ensure any languages passed on the command line are valid / known.
 */ export const checkLanguages = ( languages , localeMap ) : string [ ] => { languages . forEach ( ( k ) => { if ( localeMap [ k ] === undefined ) { throw new Error ( `Unknown or unsupported language '${ k }'` ) ; } } ) ; return languages ; } ;