Skip to content

Commit

Permalink
feat(util/fs): existFile option option
Browse files Browse the repository at this point in the history
  • Loading branch information
AliMD committed May 27, 2023
1 parent 3a43034 commit c8c2020
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions core/util/src/node/fs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {existsSync, readFileSync, writeFileSync, renameSync, mkdirSync} from 'node:fs';
import {rename, mkdir, writeFile, readFile, rm, symlink} from 'node:fs/promises';
import {existsSync, readFileSync, writeFileSync, mkdirSync, copyFileSync, renameSync} from 'node:fs';
import {mkdir, writeFile, readFile, rm, symlink, copyFile, rename} from 'node:fs/promises';
import {dirname} from 'node:path';

import {createLogger} from '@alwatr/logger';
Expand Down Expand Up @@ -99,6 +99,7 @@ export const readJsonFile = async <T extends StringifyableRecord = Stringifyable
export const writeJsonFileSync = <T extends StringifyableRecord = StringifyableRecord>(
path: string,
data: T,
existFile: 'replace' | 'copy' | 'rename' = 'replace',
space?: string | number,
): void => {
logger.logMethodArgs?.('writeJsonFileSync', path);
Expand All @@ -118,10 +119,15 @@ export const writeJsonFileSync = <T extends StringifyableRecord = StringifyableR

if (existsSync(path)) {
try {
renameSync(path, path + '.bk');
if (existFile === 'copy') {
copyFileSync(path, path + '.bk');
}
else if (existFile === 'rename') {
renameSync(path, path + '.bk');
}
}
catch (err) {
logger.error('writeJsonFileSync', 'rename_failed', err);
logger.error('writeJsonFileSync', 'rename_copy_failed', err);
}
}
else {
Expand Down Expand Up @@ -153,6 +159,7 @@ export const writeJsonFileSync = <T extends StringifyableRecord = StringifyableR
export const writeJsonFile = async <T extends StringifyableRecord = StringifyableRecord>(
path: string,
data: T,
existFile: 'replace' | 'copy' | 'rename' = 'replace',
space?: string | number,
): Promise<void> => {
logger.logMethodArgs?.('writeJsonFile', path);
Expand All @@ -173,10 +180,15 @@ export const writeJsonFile = async <T extends StringifyableRecord = Stringifyabl

if (existsSync(path)) {
try {
await rename(path, path + '.bk');
if (existFile === 'copy') {
await copyFile(path, path + '.bk');
}
else if (existFile === 'rename') {
await rename(path, path + '.bk');
}
}
catch (err) {
logger.error('writeJsonFile', 'rename_failed', err);
logger.error('writeJsonFile', 'rename_copy_failed', err);
}
}
else {
Expand Down

0 comments on commit c8c2020

Please sign in to comment.