11import type { IpLocationApiInputSettings , IpLocationApiSettings } from './functions/getSettings.js'
22/* eslint-disable jsdoc/check-param-names */
33import { existsSync } from 'node:fs'
4- import { mkdir } from 'node:fs/promises'
4+ import { cp , mkdir , readdir , rename , rm , writeFile } from 'node:fs/promises'
5+ import path from 'node:path'
6+ import { DATABASE_SUFFIX_SHA } from './constants.js'
57import { createDatabase } from './functions/database/createDatabase.js'
68import { downloadAndExtractDatabase } from './functions/database/downloadAndExtractDatabase.js'
79import { getSettings } from './functions/getSettings.js'
@@ -11,18 +13,34 @@ import { getSettings } from './functions/getSettings.js'
1113 * @param inputSettings - Partial settings to override the default ones.
1214 * @returns An array of extracted file names or false if no update was needed.
1315 */
14- export async function update ( inputSettings ?: Partial < IpLocationApiInputSettings > ) : Promise < false | string [ ] > {
16+ export async function update ( inputSettings ?: Partial < IpLocationApiInputSettings > ) : Promise < void > {
1517 const settings = getSettings ( inputSettings )
1618 await ensureDirectoriesExist ( settings )
17- const { files, dataType } = await downloadAndExtractDatabase ( settings )
19+ const { files, sha256 } = await downloadAndExtractDatabase ( settings )
1820 if ( ! files )
19- return false
21+ return
2022
2123 // TODO: Add debug log
22- await createDatabase ( files , settings , dataType )
24+ await createDatabase ( files , settings )
2325 // TODO: Add debug log
2426
25- return files
27+ //* Save the sha256 hash of the database
28+ await writeFile ( path . join ( settings . fieldDir , `${ settings . series } -${ settings . dataType } -CSV${ DATABASE_SUFFIX_SHA } ` ) , sha256 )
29+
30+ //* Rename the temporary files to the correct name
31+ const tmpFiles = ( await readdir ( settings . fieldDir ) ) . filter ( file => file . endsWith ( '.tmp' ) )
32+ for ( const file of tmpFiles ) {
33+ await rename ( path . join ( settings . fieldDir , file ) , path . join ( settings . fieldDir , file . replace ( '.tmp' , '' ) ) )
34+ }
35+
36+ if ( settings . smallMemory ) {
37+ //* Copy the temporary files to the correct name
38+ await cp ( path . join ( settings . fieldDir , 'v4-tmp' ) , path . join ( settings . fieldDir , 'v4' ) , { recursive : true , force : true } )
39+ await cp ( path . join ( settings . fieldDir , 'v6-tmp' ) , path . join ( settings . fieldDir , 'v6' ) , { recursive : true , force : true } )
40+ //* Remove the temporary files
41+ await rm ( path . join ( settings . fieldDir , 'v4-tmp' ) , { recursive : true , force : true , maxRetries : 3 } )
42+ await rm ( path . join ( settings . fieldDir , 'v6-tmp' ) , { recursive : true , force : true , maxRetries : 3 } )
43+ }
2644}
2745
2846/**
0 commit comments