import { camelCase , flatten , omit , pick , zipObject } from 'lodash' ; import { basename , join } from 'path' ; import { directories } from '../util/directories' ; import { get } from '../util/get' ; import { link } from '../util/link' ; import { readdir } from '../util/readdir' ; import { separate } from '../util/separate' ; import { translate } from '../util/translate' ; import { width } from '../util/width' ; import { write } from '../util/write' ; const replace = { Yes : true , No : false , None : null , NA : null , NFS : - 1 , Unlimited : - 1 , '' : null , '—' : null , 'No lighting' : null , 'Not in catalog' : null , 'Does not play music' : null , } ; const duplicateValues = [ 'sourceSheet' , 'name' , 'diy' , 'patternCustomize' , 'kitCost' , 'size' , 'milesPrice' , 'sourceNotes' , 'versionAdded' , 'versionUnlocked' , 'interact' , 'tag' , 'outdoor' , 'speakerType' , 'lightingType' , 'catalog' , 'set' , 'series' , 'bodyTitle' , 'bodyCustomize' , 'buy' , 'sell' , 'customize' , 'translations' , 'labelThemes' , 'source' , 'style1' , 'style2' , 'hhaSeries' , 'hhaSet' , 'themesTranslations' , 'seriesTranslations' , 'hhaBasePoints' , 'primaryShape' , 'secondaryShape' , 'villagerEquippable' , 'seasonalAvailability' , 'type' , 'unlocked' , ] ; const separateValues = [ 'tier1Noun' , 'tier2Noun' , 'tier3Noun' , 'tier4Noun' , 'tier5Noun' , 'tier6Noun' , 'labelThemes' , 'source' , 'fog' , 'specialClouds' , 'description' , 'catchPhrase' , 'unlockNotes' , 'sourceNotes' , 'internalLabel' , 'northernHemisphereDates' , 'southernHemisphereDates' , '2020Dates' , '2021Dates' , ] ; const files = flatten ( [ readdir ( directories . raw ) ] ) ; for ( const file of files ) { const items = get ( file ) ; const variationNames = new Set ( ) ; let sanitized = [ ] ; const variations = [ ] ; for ( const item of items ) { const keys = Object . keys ( item ) . map ( ( key ) => ( key === '#' ? 'num' : camelCase ( key ) ) ) ; let values = Object . values ( item ) . map ( ( value ) => link ( value ) ) ; values = values . map ( ( value ) => ( replace . hasOwnProperty ( value ) ? replace [ value ] : value ) ) ; const object = zipObject ( keys , values ) ; for ( const value of separateValues ) { if ( object [ value ] ) object [ value ] = separate ( object [ value ] , ';' ) ; } translate ( object ) ; sanitized . push ( object ) ; if ( object . variation || object . pattern ) { variationNames . add ( object . name ) ; } } for ( const name of variationNames ) { const duplicates = sanitized . filter ( ( item ) => item . name === name ) ; const values = duplicates . map ( ( item ) => omit ( item , duplicateValues ) ) ; const base = pick ( duplicates [ 0 ] , duplicateValues ) ; variations . push ( { ... base , variations : values } ) ; } sanitized = sanitized . filter ( ( item ) => ! variationNames . has ( item . name ) ) ; write ( join ( directories . sanitized , basename ( file ) ) , [ ... sanitized , ... variations ] , width ( basename ( file ) ) ) ; }