diff --git a/src/file-utility/file-utility.test.ts b/src/file-utility/file-utility.test.ts index 453e4e3..d7ed4c6 100644 --- a/src/file-utility/file-utility.test.ts +++ b/src/file-utility/file-utility.test.ts @@ -29,6 +29,30 @@ describe('fileUtility functions', () => { expect(newTargetContent).toEqual(srcContent); }); + + it('should not crash if the file does not exist', async () => { + const srcPath = `${tmpFolder}/a.md`; + const targetPath = `${tmpFolder}/b.md`; + + await setup(); + + overwriteFile(srcPath, targetPath); + }); + + it('should write the file if the file destination does not exist', async () => { + const srcPath = `${tmpFolder}/a.md`; + const srcContent = nanoid(); + + const targetPath = `${tmpFolder}/b.md`; + + await setup(); + fs.writeFileSync(srcPath, srcContent); + + overwriteFile(srcPath, targetPath); + const newTargetContent = fs.readFileSync(targetPath).toString(); + + expect(newTargetContent).toEqual(srcContent); + }); }); describe('overwriteJsonFileFields function', () => { diff --git a/src/file-utility/file-utility.ts b/src/file-utility/file-utility.ts index 0d913a9..3199cba 100644 --- a/src/file-utility/file-utility.ts +++ b/src/file-utility/file-utility.ts @@ -1,6 +1,7 @@ -import fs from 'fs'; +import fs, { existsSync } from 'fs'; export const overwriteFile = (fileSrcPath: string, fileTargetPath: string): void => { + if (!existsSync(fileSrcPath)) return; const content = fs.readFileSync(fileSrcPath); fs.writeFileSync(fileTargetPath, content); };