-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle and prevent accidental damage to the configuration file
- Loading branch information
1 parent
4a6576c
commit a946c16
Showing
10 changed files
with
337 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,38 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
import { JSONFile, JSONFileSync } from '@autorecord/shared' | ||
import { logger } from './logger' | ||
|
||
export async function readJSONFile<T = unknown>(filePath: string, defaultValue: T): Promise<T> { | ||
if (!fs.existsSync(filePath)) return defaultValue | ||
|
||
const buffer = await fs.promises.readFile(filePath) | ||
return JSON.parse(buffer.toString('utf8')) as T | ||
try { | ||
const buffer = await fs.promises.readFile(filePath) | ||
return JSON.parse(buffer.toString('utf8')) as T | ||
} catch (error) { | ||
logger.error('readJSONFile error', filePath, error) | ||
return defaultValue | ||
} | ||
} | ||
|
||
export function readJSONFileSync<T = unknown>(filePath: string, defaultValue: T): T { | ||
if (!fs.existsSync(filePath)) return defaultValue | ||
|
||
const buffer = fs.readFileSync(filePath) | ||
return JSON.parse(buffer.toString('utf8')) as T | ||
try { | ||
const buffer = fs.readFileSync(filePath) | ||
return JSON.parse(buffer.toString('utf8')) as T | ||
} catch (error) { | ||
logger.error('readJSONFileSync error', filePath, error) | ||
return defaultValue | ||
} | ||
} | ||
|
||
export async function writeJSONFile<T = unknown>(filePath: string, json: T): Promise<void> { | ||
fs.mkdirSync(path.dirname(filePath), { recursive: true }) | ||
await fs.promises.writeFile(filePath, JSON.stringify(json)) | ||
await new JSONFile<T>(filePath).write(json) | ||
} | ||
|
||
export function writeJSONFileSync<T = unknown>(filePath: string, json: T): void { | ||
fs.mkdirSync(path.dirname(filePath), { recursive: true }) | ||
fs.writeFileSync(filePath, JSON.stringify(json)) | ||
new JSONFileSync<T>(filePath).write(json) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.