1- /*
2- * This file is part of Building Blocks CommonID Tool
3- * Copyright (c) 2024 WFP
4- *
5- * This program is free software: you can redistribute it and/or modify
6- * it under the terms of the GNU General Public License as published by
7- * the Free Software Foundation, version 3.
8- *
9- * This program is distributed in the hope that it will be useful, but
10- * WITHOUT ANY WARRANTY; without even the implied warranty of
11- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12- * General Public License for more details.
13- *
14- * You should have received a copy of the GNU General Public License
15- * along with this program. If not, see <http://www.gnu.org/licenses/>.
16- */
17-
18- import { join , dirname } from 'node:path' ;
19- import { fileURLToPath } from 'node:url' ;
1+ // Common Identifier Application
2+ // Copyright (C) 2024 World Food Programme
3+
4+ // This program is free software: you can redistribute it and/or modify
5+ // it under the terms of the GNU Affero General Public License as published by
6+ // the Free Software Foundation, either version 3 of the License, or
7+ // (at your option) any later version.
8+
9+ // This program is distributed in the hope that it will be useful,
10+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+ // GNU Affero General Public License for more details.
13+
14+ // You should have received a copy of the GNU Affero General Public License
15+ // along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+ import { dirname } from 'node:path' ;
2018import fs from 'node:fs' ;
2119import Debug from 'debug' ;
2220const log = Debug ( 'CID:ConfigStore' ) ;
@@ -29,7 +27,7 @@ import type { AppConfigData, Config } from './Config.js';
2927// Ensure the application's config file directory exists
3028function ensureAppDirectoryExists ( appDir : string ) {
3129 if ( ! fs . existsSync ( appDir ) ) {
32- console . log ( "Application directory '" , appDir , "' does not exits -- creating it" ) ;
30+ log ( "Application directory '" , appDir , "' does not exits -- creating it" ) ;
3331 fs . mkdirSync ( appDir /*, { recursive: true } */ ) ;
3432 }
3533}
@@ -42,7 +40,7 @@ function saveConfig(configData: Config.Options, outputPath: string) {
4240 // update the config hash on import to account for the
4341 const outputData = JSON . stringify ( configData , null , ' ' ) ;
4442 fs . writeFileSync ( outputPath , outputData , CONFIG_FILE_ENCODING ) ;
45- console . log ( 'Written config data to ' , outputPath ) ;
43+ log ( 'Written config data to ' , outputPath ) ;
4644}
4745
4846interface ConfigStorePaths {
@@ -110,20 +108,20 @@ export class ConfigStore {
110108 // if the load succesds we have a valid config -- use it as a
111109 // user-provided one
112110 if ( userConfigLoad . success ) {
113- console . log ( 'User config validation success - using it as config' ) ;
111+ log ( 'User config validation success - using it as config' ) ;
114112 this . useUserConfig ( userConfigLoad . config , userConfigLoad . lastUpdated ) ;
115113 return ;
116114 }
117115
118- console . log ( 'User config validation not successful - attempting to load backup config' ) ;
116+ log ( 'User config validation not successful - attempting to load backup config' ) ;
119117 // if the default config load failed use the backup default
120118 // from the app distribution
121119 const backupConfigLoad = loadConfig ( this . getBackupConfigFilePath ( ) , this . getRegion ( ) ) ;
122120
123121 // if the load succesds we have a valid config -- use it as
124122 // a config-from-backup
125123 if ( backupConfigLoad . success ) {
126- console . log ( 'Backup config validation success - using it as config' ) ;
124+ log ( 'Backup config validation success - using it as config' ) ;
127125 backupConfigLoad . config . isBackup = true ;
128126 this . useBackupConfig ( backupConfigLoad . config ) ;
129127 return ;
@@ -133,7 +131,7 @@ export class ConfigStore {
133131 this . loadError = backupConfigLoad . error ;
134132
135133 // if the backup config fails to load we are screwed
136- console . log (
134+ log (
137135 'Backup config load failed - the application should alert the user: ' ,
138136 backupConfigLoad . error ,
139137 ) ;
@@ -178,21 +176,21 @@ export class ConfigStore {
178176 // This method does not save the backup as the user config, only deletes the user config file
179177 removeUserConfig ( ) {
180178 // attempt to load the backup config
181- console . log ( '[removeUserConfig] Attempting to remove user configuration and replace with backup.' ) ;
179+ log ( '[removeUserConfig] Attempting to remove user configuration and replace with backup.' ) ;
182180
183181 // if the current config is already a backup config don't do anything
184182 if ( this . isCurrentConfigBackup ( ) ) {
185- console . log ( '[removeUserConfig] Already using a backup config -- bailing' ) ;
183+ log ( '[removeUserConfig] Already using a backup config -- bailing' ) ;
186184 // this is not an error - we're already using the backup
187185 return ;
188186 }
189187
190- console . log ( '[removeUserConfig] Trying to load backup config file' ) ;
188+ log ( '[removeUserConfig] Trying to load backup config file' ) ;
191189 const backupConfigLoad = loadConfig ( this . getBackupConfigFilePath ( ) , this . getRegion ( ) ) ;
192190
193191 // if failed return the error message (do not delete the user config yet)
194192 if ( ! backupConfigLoad . success ) {
195- console . log (
193+ log (
196194 'Backup config validation failed -- returning error and keeping existing user config:' ,
197195 backupConfigLoad . error ,
198196 ) ;
@@ -203,7 +201,7 @@ export class ConfigStore {
203201 }
204202
205203 // if successful use the loaded backup configuration
206- console . log ( '[removeUserConfig] Backup config validation success - using it as config' ) ;
204+ log ( '[removeUserConfig] Backup config validation success - using it as config' ) ;
207205 backupConfigLoad . config . isBackup = true ;
208206 this . useBackupConfig ( backupConfigLoad . config ) ;
209207
@@ -244,7 +242,7 @@ export class ConfigStore {
244242 // deletes the user configuration file
245243 _deleteUserConfigFile ( ) {
246244 const configPath = this . getConfigFilePath ( ) ;
247- console . log ( 'Deleting config file: ' , configPath ) ;
245+ log ( 'Deleting config file: ' , configPath ) ;
248246 fs . unlinkSync ( configPath ) ;
249247 }
250248
0 commit comments