source/lib/VBConfig.js
// Set up the config object by reading the config JSON file

Set up the config object by reading the config JSON file
Set up the config object by reading the config JSON file
init() {
    const fname = 'vb-config.json'
    const defaultFolder = app.getPath('userData')
    let configFolder = ''
    let configPath = ''
    let data = ''
    let version = ''
    try {
      const stats = fs.statSync(defaultFolder)
      if (stats && stats.isDirectory()) {
        configFolder = defaultFolder
      }
    }
    catch (e) {
    }
    if (configFolder === '') {
      try {
        fs.mkdirSync(defaultFolder)
        configFolder = defaultFolder
      }
      catch (e) {
        console.log('VBConfig could not create userData folder:', e)
      }
    }
    configPath = path.join(configFolder, fname)
    try {
      data = fs.readFileSync(configPath, 'utf8')
    }
    catch (e) {
      data = ''
    }
    if (data) {
      try {
        this.config = JSON.parse(data)
      }
      catch (e) {
        this.config = null
        console.log('VBConfig. JSON.parse error:', e)
      }
    }
    if (!this.config) {
      try {
        data = fs.readFileSync(path.join(__dirname, '..', fname), 'utf8')
        version = app.getVersion()
      }
      catch (e) {
        data = ''
        console.log('VBConfig. Unable to read default config: Error:', e)
      }
      if (data) {
        try {
          this.config = JSON.parse(data)
          if (version) {
            this.config.version = version
          }
        }
        catch (e) {
          this.config = null
          console.log('VBConfig. JSON.parse error for default config:', e)
        }
      }
    }
    if (this.config) {
      this.config.configPath = configPath
    }
    return this.config
  }
init ( ) { const fname = 'vb-config.json' const defaultFolder = app . getPath ( 'userData' ) let configFolder = '' let configPath = '' let data = '' let version = '' try { const stats = fs . statSync ( defaultFolder ) if ( stats && stats . isDirectory ( ) ) { configFolder = defaultFolder } } catch ( e ) { } if ( configFolder === '' ) { try { fs . mkdirSync ( defaultFolder ) configFolder = defaultFolder } catch ( e ) { console . log ( 'VBConfig could not create userData folder:' , e ) } } configPath = path . join ( configFolder , fname ) try { data = fs . readFileSync ( configPath , 'utf8' ) } catch ( e ) { data = '' } if ( data ) { try { this . config = JSON . parse ( data ) } catch ( e ) { this . config = null console . log ( 'VBConfig. JSON.parse error:' , e ) } } if ( ! this . config ) { try { data = fs . readFileSync ( path . join ( __dirname , '..' , fname ) , 'utf8' ) version = app . getVersion ( ) } catch ( e ) { data = '' console . log ( 'VBConfig. Unable to read default config: Error:' , e ) } if ( data ) { try { this . config = JSON . parse ( data ) if ( version ) { this . config . version = version } } catch ( e ) { this . config = null console . log ( 'VBConfig. JSON.parse error for default config:' , e ) } } } if ( this . config ) { this . config . configPath = configPath } return this . config }
Set up the config object by reading the config JSON file
Set up the config object by reading the config JSON file
// Filename for the config file
 // Use the user's appData folder for the config file
 // Actual folder used for the config file
 // Full pathname of the config file
 // JSON data read from config file
 // Current app version if default config file is used
 // Determine whether the config folder exists
 // It's okay if the userData folder is missing
 // the first time the application is run
 // If config folder does not exist, attempt to create it
 // Append config filename to folder name
 // Note that if we couldn't create the config folder,
 // then the default program folder is used instead
 // Attempt to read the config file (it won't exist the first time run)
 // If the config JSON data was read, parse it into a config object
 // Attempt to parse the JSON data in the config file
 // If no config data found, use the default config file
 // Attempt to parse the JSON data in the default config file
 // If default config file used, fill in the current package version
 // If we read the config data, save the pathname used in the config obj
 // Otherwise it will be a fatal error, to be handled by the caller
