-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ecbc003
commit a9074a1
Showing
5 changed files
with
159 additions
and
25 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { parse } from 'node:path' | ||
import { access, constants, mkdir, writeFile } from 'node:fs/promises' | ||
import { handleFileSystemError } from './error/filesystem-error' | ||
|
||
export const saveToFile = async (filePath: string, data: string) => { | ||
// Check if the path exists, and create it if it doesn't | ||
const resolvedPath = parse(filePath).dir | ||
try { | ||
await access(resolvedPath, constants.F_OK) | ||
} | ||
catch { | ||
try { | ||
await mkdir(resolvedPath, { recursive: true }) | ||
} | ||
catch (mkdirError) { | ||
handleFileSystemError('mkdir', mkdirError as Error) | ||
return // Exit early if the directory creation fails | ||
} | ||
} | ||
|
||
try { | ||
await writeFile(filePath, data, { mode: 0o600 }) | ||
} | ||
catch (writeError) { | ||
handleFileSystemError('write', writeError as Error) | ||
} | ||
} |