44 * Handles reading, writing, and updating configuration files while maintaining formatting and comments.
55 */
66
7+ import TOML from "@iarna/toml" ;
78import fs from "fs" ;
89import path from "path" ;
9- import TOML from "@iarna/toml" ;
1010import { logger } from "../utils/logger.js" ;
1111
1212/**
@@ -69,7 +69,7 @@ function updateTomlValue(tomlContent, section, key, newValue) {
6969
7070 // Parse the line: key = value # comment
7171 const match = line . match (
72- / ^ ( \s * ) ( [ a - z A - Z _ ] [ a - z A - Z 0 - 9 _ - ] * ) ( \s * = \s * ) ( [ ^ # ] + ?) ( (?: \s * # .* ) ? ) $ /
72+ / ^ ( \s * ) ( [ a - z A - Z _ ] [ a - z A - Z 0 - 9 _ - ] * ) ( \s * = \s * ) ( [ ^ # ] + ?) ( (?: \s * # .* ) ? ) $ / ,
7373 ) ;
7474
7575 // Check if this line matches the key we're looking for
@@ -89,7 +89,7 @@ function updateTomlValue(tomlContent, section, key, newValue) {
8989 } else if ( Array . isArray ( newValue ) ) {
9090 // Simple array formatting
9191 const items = newValue . map ( ( v ) =>
92- typeof v === "string" ? `"${ v } "` : v
92+ typeof v === "string" ? `"${ v } "` : v ,
9393 ) ;
9494 formattedValue = `[${ items . join ( ", " ) } ]` ;
9595 } else if ( newValue === null || newValue === undefined ) {
@@ -111,7 +111,7 @@ function updateTomlValue(tomlContent, section, key, newValue) {
111111 // Warn if key was not found
112112 if ( ! updated ) {
113113 console . warn (
114- `Warning: Key "${ key } " in section "${ section || "root" } " not found`
114+ `Warning: Key "${ key } " in section "${ section || "root" } " not found` ,
115115 ) ;
116116 }
117117
@@ -165,13 +165,17 @@ class ConfigManager {
165165 * Creates a new ConfigManager instance.
166166 * @param {string } userConfigPath - Path to the user configuration file.
167167 * @param {string } templatePath - Path to the template configuration file.
168+ * @param {string } versionFilePath - Path to the version.toml (app version)
169+ * @param {string } appVersion - Current electron bundle version from package.json
168170 * @example
169- * const manager = new ConfigManager("/path/to/config.toml", "/path/to/template.toml");
171+ * const manager = new ConfigManager("/path/to/config.toml", "/path/to/template.toml", "/path/to/version.toml" app.getVersion() );
170172 */
171- constructor ( userConfigPath , templatePath ) {
173+ constructor ( userConfigPath , templatePath , versionFilePath , appVersion ) {
172174 // Store paths
173175 this . userConfigPath = userConfigPath ;
174176 this . templatePath = templatePath ;
177+ this . versionFilePath = versionFilePath ;
178+ this . appVersion = appVersion ;
175179
176180 // Ensure user config exists (copy from template on first run)
177181 this . ensureConfigExists ( ) ;
@@ -192,16 +196,44 @@ class ConfigManager {
192196
193197 // Copy template if user config doesn't exist
194198 if ( ! fs . existsSync ( this . userConfigPath ) ) {
195- if ( fs . existsSync ( this . templatePath ) ) {
196- // Copy template to user config location
197- fs . copyFileSync ( this . templatePath , this . userConfigPath ) ;
198- logger . config . info (
199- `Created config from template: ${ this . userConfigPath } `
200- ) ;
201- } else {
202- // Throw error if template is missing
199+ if ( ! fs . existsSync ( this . templatePath ) ) {
203200 throw new Error ( `Template not found: ${ this . templatePath } ` ) ;
204201 }
202+
203+ // Copy template to user config location
204+ fs . copyFileSync ( this . templatePath , this . userConfigPath ) ;
205+ logger . config . info (
206+ `Created config from template: ${ this . userConfigPath } ` ,
207+ ) ;
208+
209+ fs . writeFileSync (
210+ this . versionFilePath ,
211+ `version = "${ this . appVersion } "` ,
212+ "utf-8" ,
213+ ) ;
214+ logger . config . info ( `Created app version file: ${ this . versionFilePath } ` ) ;
215+ return ;
216+ }
217+
218+ // If config does exist, get app's version
219+ // In case version.toml doesn't exists it returns null
220+ const storedVersion = fs . existsSync ( this . versionFilePath )
221+ ? ( fs
222+ . readFileSync ( this . versionFilePath , "utf-8" )
223+ . trim ( )
224+ . match ( / ^ v e r s i o n \s * = \s * " ( .+ ) " $ / ) ?. [ 1 ] ?? null )
225+ : null ;
226+
227+ if ( storedVersion !== this . appVersion ) {
228+ fs . copyFileSync ( this . templatePath , this . userConfigPath ) ;
229+ fs . writeFileSync (
230+ this . versionFilePath ,
231+ `version = "${ this . appVersion } "` ,
232+ "utf-8" ,
233+ ) ;
234+ logger . config . info (
235+ `Config updated from template (from version ${ storedVersion ?? "unknown" } to ${ this . appVersion } )` ,
236+ ) ;
205237 }
206238 }
207239
@@ -360,4 +392,4 @@ class ConfigManager {
360392 }
361393}
362394
363- export { ConfigManager , updateTomlValue , updateTomlFromObject } ;
395+ export { ConfigManager , updateTomlFromObject , updateTomlValue } ;
0 commit comments